[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/vtlib/Vtiger/ -> Filter.php (source)

   1  <?php
   2  /*+**********************************************************************************
   3   * The contents of this file are subject to the vtiger CRM Public License Version 1.0
   4   * ("License"); You may not use this file except in compliance with the License
   5   * The Original Code is:  vtiger CRM Open Source
   6   * The Initial Developer of the Original Code is vtiger.
   7   * Portions created by vtiger are Copyright (C) vtiger.
   8   * All Rights Reserved.
   9   ************************************************************************************/
  10  include_once ('vtlib/Vtiger/Utils.php');
  11  include_once ('vtlib/Vtiger/Version.php');
  12  
  13  /**
  14   * Provides API to work with vtiger CRM Custom View (Filter)
  15   * @package vtlib
  16   */
  17  class Vtiger_Filter {
  18      /** ID of this filter instance */
  19      var $id;
  20      var $name;
  21      var $isdefault;
  22  
  23      var $status    = false; // 5.1.0 onwards
  24      var $inmetrics = false;
  25      var $entitytype= false;
  26  
  27      var $module;
  28  
  29      /**
  30       * Constructor
  31       */
  32  	function __construct() {
  33      }
  34  
  35      /**
  36       * Get unique id for this instance
  37       * @access private
  38       */
  39  	function __getUniqueId() {
  40          global $adb;
  41          return $adb->getUniqueID('vtiger_customview');
  42      }
  43  
  44      /**
  45       * Initialize this filter instance
  46       * @param Vtiger_Module Instance of the module to which this filter is associated.
  47       * @access private
  48       */
  49  	function initialize($valuemap, $moduleInstance=false) {
  50          $this->id = $valuemap[cvid];
  51          $this->name= $valuemap[viewname];
  52          $this->module=$moduleInstance? $moduleInstance: Vtiger_Module::getInstance($valuemap[tabid]);
  53      }
  54  
  55      /**
  56       * Create this instance
  57       * @param Vtiger_Module Instance of the module to which this filter should be associated with
  58       * @access private
  59       */
  60  	function __create($moduleInstance) {
  61          global $adb;
  62          $this->module = $moduleInstance;
  63  
  64          $this->id = $this->__getUniqueId();
  65          $this->isdefault = ($this->isdefault===true||$this->isdefault=='true')?1:0;
  66          $this->inmetrics = ($this->inmetrics===true||$this->inmetrics=='true')?1:0;
  67  
  68          $adb->pquery("INSERT INTO vtiger_customview(cvid,viewname,setdefault,setmetrics,entitytype) VALUES(?,?,?,?,?)", 
  69              Array($this->id, $this->name, $this->isdefault, $this->inmetrics, $this->module->name));
  70  
  71          self::log("Creating Filter $this->name ... DONE");
  72  
  73          // Filters are role based from 5.1.0 onwards
  74          if(!$this->status) {
  75              if(strtoupper(trim($this->name)) == 'ALL') $this->status = '0'; // Default
  76              else $this->status = '3'; // Public
  77              $adb->pquery("UPDATE vtiger_customview SET status=? WHERE cvid=?", Array($this->status, $this->id));
  78  
  79              self::log("Setting Filter $this->name to status [$this->status] ... DONE");
  80          }
  81          // END
  82          
  83      }
  84  
  85      /**
  86       * Update this instance
  87       * @access private
  88       * @internal TODO
  89       */
  90  	function __update() {
  91          self::log("Updating Filter $this->name ... DONE");
  92      }
  93  
  94      /**
  95       * Delete this instance
  96       * @access private
  97       */
  98  	function __delete() {
  99          global $adb;
 100          $adb->pquery("DELETE FROM vtiger_cvadvfilter WHERE cvid=?", Array($this->id));
 101          $adb->pquery("DELETE FROM vtiger_cvcolumnlist WHERE cvid=?", Array($this->id));
 102          $adb->pquery("DELETE FROM vtiger_customview WHERE cvid=?", Array($this->id));
 103      }
 104  
 105      /**
 106       * Save this instance
 107       * @param Vtiger_Module Instance of the module to use
 108       */
 109  	function save($moduleInstance=false) {
 110          if($this->id) $this->__update();
 111          else $this->__create($moduleInstance);
 112          return $this->id;
 113      }
 114  
 115      /**
 116       * Delete this instance
 117       * @access private
 118       */
 119  	function delete() {
 120          $this->__delete();
 121      }
 122  
 123      /**
 124       * Get the column value to use in custom view tables.
 125       * @param Vtiger_Field Instance of the field
 126       * @access private
 127       */
 128  	function __getColumnValue($fieldInstance) {
 129          $tod = split('~', $fieldInstance->typeofdata);
 130          $displayinfo = $fieldInstance->getModuleName().'_'.str_replace(' ','_',$fieldInstance->label).':'.$tod[0];
 131          $cvcolvalue = "$fieldInstance->table:$fieldInstance->column:$fieldInstance->name:$displayinfo";
 132          return $cvcolvalue;
 133      }
 134  
 135      /**
 136       * Add the field to this filer instance
 137       * @param Vtiger_Field Instance of the field
 138       * @param Integer Index count to use
 139       */
 140  	function addField($fieldInstance, $index=0) {
 141          global $adb;
 142  
 143          $cvcolvalue = $this->__getColumnValue($fieldInstance);
 144  
 145          $adb->pquery("UPDATE vtiger_cvcolumnlist SET columnindex=columnindex+1 WHERE cvid=? AND columnindex>=? ORDER BY columnindex DESC", 
 146              Array($this->id, $index));
 147          $adb->pquery("INSERT INTO vtiger_cvcolumnlist(cvid,columnindex,columnname) VALUES(?,?,?)", Array($this->id, $index, $cvcolvalue));
 148  
 149          $this->log("Adding $fieldInstance->name to $this->name filter ... DONE");
 150          return $this;
 151      }
 152  
 153      /**
 154       * Add rule to this filter instance
 155       * @param Vtiger_Field Instance of the field
 156       * @param String One of [EQUALS, NOT_EQUALS, STARTS_WITH, ENDS_WITH, CONTAINS, DOES_NOT_CONTAINS, LESS_THAN, 
 157       *                       GREATER_THAN, LESS_OR_EQUAL, GREATER_OR_EQUAL]
 158       * @param String Value to use for comparision
 159       * @param Integer Index count to use
 160       */
 161  	function addRule($fieldInstance, $comparator, $comparevalue, $index=0, $group=1, $condition='and') {
 162          global $adb;
 163  
 164          if(empty($comparator)) return $this;
 165  
 166          $comparator = self::translateComparator($comparator);
 167          $cvcolvalue = $this->__getColumnValue($fieldInstance);
 168  
 169          $adb->pquery("UPDATE vtiger_cvadvfilter set columnindex=columnindex+1 WHERE cvid=? AND columnindex>=? ORDER BY columnindex DESC",
 170              Array($this->id, $index));        
 171          $adb->pquery("INSERT INTO vtiger_cvadvfilter(cvid, columnindex, columnname, comparator, value, groupid, column_condition) VALUES(?,?,?,?,?,?,?)",
 172              Array($this->id, $index, $cvcolvalue, $comparator, $comparevalue, $group, $condition));
 173  
 174          Vtiger_Utils::Log("Adding Condition " . self::translateComparator($comparator,true) ." on $fieldInstance->name of $this->name filter ... DONE");
 175          
 176          return $this;
 177      }
 178  
 179      /**
 180       * Translate comparator (condition) to long or short form.
 181       * @access private
 182       * @internal Used from Vtiger_PackageExport also
 183       */
 184  	static function translateComparator($value, $tolongform=false) {
 185          $comparator = false;
 186          if($tolongform) {
 187              $comparator = strtolower($value);
 188              if($comparator == 'e') $comparator = 'EQUALS';
 189              else if($comparator == 'n') $comparator = 'NOT_EQUALS';
 190              else if($comparator == 's') $comparator = 'STARTS_WITH';
 191              else if($comparator == 'ew') $comparator = 'ENDS_WITH';
 192              else if($comparator == 'c') $comparator = 'CONTAINS';
 193              else if($comparator == 'k') $comparator = 'DOES_NOT_CONTAINS';
 194              else if($comparator == 'l') $comparator = 'LESS_THAN';
 195              else if($comparator == 'g') $comparator = 'GREATER_THAN';
 196              else if($comparator == 'm') $comparator = 'LESS_OR_EQUAL';
 197              else if($comparator == 'h') $comparator = 'GREATER_OR_EQUAL';
 198          } else {
 199              $comparator = strtoupper($value);
 200              if($comparator == 'EQUALS') $comparator = 'e';
 201              else if($comparator == 'NOT_EQUALS') $comparator = 'n';
 202              else if($comparator == 'STARTS_WITH') $comparator = 's';
 203              else if($comparator == 'ENDS_WITH') $comparator = 'ew';
 204              else if($comparator == 'CONTAINS') $comparator = 'c';
 205              else if($comparator == 'DOES_NOT_CONTAINS') $comparator = 'k';
 206              else if($comparator == 'LESS_THAN') $comparator = 'l';
 207              else if($comparator == 'GREATER_THAN') $comparator = 'g';
 208              else if($comparator == 'LESS_OR_EQUAL') $comparator = 'm';
 209              else if($comparator == 'GREATER_OR_EQUAL') $comparator = 'h';
 210          }
 211          return $comparator;
 212      }
 213  
 214      /**
 215       * Helper function to log messages
 216       * @param String Message to log
 217       * @param Boolean true appends linebreak, false to avoid it
 218       * @access private
 219       */
 220  	static function log($message, $delim=true) {
 221          Vtiger_Utils::Log($message, $delim);
 222      }
 223  
 224      /**
 225       * Get instance by filterid or filtername
 226       * @param mixed filterid or filtername
 227       * @param Vtiger_Module Instance of the module to use when filtername is used
 228       */
 229  	static function getInstance($value, $moduleInstance=false) {
 230          global $adb;
 231          $instance = false;
 232  
 233          $query = false;
 234          $queryParams = false;
 235          if(Vtiger_Utils::isNumber($value)) {
 236              $query = "SELECT * FROM vtiger_customview WHERE cvid=?";
 237              $queryParams = Array($value);
 238          } else {
 239              $query = "SELECT * FROM vtiger_customview WHERE viewname=? AND entitytype=?";
 240              $queryParams = Array($value, $moduleInstance->name);
 241          }
 242          $result = $adb->pquery($query, $queryParams);
 243          if($adb->num_rows($result)) {
 244              $instance = new self();
 245              $instance->initialize($adb->fetch_array($result), $moduleInstance);
 246          }
 247          return $instance;
 248      }
 249  
 250      /**
 251       * Get all instances of filter for the module
 252       * @param Vtiger_Module Instance of module
 253       */
 254  	static function getAllForModule($moduleInstance) {
 255          global $adb;
 256          $instances = false;
 257  
 258          $query = "SELECT * FROM vtiger_customview WHERE entitytype=?";
 259          $queryParams = Array($moduleInstance->name);
 260          
 261          $result = $adb->pquery($query, $queryParams);
 262          for($index = 0; $index < $adb->num_rows($result); ++$index) {
 263              $instance = new self();
 264              $instance->initialize($adb->fetch_array($result), $moduleInstance);
 265              $instances[] = $instance;
 266          }
 267          return $instances;
 268      }
 269  
 270      /**
 271       * Delete filter associated for module
 272       * @param Vtiger_Module Instance of module
 273       */
 274  	static function deleteForModule($moduleInstance) {
 275          global $adb;
 276  
 277          $cvidres = $adb->pquery("SELECT cvid FROM vtiger_customview WHERE entitytype=?", Array($moduleInstance->name));
 278          if($adb->num_rows($cvidres)) {
 279              $cvids = Array();
 280              for($index = 0; $index < $adb->num_rows($cvidres); ++$index) {
 281                  $cvids[] = $adb->query_result($cvidres, $index, 'cvid');
 282              }
 283              if(!empty($cvids)) {
 284                  $adb->pquery("DELETE FROM vtiger_cvadvfilter WHERE cvid  IN (" . implode(',', $cvids) . ")", array());
 285                  $adb->pquery("DELETE FROM vtiger_cvcolumnlist WHERE cvid IN (" . implode(',', $cvids) . ")", array());
 286                  $adb->pquery("DELETE FROM vtiger_customview WHERE cvid   IN (" . implode(',', $cvids) . ")", array());
 287              }
 288          }
 289      }
 290  }
 291  ?>


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1