Yii2 Kartik-v Editable Column update multiple table cells - php

I set up editable column for the GridView in Yii2 with Kartik Editable extension. The problem I am facing is that I cannot find a way to update multiple table cell from one editable column.
The things I did:
GridView column
[
'class' => 'kartik\grid\EditableColumn',
'attribute'=>'post_title',
'editableOptions'=> function ($model, $key, $index) {
return [
'inputType' => \kartik\editable\Editable::INPUT_TEXT,
'size'=>'sm',
'afterInput'=>function ($form, $widget) use ($model, $index) {
return $form->field($model, 'post_description')->textInput(['placeholder'=>'Enter post title']);
}
];
}
],
By clicking edit post title column it shows edit fields for title and description
PostsController action
public function actionIndex()
{
$searchModel = new PostsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
if (Yii::$app->request->post('hasEditable')) {
$postId = Yii::$app->request->post('editableKey');
$model = Posts::findOne($postId);
$out = Json::encode(['output'=>'', 'message'=>'']);
$post = [];
$posted = current($_POST['Posts']);
$post['Posts'] = $posted;
if ($model->load($post)) {
$output = '';
$out = Json::encode(['output'=>$output, 'message'=>'']);
$model->save();
}
echo $out;
return;
}
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
So, when I edit post title and description only post title is saved into database.
I think it is because current saves only one value
$posted = current($_POST['Posts']);
What is the proper way to save both
$model->post_title and $model->post_description ?

This is an Ajax editable column. At a time only one value would be sent to the controller.
So the class of post_title and post_description column must be editable in the view.
This should work everytime you edit those columns.
Also change this
if ($model->load($post)) {
if (isset($model->post_title)){
// here you can format the value of the attribute
and display on the screen
$output = $model->post_title;
}
if (isset($model->post_description)){
$output = $model->post_description;
}
$model->save();
// Here you send a message that the value has been saved
$out = Json::encode(['output'=>$output, 'message'=>'Saved']);
}

Related

Yii2 first and last items in the ListView

I have a typical ListView widget in a view as follows:
public function actionView($id)
{
$model = $this->findModel($id);
$dataProvider = new \yii\data\ActiveDataProvider(['query' => \app\models\Verses::find()->where(['sura_id' =>$id])->with('sura')]);
return $this->render('view', [
'model' => $model,
'dataProvider' => $dataProvider,
]);
}
In the view a list of Verses model is rendered and the pager of the ListView is available too.
I want to add some details to the page title. Those details are data from the first and last records of the Verses model rendered in the list.
I have tried to use min() and max() PHP functions to get the first and the last records in the view as follows:
$min = min($model->verses);
echo $min->verse_id;
However, it returns the ultimate first record regarding-less the current pager page. In other words, it does not return the verse_id value of the first item of the list.
Is there any way to get the first, last or even a specific nth item of the ListView?
You should simply try :
$models = $dataProvider->getModels();
$first = reset($models);
$last = end($models);
Read more about reset and end.
You could access to the models array of dataProvider a get the firts and the last
public function actionView($id)
{
$model = $this->findModel($id);
$dataProvider = new \yii\data\ActiveDataProvider(['query' => \app\models\Verses::find()->where(['sura_id' =>$id])->with('sura')]);
$first = $dataProvider['models'][0];
$last = $dataProvider['models'][$dataProvide['totalCount']-1];
or you can use
$myModels = $dataProvider->gestModels();
$first = myModels[0];
$last = myModels[$dataProvide['totalCount']-1];
and for the page you can use count instead of total count
return $this->render('view', [
'model' => $model,
'dataProvider' => $dataProvider,
]);
}

Yii2 checkboxlist broken down into categories (nested sets)

I seem to be having some trouble creating a form input that allows checkboxlists and nested sets to work together.
What I'd like, is something exactly like what bookbub does:
http://i.imgur.com/PfpgSf5.jpg
Right now in my database I have it structured as follows:
Category table
- id
- name
- parent_id
Basically, my idea is to display everything on the _form that has parent_id as null as a heading (no checkbox) and everything that has a parent_id as a checkbox under the appropriate heading.
However, the only solution that I can get that's close doesn't seem to allow me to have checkboxes already checked if we're updating a user's preferences. It does however display things exactly how I would like. Here's what I have so far:
ProfileReader's (ProfileReader is my model that extends users to hold their preferences) _form:
<?php
$primaryCategories = (new Category)->getPrimaryCategories();
//$selectedCategories = yii\helpers\ArrayHelper::map($model->categories, 'id', 'name');
foreach ($primaryCategories as $pc) {
echo '<p>'.$pc->name.'</p>';
if ($pc->subCategories) {
//the following never fully worked. It doesn't automatically check the boxes for relations that
//are already setup. You need to somehow use $model->categories[#] and 'id' for that to work
//echo $form->field($model->categories[#], 'id')->label(false)
echo $form->field($pc, 'subCategories[' . $pc->id . '][]')->label(false)
->checkboxList(yii\helpers\ArrayHelper::map($pc->subCategories, 'id', 'name'),
['separator' => '<p>']);
}
}
?>
ProfileReaderController:
public function actionUpdate()
{
$model = $this->findModel(\Yii::$app->user->identity->id);
if ($model == null) {
$model = new ProfileReader();
$model->user_id = \Yii::$app->user->identity->id;
}
if ($model->load(Yii::$app->request->post()) && $model->save()) {
//link the categories to the pen name
$categories = Yii::$app->request->post()['Category']['subCategories'];
$model->unlinkAll('categories', true);
foreach ($categories as $category) {
if ($category)
foreach ($category as $c) {
$c = (new Category)->findOne($c);
$model->link('categories', $c);
}
}
return $this->redirect(['update']);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
ProfileReader:
public function getCategories()
{
return $this->hasMany(Category::className(), ['id' => 'category_id'])
->viaTable('user_category', ['user_id' => 'user_id']);
}
Does anyone have any clue how I can make this work? Is it even possible in Yii2 with activeform?
Okay, after many hours I finally figured it out. Posting my resulting code here so that it may help someone else. Don't ask me to explain it as I don't fully get it myself :P
It might also be a good idea to do some testing on it too before throwing it into a live environment, I haven't done any yet.
update action:
/**
* Updates an existing ProfileReader model.
* If update is successful, the browser will be redirected to the 'view' page.
* #param integer $id
* #return mixed
*/
public function actionUpdate()
{
$model = $this->findModel(\Yii::$app->user->identity->id);
if ($model == null) {
$model = new ProfileReader();
$model->user_id = \Yii::$app->user->identity->id;
}
if ($model->load(Yii::$app->request->post()) && $model->save()) {
//unlink the categories first to avoid duplicates
$model->unlinkAll('categories', true);
//link the categories to the pen name
foreach ($model->categoriesArray as $pc) {
if ($pc) {
foreach ($pc as $sc) {
$sc = (new Category)->findOne($sc);
$model->link('categories', $sc);
}
}
}
return $this->redirect(['update']);
} else {
//get the categories and separate them into groups based on parent_id
foreach ($model->categories as $c) {
$model->categoriesArray[$c->parent_id][] = $c;
}
return $this->render('update', [
'model' => $model,
]);
}
}
ProfileReader model (had to add a variable):
public $categoriesArray;
_form:
<label class="control-label">Categories</label>
<?php
$allCategories = (new Category)->getOrderedCategories();
foreach ($allCategories as $pc) {
echo '<p>'.$pc['name'].'</p>';
echo $form->field($model, 'categoriesArray['.$pc['id'].'][]')->label(false)
->checkboxList(yii\helpers\ArrayHelper::map($pc['subCategories'], 'id', 'name'),
['separator' => '<p>']);
}
?>

Grid Field not showing entries [SilverStripe]

I am using the MultiForm module to submit a long form with SilverStripe. The logic for this form is in 'CampaignBriefForm.php' whereas the gridfield CMS field is being added in 'CampaignBriefPage.php'. I have a Data Object for a CampaignBriefLead which is what the form creates.
Campaign Brief Page
private static $has_many = array(
'CampaignBriefLeads' => 'CampaignBriefLead'
);
public function CampaignBriefForm() {
return new CampaignBriefForm($this, 'CampaignBriefForm');
}
Campaign Brief Lead (DO)
private static $has_one = array( "Page" => "CampaignBriefPage" );
As you can see the Campaign Brief page has the correct relationship with the Data Object and also you can see the the form itself (done in a sepearate file) is correctly returning (as it's being saved in the DB). For some reason however, the gridfield will not show me what is in the database for that Data Object. The grid field code is as follows.
$fields = parent::getCMSFields();
$contactConfig = GridFieldConfig_RelationEditor::create();
$contactConfig->getComponentByType('GridFieldDataColumns')->setDisplayFields(
array(
'CompanyName' => 'Company Name',
'StartDate' => 'Start Date',
'Duration' => 'Duration',
'WebsiteURL' => 'Website',
'Budget' => 'Budget'
));
$contactGrid = new GridField(
'CampaignBrief',
'Campaign Enquiries',
$this->CampaignBriefLeads(),
$contactConfig
);
$fields->addFieldToTab("Root.Enquiries", $contactGrid);
To me this all looks correct and should work but for some reason it is not working.
Note
The link existing option on the gridfield allows me to link one of the entries from the DO with the gridfield weirdly?? So it saves one entry but I have to do it manually, this tells me it can see the DB but won't pull for some reason.
For reviewing reasons, here is the code for the multiform where the campaign brief lead is actually saved to the DB after the form is submitted.
public function finish($data, $form) {
parent::finish($data, $form);
$steps = DataObject::get(
'MultiFormStep',
"SessionID = {$this->session->ID}"
);
$enquiry = new CampaignBriefLead();
foreach($steps as $step) {
$data = $step->loadData();
foreach($data as $key => $value) {
if($key == 'url' || $key == 'MultiFormSessionID' || $key == 'action_finish') {
continue;
}
if(isset($data[$key])) {
$enquiry->$key = $data[$key];
error_log($data[$key]);
}
}
}
$enquiry->write();
$this->controller->redirect('/campaign-brief/');
}
If you need anything more let me know. Thanks.
I would take a guess that the CampaignBriefLead PageID is not being set on your form submission.
Check the CampaignBriefLead table in your database and check the PageID column. If it is blank, null or 0 for each row then it is not being set.
One way to fix this problem for any new submission is to set the PageID for the $enquiry:
public function finish($data, $form) {
// ...
$enquiry = new CampaignBriefLead();
if ($campaignBriefPage = CampaignBriefPage::get()->first()) {
$enquiry->PageID = $campaignBriefPage->ID;
}
// ...
}
For the existing entries you will need to update the entries to have the correct PageID.

Yii2 display data using for each loop

Using yii2 I have created a Model and CRUD using gii.
I want to use a foreach or while loop in my VIEW to display the data in the following format
For each row in database table
echo("addMarker($lat_field, $lon_field);\n");
I have an index page which is rendered using the following controller action.
public function actionIndex()
{
$this->layout = 'directory';
$searchModel = new ShopDirectorySearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
I can use the following to display the data using listview which displays all the data/rows within database however it has html around it and obviously isn't outputted in the format I wish it to be.
<?= ListView::widget([
'dataProvider' => $dataProvider,
'itemOptions' => ['class' => 'col-xs-6 col-sm-3'],
'itemView' => '_index',
]);?>
No need to use ListView here, you should simply try :
foreach ($dataProvider->models as $model) {
echo "addMarker({$model->lat_field}, {$model->lon_field});";
}
If you really want to use ListView, you could simply edit _index view file.
if(!empty($st_data))
{
foreach($st_data as $row)
{
echo 'Country Name: '.$row['country_name'].'</br>';
echo 'State Name: '.$row['state_name'].'</br>';
echo 'City Name: '.$row['city_name'].'</br>';
echo '</br>';
}
exit;
}
$rows = ShopDirectory::findAll();
if(!empty($rows))
{
foreach($rows as $row)
{
$lat = $row->lat;
$lon = $row->lon;
$this->view->registerJs('addmarker("'.$lat.'", "'.$lon.'"."\n");', yii\web\View::POS_END);
...
}
}
http://www.yiiframework.com/forum/index.php/topic/61940-simple-while-loop-to-list-all-rows/page__view__findpost__p__274731

Yii2: Using Kartik Depdrop Widget?

Ok I am trying to use the Kartik Depdrop widget, all I am getting a white drop-down list that is values not showing in the dependent drop-down list.
I have a state model and a city model and I have it setup like this.
In _form.php
$catList=ArrayHelper::map(app\models\State::find()->all(), 'id', 'state_name' );
echo $form->field($model, 'state')->dropDownList($catList, ['id'=>'state_name']);
echo $form->field($model, 'district_city')->widget(DepDrop::classname(), [
'options'=>['id'=>'district_city'],
'pluginOptions'=>[
'depends'=>['state_name'], // the id for cat attribute
'placeholder'=>'Select...',
'url'=> \yii\helpers\Url::to(['patient-entry/subcat'])
]
]);
?>
Then in model
public static function getCity($city_id) {
$data=\app\models\City::find()
->where(['state_name'=>$city_id])
->select(['id','city_name'])->asArray()->all();
return $data;
}
Then in my controller
public function actionSubcat() {
$out = [];
if (isset($_POST['depdrop_parents'])) {
$parents = $_POST['depdrop_parents'];
if ($parents != null) {
$cat_id = $parents[0];
$out = \app\models\PatientEntry::getCity($cat_id);
echo Json::encode(['output'=>$out, 'selected'=>'']);
return;
}
}
echo Json::encode(['output'=>'', 'selected'=>'']);
}
When I select the state field, the firebug console shows the data correctly like:
{"output":[{"id":"172","city_name":"Along"},{"id":"173","city_name":"Bomdila"},{"id":"174","city_name":"Itanagar"},{"id":"175","city_name":"Naharlagun"},{"id":"176","city_name":"Pasighat"}],"selected":""}
The city field drop-down is also shows as if it has been filled up with data, but only with white-spaces.
What I am doing wrong here?
Thanks.
Ok I found the solution, All the code is ok, actually the depdrop widget looks for the pair id and name like:
// the getSubCatList function will query the database based on the
// cat_id and return an array like below:
// [
// ['id'=>'<sub-cat-id-1>', 'name'=>'<sub-cat-name1>'],
// ['id'=>'<sub-cat_id_2>', 'name'=>'<sub-cat-name2>']
// ]
Accordingly I have changed the code in the model
->select(['id','city_name'])->asArray()->all();
with
->select(['id','city_name AS name'])->asArray()->all();
That's all and it is working fine now. Hope someone will find this useful.
Instead of changing the Select statement you could also:
echo $form->field($model, 'district_city')->widget(DepDrop::classname(), [
'options'=>['id'=>'district_city'],
'pluginOptions'=>[
//// change default 'nameParam'=>'name' to
'nameParam'=>'city_name',
'depends'=>['state_name'], // the id for cat attribute
'placeholder'=>'Select...',
'url'=> \yii\helpers\Url::to(['patient-entry/subcat'])
]
]);
?>
change the 'nameParam' to 'city_name'

Categories