how to integrate multimodelform with echmultiselect yii? - yii

I am running into a little snag with combining extensions "EchMultiSelect" and "MultiModelForm" of YII framework.
What I'm trying to do is copy a set of form elements one of which elements is an EchMultiSelect widget.
According to tutorial on the jqRelCopy page, I would need to pass a copy of the element (datePicker in their example) to the 'jsAfterNewId' option:
'jsAfterNewId' => JQRelcopy::afterNewIdDatePicker($datePickerConfig),
So, I tried to modify that to:
'jsAfterNewId' => MultiModelForm::afterNewIdMultiSelect($memberFormConfig['elements']),
Where I also added the following to MultiModelForm.php:
public static function afterNewIdMultiSelect($element)
{
$options = isset($element['options']) ? $element['options'] : array();
$jsOptions = CJavaScript::encode($options);
return "if(this.attr('multiple')=='multiple'){this.multiselect(jQuery.extend({$jsOptions}));};";
}
its copied and working properly when i am using Add Person link but what happens if i am adding/cloning three items for example and when i change the third item multiselct option then its reflecting to the first multiselect dropdown only this is same for other as well also when i am adding new items by clicking on the Add Person link then its cloning the same element to the new row item
here is the code for the form configuration variables and multimodel widget call.
//$userList=array of the userIds from users table
$memberFormConfig = array(
'elements'=>array(
'userId'=>array(
'type'=>'ext.EchMultiSelect.EchMultiSelect',
'model' => $User,
'dropDownAttribute' => 'userId',
'data' => $userList,
'dropDownHtmlOptions'=> array(
'style'=>'width:500px;',
),
),
...
...
));
calling the MultiModelForm widget from the same view file
$this->widget('ext.multimodelform.MultiModelForm',array(
'id' => 'id_member', //the unique widget id
'formConfig' => $memberFormConfig, //the form configuration array
'model' => $model, //instance of the form model
'tableView' => true,
'validatedItems' => $validatedMembers,
'data' => Person::model()->findAll('userId=:userId', array(':userId'=>$model->id)),
'addItemText' => 'Add Person',
'showAddItemOnError' => false, //not allow add items when in validation error mode (default = true)
'fieldsetWrapper' => array('tag' => 'div',
'htmlOptions' => array('class' => 'view','style'=>'position:relative;background:#EFEFEF;')
),
'removeLinkWrapper' => array('tag' => 'div',
'htmlOptions' => array('style'=>'position:absolute; top:1em; right:1em;')
),
'jsAfterNewId' => MultiModelForm::afterNewIdMultiSelect($memberFormConfig['elements']),
));
Can someone help me with this please?
Thanks in advance!

After a long searching and googleing i found the solution for this, just replace the function in your MultiModelForm.php:
public static function afterNewIdMultiSelect($element)
{
$options = isset($element['options']) ? $element['options'] : array();
$jsOptions = CJavaScript::encode($options);
return "if ( this.hasClass('test123456') )
{
var mmfComboBoxParent = this.parent();
// cloning autocomplete and select elements (without data and events)
var mmfComboBoxClone = this.clone();
var mmfComboSelectClone = this.prev().clone();
// removing old combobox
mmfComboBoxParent.empty();
// addind new cloden elements ()
mmfComboBoxParent.append(mmfComboSelectClone);
mmfComboBoxParent.append(mmfComboBoxClone);
// re-init autocomplete with default options
mmfComboBoxClone.multiselect(jQuery.extend({$jsOptions}));
}";
}
Thats It....!!
Thanks...!!!

Related

Yii 2.0 Select Pre-selected values from Database

I have been trying to fix an issue but to no avail but i am sure i will find a solution here. I am using Kartik 2.0 Select extension to do a multiple select. Fine, all working when inserting into the database but i am unable to retrieve the saved records to be displayed as selected back in the select field.
//I have included the kartik widgets already
use kartik\widgets\Select2;
<label>Desired Specialization(s)</label>
<?= $form->field($spec, 'id')->label(false)->widget(Select2::classname(), [
'data' => $model->getAllSpecializations(),
'options' => ['placeholder' => 'You can choose more than one specialization ...'],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true
],
]);
?>
</div>
Please, your reply will be appreciated. Thanks
I think you need to add the saved values as the initial data? Like so:
'value' => $savedDataArray, // initial value
http://demos.krajee.com/widget-details/select2#usage-tags
After much digging into the code, i found a way on how to display selected database values into a multi-select option using Yii Select2
My Model
public function getCandidateLanguage()
{
$langValues = (new \yii\db\Query())
->select('c.language_id AS id, l.lang_name')
->from('candidate_language c ')
->innerJoin('languages l','c.language_id = l.id')
->where('c.candidate_id='.$this->candidate_id)
->orderBy('c.language_id')
->all();
return \yii\helpers\ArrayHelper::map($langValues,'id','lang_name');
}
My View
use kartik\widgets\Select2;
<?php
//the line below is to fetch the array key of $model->getCandidateLanguage() array
$lang->id = array_keys($model->getCandidateLanguage()); // value to initialize
echo Select2::widget([
'model' => $lang,
'attribute' => 'id',
'data' => $model->getAllLanguages(),
'options' => ['placeholder' => 'Choose multiple languages'],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true,
'tags' => true,
],
]);
?>
Hope it help someone who is facing the same issue.

Visibility of a button in cgridview based on the equality of two models fields

i have this array that represent the data for a button in a CGridView widget in YII.
array(
'Button' =>
array(
'imageUrl'=>Yii::app()->request->baseUrl.'/images/image.png',
'url'=>'Yii::app()->createUrl("controller/action", array("column"=>$data->column))',
'visibile'=>'$visibile',
))
I need $visibile to be true or false based on this function:
if (array_key_exists(0 , Table::model()->findAllByAttributes(array('column' => $model->column)))){
$visible = true;
}
else {
$visibile = null;
}
where Table::model is not the same model of the grid.
How can i modify the visibility of the button based on the equality of the value in the record of this model and the model of the view?
I hope my question was clear, thanks everyone!
You could do this in Yii
array(
'Button' =>
array(
'imageUrl'=>Yii::app()->request->baseUrl.'/images/image.png',
'url'=>'Yii::app()->createUrl("controller/action", array("column"=>$data->column))',
'visibile'=>function($index, $data) {
// the logic, where $data is the model for that row
// ...
// return true(visible) or false(invisible)
},
))
Following might work for you:
array(
'Button' =>
array(
'imageUrl'=>Yii::app()->request->baseUrl.'/images/image.png',
'url'=>'Yii::app()->createUrl("controller/action", array("column"=>$data->column))',
'visibile'=>"return array_key_exists(0 , Table::model()->findAllByAttributes(array('column' => $model->column)));",
))

Problems returning result of CDbCriteria based query

I have a query as follows
$criteria1 = new CDbCriteria();
$criteria1->condition = 'id = 1';
$modelA=Table1::model()->find($criteria1);
I can pass it to a view and return the title and entry
$this->widget('bootstrap.widgets.TbBox', array(
title' => $modelA['title'],
'content' => $modelA['entry'] ));
Now I'd like to return a range of entries
$criteria2 = new CDbCriteria();
$criteria2->condition = 'id > 7';
$modelB=Table1::model()->findAll($criteria2);
(btw : I'm following a form as laid out here). I was expecting to be able to read the resulting array of values out as below, but ['title'] is now being seen as a undefined index (obviously I'm expecting to read this out in a loop but you get the point)
$this->widget('bootstrap.widgets.TbBox', array(
'title' => $modelB['title'][0],
'content' => $modelB['entry'][0]));
Where am I going wrong?
Thanks
No, the indexes should be specified in the different order: the number of a specific element first, then the name of the property. Additionally, it's better (=cleaner) to name the result of findAll so it'll show you (and any other reader) that it's a collection, not a single model:
$models = Table1::model()->findAll($criteria2);
// ...
$this->widget('bootstrap.widgets.TbBox', array(
'title' => $models[0]['title']
//...
));
But even that's not necessary if you use foreach (and you probably will):
foreach ($models as $model):
// ...
$this->widget('some.name', array(
'title' => $model['title']
);
endforeach;

Drupal module's list of permissions are stuck

From what I understand, my problem is that I am (rather, my client is) running an older version of Drupal, specifically Core 6.26 If you're curious about any additional specs, I'll be happy to divulge.
With that out of the way, I made a new module with the following menu hook.
function checkin_menu(){
$items = array();
$items['checkin'] = array(
'title' => 'Checkin'
,'type' => MENU_CALLBACK
,'access arguments' => array('checkin')
,'page callback' => 'checkin'
);
}
The permissions listed out exactly what I expected. There was a section called "Checkin" the same as the module's name as specified in the .info file, and one item to give permissions to "checkin"
Later on I expanded the module to have two different paths. The second one is supposed to be for admins only.
function checkin_menu(){
$items = array();
$items['checkin'] = array(
'title' => 'Checkin'
,'type' => MENU_CALLBACK
,'access arguments' => array('create a checkin')
,'page callback' => 'checkin'
);
$items['checkin_admin'] = array(
'title' => 'Checkin Admin'
,'type' => MENU_CALLBACK
,'access arguments' => array('view all checkins')
,'page callback' => 'device_checkin_page'
);
return $items;
}
Much to my surprise neither "create a checkin" or "view all checkins" is showing up. I still have the original "checkins" showing on the permissions page. I've been hunting for answers for a couple days now. Help a guy out?
Permissions are defined by a different hook, which is hook_perm.
So you should be doing something like this:
/**
* Implementation of the hook_perm()
*/
function checkin_perm() {
return array (
'create a checkin',
'view all checkins',
);
}

Yii: in a view _view, ho to add a related grid [sortable and searchable]?

I've Client [1->N] Delivery
In _view of Client I want Delivery related to my client
This is in my ClientController
public function actionView($id)
{
$client = $this->loadModel($id);
$delivery_provider = new CActiveDataProvider(
'Delivery',
array (
'criteria' => array (
'condition' => 'client_id = :c_id',
'params' => array (':c_id' => $client->id),
), // fine dei criteri
) // fine array di definizione cactiveprovider
); // fine del CActive provider
$this->render('view',array(
'model'=> $client,
'delivery_provider' => $delivery_provider,
));
}
Then modules/admin/views/client/_view.php I add my CGridView. ... but ... it's not searchable and not sortable (but pagination works...)
How to proceed ?
Since Delivery is a model it's better to use the CActiveRecord::search() instead. This method is automatically generated for you if you used Gii.
For searching you have to capture the results of the search form / filters using $this->setAttributes($_GET['Delivery']); assuming your inputs have names of the form Delivery[attribute_name].
public function actionView($id){
$client = $this->loadModel($id);
$delivery = new Delivery('search');
if(isset($_GET['Delivery']))
$delivery->setAttributes($_GET['Delivery']);
$delivery->client_id=$id;
$this->render('view',array(
'model'=> $client,
'delivery_provider' => $delivery->search(),
));
}