How to pop up the msg box after deleting from cgridview? - yii

This is the CButtonColumn of my gridview
array(
'header' => 'Action',
'class' => 'CButtonColumn',
'template' => '{view} {delete}',
'buttons' => array(
'delete' =>
array(
'url' => 'Yii::app()->controller->createUrl("/workorders/delete",array("id"=>$data->primaryKey))',
'label' => 'delete',
'options' => array( // this is the 'html' array but we specify the 'ajax' element
'confirm' => $alert,
'class' => 'grid_action_set1',
'ajax' => array(
'type' => 'POST',
'url' => "js:$(this).attr('href')", // ajax post will use 'url' specified above
'success' => 'function(data){
if(data == "true"){
//update the grid...
$.fn.yiiGridView.update("workorders-grid");
return false;
}else{
$("#mydialog").dialog("open"); return false;
return false;
}
}',
),
),
),
'view' => array
(
'label'=>'View workorder detail.',
'url'=>'Yii::app()->createUrl("/workorders/view", array("id"=>$data->id))',
),
),
'htmlOptions' => array('width' => '30px', 'style'=>'text-align:center;')
),
This is the check the record before delete
protected function beforeDelete()
{
if ($this->exists("(status_id = 6 OR status_id = 7) AND parent_mac = '$this->mac_address'"))
return false;
else
return parent::beforeSave(); // prevent actual DELETE query from being run
}
This is my controller
public function actionDelete($id)
{
if($this->loadModel($id)->delete())
{
if(!isset($_GET['ajax']))
Yii::app()->user->setFlash('success','Normal - Deleted Successfully');
else
echo "<div class='flash-success'>Ajax - Deleted Successfully</div>";
$data = 'true';
}else{
if(!isset($_GET['ajax']))
Yii::app()->user->setFlash('error','This node is a parents of some other node. <br/>Please replace.');
else
echo "<div class='flash-error'>This node is a parents of some other node. <br/>Please replace.</div>";
$data = 'false';
}
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}
My problem is when the user click the delete button, the confirmation box "Are you sure to delete?" will come out. When user click "Yes", check the record is valid to delete.
If the record is valid to delete, delete the record and "successfully" Msg box pop up. update gridview.
If the record is not valid to delete, "the record is blah blah blah" Msg box pop up.
How can I do?

public function actionDelete($id) {
$jenis = Usaha::model()->findByAttributes(array('id_jenis' => $id));
if($jenis==''){
$this->loadModel($id)->delete();
}
else {
throw new CHttpException(400, "your message alert "); //try this , will show some alert this i used when some data connected with another table in database
}
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if (!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}

Related

Prestashop: How to filter in HelperList

Hello I try to filter a List in the Backoffice. It shows the filter, it also saves it after clicking on search, but nothing is happening. Same with pagination.
$schueler = $this->getAllSchuelerbyDiplom($id_diplom);
$diplom_name = $this->getDiplomNamebyID($id_diplom);
$fields_list = array(
'id_schueler' => array(
'title' => 'ID',
'align' => 'center',
'class' => 'fixed-width-xs',
'search' => true),
'customer_name' => array(
'title' => $this->l('ID Customer')),
'id_gruppe' => array(
'title' => $this->l('ID Gruppe')),
'name' => array(
'title' => $this->l('Name'),
'filter_key' => 'name'.$diplom_name),
'vorname' => array(
'title' => $this->l('Vorname')),
'punkte' => array(
'title' => $this->l('Punkte')),
'bestanden' => array(
'title' => $this->l('Bestanden'),
'active' => 'toggle',
'class' => 'fixed-width-xs',
'type' => 'bool'),
'date_added' => array(
'title' => $this->l('Datum'),
'class' => 'fixed-width-xs',
'type' => 'date'),
);
$helper = new HelperList();
$helper->table = 'no-idea-what-this-is-for';
$helper->title = $diplom_name;
$helper->shopLinkType = '';
$helper->actions = array('view', 'edit', 'delete');
$helper->listTotal = count($schueler);
$helper->identifier = 'id_schueler';
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false) . '&configure=' . $this->name .'&diplom_name=' . $diplom_name;
return $helper->generateList($schueler, $fields_list);
What is wrong with my code? What is $helper->table for? I tried different things there, but nothing helps...
EDIT
public function getAllSchuelerbyDiplom($id_diplom) {
$query = new DbQuery();
$query->select('s.*, CONCAT(c.firstname,\' \',c.lastname) AS customer_name');
$query->from($this->table_name.'_schueler', 's');
$query->leftJoin('customer', 'c', 's.id_customer = c.id_customer');
$query->where('s.id_diplom = ' . (int)$id_diplom);
return Db::getInstance()->ExecuteS($query);
}
The problem is that HelperList object itself will only display filters being used but won't filter the actual data list for you.
In your $this->getAllSchuelerbyDiplom($id_diplom); method I assume you execute SQL query to retrieve all rows, however you need modify it to account for any filters, paginations (page number and rows per page) or ordering. You have to check GET/POST values or values set in cookie to detect them.
$helper->table sets the table name and list actions are prepended with that name so they are distinct from any other lists you might have on the page.
Edit:
Example of setting pagination and page
public function getPage()
{
// $tableName must be equal to what you set in $helper->table
// Check if page number was selected and return it
if (Tools::getIsset('submitFilter'.$tableName)) {
return (int)Tools::getValue('submitFilter'.$tableName);
}
else {
// Check if last selected page is stored in cookie and return it
if (isset($this->context->cookie->{'submitFilter'.$tableName})) {
return (int)$this->context->cookie->{'submitFilter'.$tableName};
}
else {
// Page was not set so we return 1
return 1;
}
}
}
public function getRowsPerPage()
{
// $tableName must be equal to what you set in $helper->table
// Check if number of rows was selected and return it
if (Tools::getIsset($tableName. '_pagination')) {
return (int)Tools::getValue($tableName. '_pagination');
}
else {
// Check if number of rows is stored in cookie and return it
if (isset($this->context->cookie->{$tableName. '_pagination'})) {
return (int)$this->context->cookie->{$tableName. '_pagination'};
}
else {
// Return 20 rows per page as default
return 20;
}
}
}
public function getAllSchuelerbyDiplom($id_diplom) {
$query = new DbQuery();
$query->select('s.*, CONCAT(c.firstname,\' \',c.lastname) AS customer_name');
$query->from($this->table_name.'_schueler', 's');
$query->leftJoin('customer', 'c', 's.id_customer = c.id_customer');
$query->where('s.id_diplom = ' . (int)$id_diplom);
// Limit the result based on page number and rows per page
$query->limit($this->getRowsPerPage(), ($this->getPage() - 1) * $this->getRowsPerPage());
return Db::getInstance()->ExecuteS($query);
}
You can place a p(Tools::getAllValues()); d($this->context->cookie->getAll(); after you generate a list then set a filter in your list and it will show you all the variables you need to create filters and ordering.

how to show Alert with TbAlert yii when delete data

i want to display the error with tbalert in admin page, the alert show when data is duplicate when user click delete in gridview, to check duplicate i used function beforedelete in model, but why does the alert not show in admin page ?
//code in model
public function beforeDelete()
{
$criteria = new CDbCriteria();
$criteria->condition = 'idnasabah=:idnasabah';
$criteria->params = array(':idnasabah'=>$this->idnasabah);
if (Tbpinjaman::model()->exists($criteria)) {
Yii::app()->user->setFlash(
'error',
'<strong>Ooops..!</strong> Data tidak dapat dihapus karena relasi dengan tabel pinjaman.'
);
return false;
}
else {
return parent::beforeDelete();
}
}
//code in admin page
<?php
/* #var $this TbnasabahController */
/* #var $model Tbnasabah */
$this->widget('bootstrap.widgets.TbAlert', array(
'block' => true,
'fade' => true,
'closeText' => '×', // false equals no close link
'events' => array(),
'htmlOptions' => array(),
'userComponentId' => 'user',
'alerts' => array( // configurations per alert type
// success, info, warning, error or danger
'success' => array('closeText' => '×'),
'info', // you don't need to specify full config
'warning' => array('block' => false, 'closeText' => false),
'error' => array('block' => false)
),
));
$this->widget('zii.widgets.grid.CGridView', array(
/*'type'=>'striped bordered condensed',*/
'htmlOptions'=>array('class'=>'table table-striped table-bordered table-condensed'),
'id'=>'tbregister-grid',
'filter' => $model,
'dataProvider'=>$model->search(),
'template'=>'{items}{pager}',
'columns'=>array(
array(
'header'=>'',
'headerHtmlOptions'=>array('style'=>'text-align:center;color:blue'),
'htmlOptions'=>array('style'=>'text-align:center'),
'value'=>'$this->grid->dataProvider->pagination->currentPage*$this->grid->dataProvider->pagination->pageSize + $row+1',
),

Yii select2 - returned data cannot be selected

I have been looking into select2 and yii and have managed to load data via json request/response.
The issue I'm faced with is when I try to select an entry of the returned data, I can not.
Where am I going wrong ? The data returnd by the action is json formatted as CustomerCode and Name
Widget code in form
$this->widget('bootstrap.widgets.TbSelect2', array(
'asDropDownList' => false,
'name' => 'CustomerCode',
'options' => array(
'placeholder' => 'Type a Customer Code',
'minimumInputLength' => '2',
'width' => '40%',
'ajax' => array(
//'url'=> 'http://api.rottentomatoes.com/api/public/v1.0/movies.json',
'url'=> Yii::app()->getBaseUrl(true).'/customer/SearchCustomer',
'dataType' => 'jsonp',
'data' => 'js: function (term,page) {
return {
term: term, // Add all the query string elements here seperated by ,
page_limit: 10,
};
}',
'results' => 'js: function (data,page) {return {results: data};}',
),
'formatResult' => 'js:function(data){
var markup = data.CustomerCode + " - ";
markup += data.Name;
return markup;
}',
'formatSelection' => 'js: function(data) {
return data.CustomerCode;
}',
)));
code snipped from controller action SearchCustomer
Yii::app()->clientScript->scriptMap['jquery.js'] = false;
$this->renderJSON(Customer::model()->searchByCustomer($term));
renderJSON function from base controller class
protected function renderJSON($data)
{
header('Content-type: application/json');
echo $_GET['callback'] . "(";
echo CJSON::encode($data);
echo ")";
foreach (Yii::app()->log->routes as $route) {
if($route instanceof CWebLogRoute) {
$route->enabled = false; // disable any weblogroutes
}
}
Yii::app()->end();
}
Appreciate any help on this
i try.
change
'dataType' => 'jsonp' to 'dataType' => 'json'
and check json format
https://github.com/ivaynberg/select2/issues/920

Yii CForm, Nested Forms Ajax Validation

I have created the following nested forms array;
return array(
'elements' => array(
'contact' => array(
'type' => 'form',
'elements' => array(
'first_name' => array(
'type' => 'text',
),
'last_name' => array(
'type' => 'text',
)
),
),
'lead' => array(
'type' => 'form',
'elements' => array(
'primary_skills' => array(
'type' => 'textarea',
),
),
),
),
'buttons' => array(
'save-lead' => array(
'type' => 'submit',
'label' => 'Create',
'class' => 'btn'
),
)
);
i have view page like this
echo $form->renderBegin();
echo $form['lead'];
echo $form['contact'];
echo $form->buttons['save-lead'];
echo $form->renderEnd();
my actionCreate is like this
$form = new CForm('application.views.leads.register');
$form['lead']->model = new Lead;
$form['contact']->model = new Contact;
// how can i perform ajax validation only for $form['contact']
$this->performAjaxValidation($model);
//if contact form save btn is clicked
if ($form->submitted('save-lead') && $form['contact']->validate() &&
$form['lead']->validate()
) {
$contact = $form['contact']->model;
$lead = $form['lead']->model;
if ($contact->save()) {
$lead->contact_id = $contact->id;
if ($lead->save()) {
$this->redirect(array('leads/view', 'id' => $lead->id));
}
}
}
ajax validation method is
protected function performAjaxValidation($model)
{
if (isset($_POST['ajax']) && $_POST['ajax'] === 'contact') {
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
so my question is how can i perform ajax validation on $form['contact'] and $form['lead'] elements individually?
You can have several forms in a page but they should not be nested.
Nested forms are invalid.
You should make your own validation
in actionCreate and actionUpdate of your controller you must add (i have main model "Invoice" and secondary "InvoiceDetails", and there could be more than 1 form for InvoiceDetails). But of course forms cannot be nested!
public function actionCreate()
{
...
$PostVar = 'Invoices';
if (Yii::app()->request->isAjaxRequest)
{ // if ajax
$this->performAjaxValidation($model, strtolower($PostVar) . '-form');
$PostVar = ucfirst($PostVar);
if (isset($_POST[$PostVar]))
{
$model->attributes = $_POST[$PostVar];
$dynamicModel = new InvoiceDetails(); //your model
$valid = self::validate($model, $dynamicModel);
if (!isset($_POST['ajax']))
{
if (isset($_POST['InvoiceDetails']))
{
$allDetails = array();
$allDynamicModels = $_POST['InvoiceDetails'];
//your own customization
foreach ($allDynamicModels as $key => $value)
{
$InvDet = InvoiceDetails::model()->findByPk($_POST['InvoiceDetails'][$key]['id']);
if (!isset($InvDet))
{
$InvDet = new InvoiceDetails();
}
$InvDet->attributes = $_POST['InvoiceDetails'][$key];
$InvDet->save();
$allDetails[] = $InvDet;
}
}
$model->invoicedetails = $allDetails;
if ($model->save())
{
echo CJSON::encode(array('status' => 'success'));
Yii::app()->end();
}
else
{
echo CJSON::encode(array('status' => 'error saving'));
Yii::app()->end();
}
}
// Here we say if valid
if (!isset($valid))
{
echo CJSON::encode(array('status' => 'success'));
}
else
{
echo $valid;
}
Yii::app()->end();
}
else
{
$this->renderPartial('_form', ...);
}
}
else
{
// not AJAX request
$this->render('_form', ...));
}
Nested Forms are invalid. You can use scenarios to validate the form at different instances.
Example:
`if ($form->submitted('save-lead'){
$form->scenario = 'save-lead';
if($form->validate()) {
$form->save();
}
} else {
$form->scenario = 'contact';
if($form->validate()){
$form->save();
}
}
$this->render('_form', array('form'=>$form);`

Show "page=1" params in Yii CLinkPager first page link

I want to make a CListView that remembers search form input. My application saves every search input (and the current page viewed by user) into user session and add the search id into the pagination params.
$pagination = array(
'pageVar' => 'page',
'currentPage' => $currentPage - 1,
'params' => array(
'search' => $searchId,
)
);
But since the first page link at the CLinkPager don't show 'page=1' parameter, everytime I click the link from other page (say page 2), the application will think that I need the current 'search' (whose current page value is 2).
To rephrase the question, is there any way to make CLinkPager shows param 'page=1' at the first page link? Or do you have another suggestion on handling this :)
Thanks
Edited
My overall code is below. If you want to give a better suggestion, please do :)
public function actionIndex() {
$model = new User();
if (isset($_POST['User'])) {
$model->setAttributes($_POST['User']);
} else {
if (isset($_GET['search'])) {
$searchId = $_GET['search'];
//Just getting a variable stored in the session
//array(
// $searchId => array(
// 'model' => $model //the search model
// 'pageSize' => $pageSize //current pagination page size
// 'currentPage' => $currentPage //the search model
// )
//);
$search = Yii::app()->user->getSearch($searchId);
$model->setAttributes($search['model'], false);
$currentPage = $search['currentPage'];
$pageSize = $search['pageSize'];
} else {
//create default search
}
}
//Managing pageSize parameter
if (isset($_GET['pageSize'])) { //Check whether there is pageSize in $_GET parameters
if (in_array($_GET['pageSize'], $this->getPageSizes())) {
$pageSize = $_GET['pageSize'];
} else {
$pageSize = self::DEFAULT_SEARCH_PAGESIZE;
}
} else if (!isset($pageSize)) { //Check whether pageSize is already defined before
$pageSize = self::DEFAULT_SEARCH_PAGESIZE;
}
//Managing currentPage parameter
if (isset($_GET['page'])) { //Check whether there is page parameter in $_GET parameter
$currentPage = $_GET['page'];
} else if (!isset($currentPage)) { //Check whether page is already defined before
$currentPage = self::DEFAULT_CURRENT_PAGE;
}
//Saving all into new session
$newSearch = array(
'model' => $model->getAttributes(),
'currentPage' => $currentPage,
'pageSize' => $pageSize,
);
//Check whether if there is a search before and if it exists check whether the two search is the same
if (!isset($search) || (serialize($search) != serialize($newSearch))) {
//Saves into session
$searchId = Yii::app()->user->saveSearch($newSearch);
} //else use the old search id
//-----------------------------------------------------
//Building the search
//Setting criteria
$criteria = new CDbCriteria(array(
'select' => array(
'*', //just to simplify things
),
));
//Appending criteria to search model used
$criteria = $model->search($criteria);
//Appending pagination to current variables
$pagination = array(
'pageSize' => $pageSize,
'pageVar' => 'page',
'currentPage' => $currentPage - 1,
'params' => array(
'search' => $searchId, //include the search ID in the link pager request URL
)
);
//Creating dataProvider for the search
$dataProvider = new CActiveDataProvider('Property', array(
'criteria' => $criteria,
'pagination' => $pagination,
));
//Rendering
$this->render('index', array(
'dataProvider' => $dataProvider,
'pageSize' => $pageSize,
'model' => $model,
'searchId' => $searchId,
));
}
You could probably extend CPagination by modifying the createPageUrl method and remove/change the if($page>0) condition to always be true. (Or better yet, add an additional parameter to turn on/off that behavior.)