Conditional editing I.

A collection of tutorials and posts that show additional functionality of MATE

Moderator: KarelB

Conditional editing I.

Postby KarelB » Mon Jul 26, 2010 8:41 am

Sometimes you might want to allow editing only when a certain condition is met.
For example, you want to allow editing if a certain process step is finished or you want to show all records in the table but only want to allow editing for the records that the user is the owner of.

This can be done with MATE. As an example we will take the Example1.php that comes with the free distribution and we only will allow editing for records of employees that belong to the Sales departement.

To do this we have to redefine the edit icon. Easiest way to do this is to remove the icon and add a new edit icon.
To remove the icon add the following to the initiate editor:
Code: Select all
$this->Editor->setConfig('removeIcons','E');

Next add a new edit icon using a callback
Code: Select all
$userIcons[] = array('format_fun' => array(&$this,'getEditIcon'));
$this->Editor->setConfig('userIcons',$userIcons);

The callback getEditIcon will return the normal "edit" code if we are allowed to edit it or it will just popup a warning.
Code: Select all
function getEditIcon($info)
{
   $iconHtml = '';
   $numIcons = 0;
   if(!strcmp($info['department'],'Sales'))
   {
      // Return edit icon with normal edit function
      $iconHtml .= '<li class="edit"><a href="javascript: toAjaxTableEditor(\'edit_row\',\''.$info['id'].'\');" title="Edit"></a></li>';

   } else {
      // Return edit icon with with an alert message and do nothing.
      $iconHtml .= '<li class="edit"><a href="#" onclick=\'alert("Only employees from the Sales department can be eidted");\' title="Edit"></a></li>';
   }
   $numIcons++;
   return array('icon_html' => $iconHtml, 'num_icons' => $numIcons);
}



Now it is only possible to edit rows that belong to the Sales departement. If you try to edit an other row you will get a popup message and nothing will happen.
Here is the complete script:
Code: Select all
<?php
/*
 * Mysql Ajax Table Editor
 *
 * Copyright (c) 2008 Chris Kitchen <info@mysqlajaxtableeditor.com>
 * All rights reserved.
 *
 * See COPYING file for license information.
 *
 * Download the latest version from
 * http://www.mysqlajaxtableeditor.com
 */
require_once('Common.php');
require_once('php/lang/LangVars-en.php');
require_once('php/AjaxTableEditor.php');
class SpecialEdit1 extends Common
{
   var $Editor;
   
   function displayHtml()
   {
      ?>
         <br />
   
         <div align="left" style="position: relative;"><div id="ajaxLoader1"><img src="images/ajax_loader.gif" alt="Loading..." /></div></div>
         
         <br />
         
         <div id="historyButtonsLayer" align="left">
         </div>
   
         <div id="historyContainer">
            <div id="information">
            </div>
      
            <div id="titleLayer" style="padding: 2px; font-weight: bold; font-size: 18px; text-align: center;">
            </div>
      
            <div id="tableLayer" align="center">
            </div>
            
            <div id="recordLayer" align="center">
            </div>      
            
            <div id="searchButtonsLayer" align="center">
            </div>
         </div>
         
         <script type="text/javascript">
            trackHistory = false;
            var ajaxUrl = '<?php echo $_SERVER['PHP_SELF']; ?>';
            toAjaxTableEditor('update_html','');
         </script>
      <?php
   }

   function initiateEditor()
   {
      $tableColumns['id'] = array('display_text' => 'ID', 'perms' => 'TVQSXO');
      $tableColumns['first_name'] = array('display_text' => 'First Name', 'perms' => 'EVCTAXQSHO');
      $tableColumns['last_name'] = array('display_text' => 'Last Name', 'perms' => 'EVCTAXQSHO');
      $tableColumns['email'] = array('display_text' => 'Email', 'perms' => 'EVCTAXQSHO');
      $tableColumns['department'] = array('display_text' => 'Department', 'perms' => 'EVCTAXQSHO', 'select_array' => array('Accounting' => 'Accounting', 'Marketing' => 'Marketing', 'Sales' => 'Sales', 'Production' => 'Production'));
      $tableColumns['hire_date'] = array('display_text' => 'Hire Date', 'perms' => 'EVCTAXQSHO', 'display_mask' => 'date_format(hire_date,"%d %M %Y")', 'calendar' => '%d %B %Y','col_header_info' => 'style="width: 250px;"');
      
      $tableName = 'employees';
      $primaryCol = 'id';
      $errorFun = array(&$this,'logError');
      $permissions = 'EAIDQCSXHO';
      
      $this->Editor = new AjaxTableEditor($tableName,$primaryCol,$errorFun,$permissions,$tableColumns);
      $this->Editor->setConfig('tableInfo','cellpadding="1" width="1000" class="mateTable"');
      $this->Editor->setConfig('orderByColumn','first_name');
      $this->Editor->setConfig('addRowTitle','Add Employee');
      $this->Editor->setConfig('editRowTitle','Edit Employee');
      
      $this->Editor->setConfig('removeIcons','E');
      $userIcons[] = array('format_fun' => array(&$this,'getEditIcon'));
        $this->Editor->setConfig('userIcons',$userIcons);
      
      
   }
   
   function getEditIcon($info)
   {
      $iconHtml = '';
      $numIcons = 0;
      if(!strcmp($info['department'],'Sales'))
      {
         // Return edit icon with normal edit function
         $iconHtml .= '<li class="edit"><a href="javascript: toAjaxTableEditor(\'edit_row\',\''.$info['id'].'\');" title="Edit"></a></li>';
   
      } else {
         // Return edit icon with with an alert message and do nothing.
         $iconHtml .= '<li class="edit"><a href="#" onclick=\'alert("Only employees from the Sales department can be edited");\' title="Edit"></a></li>';
      }
      $numIcons++;
      return array('icon_html' => $iconHtml, 'num_icons' => $numIcons);
   }
   

   function SpecialEdit1()
   {
      if(isset($_POST['json']))
      {
         session_start();
         // Initiating lang vars here is only necessary for the logError, and mysqlConnect functions in Common.php.
         // If you are not using Common.php or you are using your own functions you can remove the following line of code.
         $this->langVars = new LangVars();
         $this->mysqlConnect();
         if(ini_get('magic_quotes_gpc'))
         {
            $_POST['json'] = stripslashes($_POST['json']);
         }
         if(function_exists('json_decode'))
         {
            $data = json_decode($_POST['json']);
         }
         else
         {
            require_once('php/JSON.php');
            $js = new Services_JSON();
            $data = $js->decode($_POST['json']);
         }
         if(empty($data->info) && strlen(trim($data->info)) == 0)
         {
            $data->info = '';
         }
         $this->initiateEditor();
         $this->Editor->main($data->action,$data->info);
         if(function_exists('json_encode'))
         {
            echo json_encode($this->Editor->retArr);
         }
         else
         {
            echo $js->encode($this->Editor->retArr);
         }
      }
      else if(isset($_GET['export']))
      {
            session_start();
            ob_start();
            $this->mysqlConnect();
            $this->initiateEditor();
            echo $this->Editor->exportInfo();
            header("Cache-Control: no-cache, must-revalidate");
            header("Pragma: no-cache");
            header("Content-type: application/x-msexcel");
            header('Content-Type: text/csv');
            header('Content-Disposition: attachment; filename="'.$this->Editor->tableName.'.csv"');
            exit();
        }
      else
      {
         $this->displayHeaderHtml();
         $this->displayHtml();
         $this->displayFooterHtml();
      }
   }
}
$lte = new SpecialEdit1();
?>


Have fun!
Karel
KarelB
 
Posts: 438
Joined: Mon Dec 01, 2008 11:49 pm
Location: The Netherlands

Return to How To

Who is online

Users browsing this forum: No registered users and 1 guest

cron