OctoberCMS Builder plugin with state and suburb - php

I have created two plugins (States and Suburbs) using Builder plugin and so far it works great.
The Thing is, in States plugin, I am simply allowing ability to add state names and in Suburbs plugin, I am allowing users to select State first and then enter suburb name. All works fine as of now in these two plugins.
Now the thing is, I have a third plugin called as Properties in which I have both these 2 drop-downs State and Suburb but as of now All States and All Suburbs are showing. But I want users to select first State and then based on state selection it should should all its suburbs to my another Suburb drop-down.
I have tried to use dependsOn which Builder plugin provides but I just am not able to understand the flow to achieve it step by step based on the current scenario I have. Below is the code what I have done and tried so far.
plugins\technobrave\properties\models\Property.php
<?php namespace Technobrave\Properties\Models;
use Model;
use technobrave\states\Models\State as State;
use technobrave\suburbs\Models\Suburb as Suburb;
public function getStateIdOptions()
{
// getting all states
$get_all_states = State::all();
$fields[''] = 'Select any State';
foreach ($get_all_states as $current_state) {
$fields[$current_state->attributes['id']] = $current_state->attributes['state_name'];
}
ksort($fields);
return $fields;
}
public function getSuburbIdOptions($stateId)
{
// getting all suburbs
$get_all_suburbs = Suburb::all();
$fields[''] = 'Select any Suburb';
foreach ($get_all_suburbs as $current_suburb) {
$fields[$current_suburb->attributes['id']] = $current_suburb->attributes['suburb'];
}
ksort($fields);
return $fields;
}
}
How can I do this from here on ? Any help or guidance will be highly appreciated and useful.
Thanks

Ok Guys,
Eventually I have come up with a solution. Here is what I have done.
fields.yaml File: technobrave\properties\models\property\fields.yaml
state_id:
label: 'State:'
span: auto
required: 1
type: dropdown
tab: 'Address Information'
suburb_id:
label: 'Suburb:'
span: auto
required: 1
type: dropdown
tab: 'Address Information'
placeholder: 'Select any Suburb'
dependsOn: state_id
As you can see above,
In suburb_id I have added, below two lines of code.
placeholder: 'Select any Suburb'
dependsOn: state_id
Property Model File: technobrave\properties\models\Property.php
use Model;
use technobrave\states\Models\State as State;
use technobrave\suburbs\Models\Suburb as Suburb;
public function getStateIdOptions()
{
// getting all states
$get_all_states = State::all();
$fields[''] = 'Select any State';
foreach ($get_all_states as $current_state) {
$fields[$current_state->attributes['id']] = $current_state->attributes['state_name'];
}
ksort($fields);
return $fields;
}
public function getSuburbIdOptions()
{
return Suburb::getNameList($this->state_id);
}
Here above, I have just updated getSuburbIdOptions method with below code and removed my old code.
return Suburb::getNameList($this->state_id);
Then I went to my Suburbs plugin.
Suburb Model File: technobrave\suburbs\models\Suburb.php
And in this model file, I have made sure to use belongsTo and added getNameList method like below.
<?php namespace Technobrave\Suburbs\Models;
use Model;
use technobrave\states\Models\State as State;
/**
* Model
*/
class Suburb extends Model
{
/**
* #var string The database table used by the model.
*/
public $table = 'youtable_here_';
public $belongsTo = ['State' => [
'technobrave\states\Models\State',
'key' => 'state'
],
];
/**
* #var array Cache for nameList() method
*/
protected static $nameList = [];
public static function getNameList($stateId)
{
if (isset(self::$nameList[$stateId])) {
return self::$nameList[$stateId];
}
return self::$nameList[$stateId] = self::wherestate($stateId)->lists('suburb', 'id');
}
}
And it works.
Hope it will help to those who stuck on the same issue/implementation.
Thanks

Related

easyadmin crud controllers: adding value into related entity

I have question concerning easyadmin3. In my admin panel I have a productCrudController and one of the values I want to be able to set when creating a new product is the price.
For the price I have a separate table though which contains all my prices with a date. The idea being that the price of a product van change over time and my client wants to be able to have an overview of the price history for each product.
So in my productCrudController I work with an associationField to link to my prices entity. However I'm really stuck with the following practical issue: I don't want to have to add a price in a priceCrudController which I would then be able to select in my productCrudController (the way the associationField expects me to do).
What I want is that I can create a product and input a price which would then be inserted into my prices table.
My code:
productCrudController ->
Right now I have a field for prices where I can select a price in a dropdown menu, but so I have to add the price first with a priceCrudController, which really isn't practical.
class ProductsCrudController extends AbstractCrudController
{
public static function getEntityFqcn(): string
{
return Products::class;
}
public function configureFields(string $pageName): iterable
{
$image = ImageField::new('image')->setBasePath('resources/images');
$imageFile = TextField::new('imageFile')->setFormType(VichImageType::class);
$fields = [
IdField::new('id', 'ID')->hideOnForm(),
TextField::new('name'),
TextEditorField::new('description'),
AssociationField::new('category'),
AssociationField::new('plants')->setTemplatePath('list.html.twig'),
NumberField::new('stock'),
AssociationField::new('prices', 'bruto price')->onlyOnIndex()->setTemplatePath('price.html.twig'),
];
if($pageName == Crud::PAGE_INDEX || $pageName == Crud::PAGE_DETAIL){
$fields[] = $image;
} else {
$fields[] = $imageFile;
}
return $fields;
}
I tried just making a numberField for 'prices' to see if I could just enter a value that would then be persisted in the database, but I get the following error:
Object of class Doctrine\ORM\PersistentCollection could not be
converted to string
This is my 'prices' property in my 'products' entity and the methods:
/**
* #ORM\OneToMany(targetEntity=Prices::class, mappedBy="product")
* #Groups({"products:read"})
*/
private $prices;
/**
* #return Collection|Prices[]
*/
public function getPrices(): Collection
{
return $this->prices;
}
public function addPrice(Prices $price): self
{
if (!$this->prices->contains($price)) {
$this->prices[] = $price;
$price->setProduct($this);
}
return $this;
}
public function removePrice(Prices $price): self
{
if ($this->prices->removeElement($price)) {
// set the owning side to null (unless already changed)
if ($price->getProduct() === $this) {
$price->setProduct(null);
}
}
return $this;
}
I have the feeling I might need to do something with event listeners, but I don't really know how to go about it as I haven't really worked with them before.
I'd be very grateful for any help
You can create a form for the Prices entity and then use it in your product
CollectionField::new('prices')
->hideOnIndex()
->setLabel('bruto price')
->setTemplatePath('price.html.twig')
->setFormTypeOptions([
'label' => false,
'delete_empty' => true,
'by_reference' => false,
])
->setEntryIsComplex(false)
->setCustomOptions([
'allowAdd' => true,
'allowDelete' => false,
'entryType' => PricesType::class, // Your price form class here
'showEntryLabel' => false,
])
;

Laravel Form best way to store polymorphic relationship

I have a notes model. Which has a polymorphic 'noteable' method that ideally anything can use. Probably up to 5 different models such as Customers, Staff, Users etc can use.
I'm looking for the best possible solution for creating the note against these, as dynamically as possible.
At the moment, i'm adding on a query string in the routes. I.e. when viewing a customer there's an "Add Note" button like so:
route('note.create', ['customer_id' => $customer->id])
In my form then i'm checking for any query string's and adding them to the post request (in VueJS) which works.
Then in my controller i'm checking for each possible query string i.e.:
if($request->has('individual_id'))
{
$individual = Individual::findOrFail($request->individual_id_id);
// store against individual
// return note
}elseif($request->has('customer_id'))
{
$customer = Customer::findOrFail($request->customer_id);
// store against the customer
// return note
}
I'm pretty sure this is not the best way to do this. But, i cannot think of another way at the moment.
I'm sure someone else has come across this in the past too!
Thank you
In order to optimize your code, dont add too many if else in your code, say for example if you have tons of polymorphic relationship then will you add tons of if else ? will you ?,it will rapidly increase your code base.
Try instead the follwing tip.
when making a call to backend do a maping e.g
$identifier_map = [1,2,3,4];
// 1 for Customer
// 2 for Staff
// 3 for Users
// 4 for Individual
and so on
then make call to note controller with noteable_id and noteable_identifier
route('note.create', ['noteable_id' => $id, 'noteable_identifier' => $identifier_map[0]])
then on backend in your controller you can do something like
if($request->has('noteable_id') && $request->has('noteable_identifier'))
{
$noteables = [ 'Customers', 'Staff', 'Users','Individual']; // mapper for models,add more models.
$noteable_model = app('App\\'.$noteables[$request->noteable_identifier]);
$noteable_model::findOrFail($request->noteable_id);
}
so with these lines of code your can handle tons of polymorphic relationship.
Not sure about the best way but I have a similar scenario to yours and this is the code that I use.
my form actions looks like this
action="{{ route('notes.store', ['model' => 'Customer', 'id' => $customer->id]) }}"
action="{{ route('notes.store', ['model' => 'User', 'id' => $user->id]) }}"
etc..
And my controller looks this
public function store(Request $request)
{
// Build up the model string
$model = '\App\Models\\'.$request->model;
// Get the requester id
$id = $request->id;
if ($id) {
// get the parent
$parent = $model::find($id);
// validate the data and create the note
$parent->notes()->create($this->validatedData());
// redirect back to the requester
return Redirect::back()->withErrors(['msg', 'message']);
} else {
// validate the data and create the note without parent association
Note::create($this->validatedData());
// Redirect to index view
return redirect()->route('notes.index');
}
}
protected function validatedData()
{
// validate form fields
return request()->validate([
'name' => 'required|string',
'body' => 'required|min:3',
]);
}
The scenario as I understand is:
-You submit noteable_id from the create-form
-You want to remove if statements on the store function.
You could do that by sending another key in the request FROM the create_form "noteable_type". So, your store route will be
route('note.store',['noteableClass'=>'App\User','id'=>$user->id])
And on the Notes Controller:
public function store(Request $request)
{
return Note::storeData($request->noteable_type,$request->id);
}
Your Note model will look like this:
class Note extends Model
{
public function noteable()
{
return $this->morphTo();
}
public static function storeData($noteableClass,$id){
$noteableObject = $noteableClass::find($id);
$noteableObject->notes()->create([
'note' => 'test note'
]);
return $noteableObject->notes;
}
}
This works for get method on store. For post, form submission will work.
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Requests\NoteStoreRequest $request
* #return \Illuminate\Http\Response
*/
public function store(NoteStoreRequest $request) {
// REF: NoteStoreRequest does the validation
// TODO: Customize this suffix on your own
$suffix = '_id';
/**
* Resolve model class name.
*
* #param string $name
* #return string
*/
function modelNameResolver(string $name) {
// TODO: Customize this function on your own
return 'App\\Models\\'.Str::ucfirst($name);
}
foreach ($request->all() as $key => $value) {
if (Str::endsWith($key, $suffix)) {
$class = modelNameResolver(Str::beforeLast($key, $suffix));
$noteable = $class::findOrFail($value);
return $noteable->notes()->create($request->validated());
}
}
// TODO: Customize this exception response
throw new InternalServerException;
}

How to add sortable column for member list in Silverstripe admin?

I am struggling to add sort functionality on one of my member summary fields in admin.
I have extended the Silverstripe member class using:
class MyMemberExtension extends DataExtension
I have added a few fields to the default gridfield in admin:
private static $db = array(
'Organisation' => 'Varchar(100)'
);
private static $summary_fields = array(
'FirstName' => 'First Name',
'Surname' => 'Surname',
'Email' => 'Email',
'OrganisationName' => 'Organisation Name',
'LastVisited' => 'Last Visited',
'NumVisit' => 'Num Visits'
);
private static $casting = array(
'OrganisationName' => 'Varchar(100)'
);
public function getOrganisationName() {
return $this->owner->Organisation;
}
...and that all works nicely.
However, only the core fields like LastVisited are giving me sort arrows on the column headers.
I'm currently stuck as to how to implement the sort on my Organisation field. I tried adding :
public function getCMSFields()
{
$fields = parent::getCMSFields();
$grid = $fields->dataFieldByName('Organisation');
$gridConfig = $grid->getConfig();
$gridConfig->addComponent(new GridFieldSortableHeader());
return $fields;
}
public function getEditForm($id = null, $fields = null) {
$form=parent::getEditForm($id, $fields);
$model = singleton($this->modelClass);
// add sorting if we have a field for...
if (class_exists('GridFieldSortableRows')
&& $model->hasField('Organisation')
&& $gridField=$form->Fields()->dataFieldByName($this->sanitiseClassName($this->modelClass))) {
if($gridField instanceof GridField) {
$gridField->getConfig()->addComponent(new GridFieldSortableRows('Organisation'));
}
}
return $form;
}
...to my class, but I'm not convinced these are even being called, as even if I just return null from these two functions nothing changes.
I have found a few answers that deal with extensions to ModelAdmin, but not for the core Member list. Thanks!
First of all, I'm not sure why you chose to have a getter named OrganisationName, where you could just as well use Organisation directly? That being said, I think your question is valid and might apply to different scenarios and/or field-types.
The Form-field that is being used to edit members is the Members GridField within SecurityAdmin. Luckily, there's an extension hook (updateEditForm) to modify the form fields of SecurityAdmin.
So in order to modify the sorting of the Members GridField, create an Extension like the following:
<?php
class MemberAdminExtension extends Extension
{
public function updateEditForm(Form $form)
{
/** #var GridField $memberGridField */
if ($memberGridField = $form->Fields()->dataFieldByName('Members')) {
/** #var GridFieldSortableHeader $sortHeader */
if ($sortHeader = $memberGridField->getConfig()->getComponentByType('GridFieldSortableHeader')) {
// Map OrganisationName to the Organisation field
$sortHeader->setFieldSorting([
'OrganisationName' => 'Organisation'
]);
}
}
}
}
And apply the extension via config to SecurityAdmin:
# Within _config/config.yml
SecurityAdmin:
extensions:
- MemberAdminExtension
After a dev/build your Member table should be sortable by Organisation Name as well…

Laravel Eloquent validation insert exception?

I've created a form which adds a category of product in a Categories table (for example Sugar Products or Beer), and each user has their own category names.
The Categories table has the columns id, category_name, userId, created_At, updated_At.
I've made the validation and every thing is okay. But now I want every user to have a unique category_name. I've created this in phpMyAdmin and made a unique index on (category_name and userId).
So my question is this: when completing the form and let us say that you forgot and enter a category twice... this category exist in the database, and eloquent throws me an error. I want just like in the validation when there is error to redirect me to in my case /dash/warehouse and says dude you are trying to enter one category twice ... please consider it again ... or whatever. I am new in laravel and php, sorry for my language but is important to me to know why is this happens and how i solve this. Look at my controller if you need something more i will give it to you.
class ErpController extends Controller{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return view('pages.erp.dash');
}
public function getWarehouse()
{
$welcome = Auth::user()->fName . ' ' . Auth::user()->lName;
$groups = Group::where('userId',Auth::user()->id)->get();
return view('pages.erp.warehouse', compact('welcome','groups'));
}
public function postWarehouse(Request $request)
{
$input = \Input::all();
$rules = array(
'masterCategory' => 'required|min:3|max:80'
);
$v = \Validator::make($input, $rules);
if ($v->passes()) {
$group = new Group;
$group->group = $input['masterCategory'];
$group->userId = Auth::user()->id;
$group->save();
return redirect('dash/warehouse');
} else {
return redirect('dash/warehouse')->withInput()->withErrors($v);
}
}
}
You can make a rule like this:
$rules = array(
'category_name' => 'unique:categories,category_name'
);

How to dynamically populate list box from database?

How to dynamically populate list box from database?
Please Provide some examples in zend framework.
Say if you have a table called Test and a corresponding model for it named Test,
class Test extends Zend_Db_Table {
protected $_name = "Test";
function getLisItems() {
$select = $this->getAdapter()->select()->from($this->_name, array(
'key' => 'column1_key',
'value' => 'column2_value'
));
return $this->getAdapter()->fetchPairs($select);
}
}
In your controller action or view, have the following code.
$t = new Test();
$list = new Zend_Form_Element_Select('list');
$list->setLabel('Select your item: ')
->addMultiOptions($t->getLisItems());
Now your list box will be populated with items from the database.

Categories