[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/vtlib/Vtiger/ -> Link.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/Utils/StringTemplate.php');
  12  include_once  'vtlib/Vtiger/LinkData.php';
  13  
  14  /**
  15   * Provides API to handle custom links
  16   * @package vtlib
  17   */
  18  class Vtiger_Link {
  19      var $tabid;
  20      var $linkid;
  21      var $linktype;
  22      var $linklabel;
  23      var $linkurl;
  24      var $linkicon;
  25      var $sequence;
  26      var $status = false;
  27      var $handler_path;
  28      var $handler_class;
  29      var $handler;
  30  
  31      // Ignore module while selection
  32      const IGNORE_MODULE = -1; 
  33  
  34      /**
  35       * Constructor
  36       */
  37  	function __construct() {
  38      }
  39  
  40      /**
  41       * Initialize this instance.
  42       */
  43  	function initialize($valuemap) {
  44          $this->tabid  = $valuemap['tabid'];
  45          $this->linkid = $valuemap['linkid'];
  46          $this->linktype=$valuemap['linktype'];
  47          $this->linklabel=$valuemap['linklabel'];
  48          $this->linkurl  =decode_html($valuemap['linkurl']);
  49          $this->linkicon =decode_html($valuemap['linkicon']);
  50          $this->sequence =$valuemap['sequence'];
  51          $this->status   =$valuemap['status'];
  52          $this->handler_path    =$valuemap['handler_path'];
  53          $this->handler_class=$valuemap['handler_class'];
  54          $this->handler        =$valuemap['handler'];
  55      }
  56  
  57      /**
  58       * Get module name.
  59       */
  60  	function module() {
  61          if(!empty($this->tabid)) {
  62              return getTabModuleName($this->tabid);
  63          }
  64          return false;
  65      }
  66  
  67      /**
  68       * Get unique id for the insertion
  69       */
  70  	static function __getUniqueId() {
  71          global $adb;
  72          return $adb->getUniqueID('vtiger_links');
  73      }
  74  
  75      /** Cache (Record) the schema changes to improve performance */
  76      static $__cacheSchemaChanges = Array();
  77  
  78      /**
  79       * Initialize the schema (tables)
  80       */
  81  	static function __initSchema() {
  82          /* vtiger_links is already core product table */
  83          /*if(empty(self::$__cacheSchemaChanges['vtiger_links'])) {
  84              if(!Vtiger_Utils::CheckTable('vtiger_links')) {
  85                  Vtiger_Utils::CreateTable(
  86                      'vtiger_links',
  87                      '(linkid INT NOT NULL PRIMARY KEY,
  88                      tabid INT, linktype VARCHAR(20), linklabel VARCHAR(30), linkurl VARCHAR(255), linkicon VARCHAR(100), sequence INT, status INT(1) NOT NULL DEFAULT 1)',
  89                      true);
  90                  Vtiger_Utils::ExecuteQuery(
  91                      'CREATE INDEX link_tabidtype_idx on vtiger_links(tabid,linktype)');
  92              }
  93              self::$__cacheSchemaChanges['vtiger_links'] = true;
  94          }*/
  95      }
  96  
  97      /**
  98       * Add link given module
  99       * @param Integer Module ID
 100       * @param String Link Type (like DETAILVIEW). Useful for grouping based on pages.
 101       * @param String Label to display
 102       * @param String HREF value or URL to use for the link
 103       * @param String ICON to use on the display
 104       * @param Integer Order or sequence of displaying the link
 105       */
 106  	static function addLink($tabid, $type, $label, $url, $iconpath='',$sequence=0, $handlerInfo=null) {
 107          global $adb;
 108          self::__initSchema();
 109          $checkres = $adb->pquery('SELECT linkid FROM vtiger_links WHERE tabid=? AND linktype=? AND linkurl=? AND linkicon=? AND linklabel=?',
 110              Array($tabid, $type, $url, $iconpath, $label));
 111          if(!$adb->num_rows($checkres)) {
 112              $uniqueid = self::__getUniqueId();
 113              $sql = 'INSERT INTO vtiger_links (linkid,tabid,linktype,linklabel,linkurl,linkicon,'.
 114              'sequence';
 115              $params = Array($uniqueid, $tabid, $type, $label, $url, $iconpath, $sequence);
 116              if(!empty($handlerInfo)) {
 117                  $sql .= (', handler_path, handler_class, handler');
 118                  $params[] = $handlerInfo['path'];
 119                  $params[] = $handlerInfo['class'];
 120                  $params[] = $handlerInfo['method'];
 121              }
 122              $sql .= (') VALUES ('.generateQuestionMarks($params).')');
 123              $adb->pquery($sql, $params);
 124              self::log("Adding Link ($type - $label) ... DONE");
 125          }
 126      }
 127  
 128      /**
 129       * Delete link of the module
 130       * @param Integer Module ID
 131       * @param String Link Type (like DETAILVIEW). Useful for grouping based on pages.
 132       * @param String Display label
 133       * @param String URL of link to lookup while deleting
 134       */ 
 135  	static function deleteLink($tabid, $type, $label, $url=false) {
 136          global $adb;
 137          self::__initSchema();
 138          if($url) {
 139              $adb->pquery('DELETE FROM vtiger_links WHERE tabid=? AND linktype=? AND linklabel=? AND linkurl=?',
 140                  Array($tabid, $type, $label, $url));
 141              self::log("Deleting Link ($type - $label - $url) ... DONE");
 142          } else {
 143              $adb->pquery('DELETE FROM vtiger_links WHERE tabid=? AND linktype=? AND linklabel=?',
 144                  Array($tabid, $type, $label));
 145              self::log("Deleting Link ($type - $label) ... DONE");
 146          }
 147      }
 148  
 149      /**
 150       * Delete all links related to module
 151       * @param Integer Module ID.
 152       */
 153  	static function deleteAll($tabid) {
 154          global $adb;
 155          self::__initSchema();
 156          $adb->pquery('DELETE FROM vtiger_links WHERE tabid=?', Array($tabid));
 157          self::log("Deleting Links ... DONE");
 158      }
 159  
 160      /**
 161       * Get all the links related to module
 162       * @param Integer Module ID.
 163       */
 164  	static function getAll($tabid) {
 165          return self::getAllByType($tabid);
 166      }
 167  
 168      /**
 169       * Get all the link related to module based on type
 170       * @param Integer Module ID
 171       * @param mixed String or List of types to select 
 172       * @param Map Key-Value pair to use for formating the link url
 173       */
 174  	static function getAllByType($tabid, $type=false, $parameters=false) {
 175          global $adb, $current_user;
 176          self::__initSchema();
 177  
 178          $multitype = false;
 179  
 180          if($type) {
 181              // Multiple link type selection?
 182              if(is_array($type)) { 
 183                  $multitype = true;
 184                  if($tabid === self::IGNORE_MODULE) {
 185                      $sql = 'SELECT * FROM vtiger_links WHERE linktype IN ('.
 186                          Vtiger_Utils::implodestr('?', count($type), ',') .') ';
 187                      $params = $type;
 188                      $permittedTabIdList = getPermittedModuleIdList();
 189                      if(count($permittedTabIdList) > 0 && $current_user->is_admin !== 'on') {
 190                          array_push($permittedTabIdList, 0);     // Added to support one link for all modules
 191                          $sql .= ' and tabid IN ('.
 192                              Vtiger_Utils::implodestr('?', count($permittedTabIdList), ',').')';
 193                          $params[] = $permittedTabIdList;
 194                      }
 195                      $result = $adb->pquery($sql, Array($adb->flatten_array($params)));
 196                  } else {
 197                      $result = $adb->pquery('SELECT * FROM vtiger_links WHERE (tabid=? OR tabid=0) AND linktype IN ('.
 198                          Vtiger_Utils::implodestr('?', count($type), ',') .')',
 199                              Array($tabid, $adb->flatten_array($type)));
 200                  }            
 201              } else {
 202                  // Single link type selection
 203                  if($tabid === self::IGNORE_MODULE) {
 204                      $result = $adb->pquery('SELECT * FROM vtiger_links WHERE linktype=?', Array($type));
 205                  } else {
 206                      $result = $adb->pquery('SELECT * FROM vtiger_links WHERE (tabid=? OR tabid=0) AND linktype=?', Array($tabid, $type));                
 207                  }
 208              }
 209          } else {
 210              $result = $adb->pquery('SELECT * FROM vtiger_links WHERE tabid=?', Array($tabid));
 211          }
 212  
 213          $strtemplate = new Vtiger_StringTemplate();
 214          if($parameters) {
 215              foreach($parameters as $key=>$value) $strtemplate->assign($key, $value);
 216          }
 217  
 218          $instances = Array();
 219          if($multitype) {
 220              foreach($type as $t) $instances[$t] = Array();
 221          }
 222  
 223          while($row = $adb->fetch_array($result)){
 224              $instance = new self();
 225              $instance->initialize($row);
 226              if(!empty($row['handler_path']) && isFileAccessible($row['handler_path'])) {
 227                  checkFileAccessForInclusion($row['handler_path']);
 228                  require_once $row['handler_path'];
 229                  $linkData = new Vtiger_LinkData($instance, $current_user);
 230                  $ignore = call_user_func(array($row['handler_class'], $row['handler']), $linkData);
 231                  if(!$ignore) {
 232                      self::log("Ignoring Link ... ".var_export($row, true));
 233                      continue;
 234                  }
 235              }
 236              if($parameters) {
 237                  $instance->linkurl = $strtemplate->merge($instance->linkurl);
 238                  $instance->linkicon= $strtemplate->merge($instance->linkicon);
 239              }
 240              if($multitype) {
 241                  $instances[$instance->linktype][] = $instance;
 242              } else {
 243                  $instances[$instance->linktype] = $instance;
 244              }
 245          }
 246          return $instances;
 247      }
 248  
 249      /**
 250       * Extract the links of module for export.
 251       */
 252  	static function getAllForExport($tabid) {
 253          global $adb;
 254          $result = $adb->pquery('SELECT * FROM vtiger_links WHERE tabid=?', array($tabid));
 255          $links  = array();
 256          while($row = $adb->fetch_array($result)) {
 257              $instance = new self();
 258              $instance->initialize($row);
 259              $links[] = $instance;
 260          }
 261          return $links;
 262      }
 263  
 264      /**
 265       * Helper function to log messages
 266       * @param String Message to log
 267       * @param Boolean true appends linebreak, false to avoid it
 268       * @access private
 269       */
 270  	static function log($message, $delimit=true) {
 271          Vtiger_Utils::Log($message, $delimit);
 272      }
 273  
 274      /**
 275       * Checks whether the user is admin or not
 276       * @param Vtiger_LinkData $linkData
 277       * @return Boolean
 278       */
 279  	static function isAdmin($linkData) {
 280          $user = $linkData->getUser();
 281          return $user->is_admin == 'on' || $user->column_fields['is_admin'] == 'on';
 282      }
 283  
 284  }
 285  ?>


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