[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/vtlib/Vtiger/ -> ModuleBasic.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/Access.php');
  11  include_once ('vtlib/Vtiger/Block.php');
  12  include_once ('vtlib/Vtiger/Field.php');
  13  include_once ('vtlib/Vtiger/Filter.php');
  14  include_once ('vtlib/Vtiger/Profile.php');
  15  include_once ('vtlib/Vtiger/Menu.php');
  16  include_once ('vtlib/Vtiger/Link.php');
  17  include_once ('vtlib/Vtiger/Event.php');
  18  include_once ('vtlib/Vtiger/Webservice.php');
  19  include_once ('vtlib/Vtiger/Version.php');
  20  require_once  'includes/runtime/Cache.php';
  21  
  22  /**
  23   * Provides API to work with vtiger CRM Module
  24   * @package vtlib
  25   */
  26  class Vtiger_ModuleBasic {
  27      /** ID of this instance */
  28      var $id = false;
  29      var $name = false;
  30      var $label = false;
  31      var $version= 0;
  32      var $minversion = false;
  33      var $maxversion = false;
  34  
  35      var $presence = 0;
  36      var $ownedby = 0; // 0 - Sharing Access Enabled, 1 - Sharing Access Disabled
  37      var $tabsequence = false;
  38      var $parent = false;
  39      var $customized = 0;
  40          var $trial = 0;
  41  
  42      var $isentitytype = true; // Real module or an extension?
  43  
  44      var $entityidcolumn = false;
  45      var $entityidfield = false;
  46  
  47      var $basetable = false;
  48      var $basetableid=false;
  49      var $customtable=false;
  50      var $grouptable = false;
  51  
  52      const EVENT_MODULE_ENABLED     = 'module.enabled';
  53      const EVENT_MODULE_DISABLED    = 'module.disabled';
  54      const EVENT_MODULE_POSTINSTALL = 'module.postinstall';
  55      const EVENT_MODULE_PREUNINSTALL= 'module.preuninstall';
  56      const EVENT_MODULE_PREUPDATE   = 'module.preupdate';
  57      const EVENT_MODULE_POSTUPDATE  = 'module.postupdate';
  58  
  59  
  60      /**
  61       * Constructor
  62       */
  63  	function __construct() {
  64      }
  65  
  66      /**
  67       * Initialize this instance
  68       * @access private
  69       */
  70  	function initialize($valuemap) {
  71          $this->id = $valuemap['tabid'];
  72          $this->name=$valuemap['name'];
  73          $this->label=$valuemap['tablabel'];
  74          $this->version=$valuemap['version'];
  75  
  76          $this->presence = $valuemap['presence'];
  77          $this->ownedby = $valuemap['ownedby'];
  78          $this->tabsequence = $valuemap['tabsequence'];
  79          $this->parent = $valuemap['parent'];
  80          $this->customized = $valuemap['customized'];
  81                  $this->trial = $valuemap['trial'];
  82  
  83          $this->isentitytype = $valuemap['isentitytype'];
  84  
  85          if($this->isentitytype || $this->name == 'Users') {
  86              // Initialize other details too
  87              $this->initialize2();
  88          }
  89      }
  90  
  91      /**
  92       * Initialize more information of this instance
  93       * @access private
  94       */
  95  	function initialize2() {
  96          $entitydata = Vtiger_Functions::getEntityModuleInfo($this->name);
  97          if ($entitydata) {
  98              $this->basetable = $entitydata['tablename'];
  99              $this->basetableid=$entitydata['entityidfield'];
 100          }
 101      }
 102  
 103      /**
 104       * Get unique id for this instance
 105       * @access private
 106       */
 107  	function __getUniqueId() {
 108          global $adb;
 109          $result = $adb->query("SELECT MAX(tabid) AS max_seq FROM vtiger_tab");
 110          $maxseq = $adb->query_result($result, 0, 'max_seq');
 111          return ++$maxseq;
 112      }
 113  
 114      /**
 115       * Get next sequence to use for this instance
 116       * @access private
 117       */
 118  	function __getNextSequence() {
 119          global $adb;
 120          $result = $adb->pquery("SELECT MAX(tabsequence) AS max_tabseq FROM vtiger_tab", array());
 121          $maxtabseq = $adb->query_result($result, 0, 'max_tabseq');
 122          return ++$maxtabseq;
 123      }
 124  
 125      /**
 126       * Initialize vtiger schema changes.
 127       * @access private
 128       */
 129  	function __handleVtigerCoreSchemaChanges() {
 130          // Add version column to the table first
 131          Vtiger_Utils::AddColumn('vtiger_tab', 'version', ' VARCHAR(10)');
 132          Vtiger_Utils::AddColumn('vtiger_tab', 'parent', ' VARCHAR(30)');
 133      }
 134  
 135      /**
 136       * Create this module instance
 137       * @access private
 138       */
 139  	function __create() {
 140          global $adb;
 141  
 142          self::log("Creating Module $this->name ... STARTED");
 143  
 144          $this->id = $this->__getUniqueId();
 145          if(!$this->tabsequence) $this->tabsequence = $this->__getNextSequence();
 146          if(!$this->label) $this->label = $this->name;
 147  
 148          $customized = 1; // To indicate this is a Custom Module
 149  
 150          $this->__handleVtigerCoreSchemaChanges();
 151  
 152          $adb->pquery("INSERT INTO vtiger_tab (tabid,name,presence,tabsequence,tablabel,modifiedby,
 153              modifiedtime,customized,ownedby,version,parent) VALUES (?,?,?,?,?,?,?,?,?,?,?)",
 154              Array($this->id, $this->name, $this->presence, -1, $this->label, NULL, NULL, $customized, $this->ownedby, $this->version,$this->parent));
 155  
 156          $useisentitytype = $this->isentitytype? 1 : 0;
 157          $adb->pquery('UPDATE vtiger_tab set isentitytype=? WHERE tabid=?',Array($useisentitytype, $this->id));
 158  
 159          if(!Vtiger_Utils::CheckTable('vtiger_tab_info')) {
 160              Vtiger_Utils::CreateTable(
 161                  'vtiger_tab_info',
 162                  '(tabid INT, prefname VARCHAR(256), prefvalue VARCHAR(256), FOREIGN KEY fk_1_vtiger_tab_info(tabid) REFERENCES vtiger_tab(tabid) ON DELETE CASCADE ON UPDATE CASCADE)',
 163                  true);
 164          }
 165          if($this->minversion) {
 166              $tabResult = $adb->pquery("SELECT 1 FROM vtiger_tab_info WHERE tabid=? AND prefname='vtiger_min_version'", array($this->id));
 167              if ($adb->num_rows($tabResult) > 0) {
 168                  $adb->pquery("UPDATE vtiger_tab_info SET prefvalue=? WHERE tabid=? AND prefname='vtiger_min_version'", array($this->minversion,$this->id));
 169              } else {
 170                  $adb->pquery('INSERT INTO vtiger_tab_info(tabid, prefname, prefvalue) VALUES (?,?,?)', array($this->id, 'vtiger_min_version', $this->minversion));
 171              }
 172          }
 173          if($this->maxversion) {
 174              $tabResult = $adb->pquery("SELECT 1 FROM vtiger_tab_info WHERE tabid=? AND prefname='vtiger_max_version'", array($this->id));
 175              if ($adb->num_rows($tabResult) > 0) {
 176                  $adb->pquery("UPDATE vtiger_tab_info SET prefvalue=? WHERE tabid=? AND prefname='vtiger_max_version'", array($this->maxversion,$this->id));
 177              } else {
 178                  $adb->pquery('INSERT INTO vtiger_tab_info(tabid, prefname, prefvalue) VALUES (?,?,?)', array($this->id, 'vtiger_max_version', $this->maxversion));
 179              }
 180          }
 181  
 182          Vtiger_Profile::initForModule($this);
 183  
 184          self::syncfile();
 185  
 186          if($this->isentitytype) {
 187              Vtiger_Access::initSharing($this);
 188          }
 189                  
 190                  $moduleInstance=  Vtiger_Module::getInstance($this->name);                
 191                  $parentTab=$this->parent;
 192                  
 193                  if(!empty($parentTab)){
 194                          $menuInstance = Vtiger_Menu::getInstance($parentTab);
 195              $menuInstance->addModule($moduleInstance);
 196                  }
 197  
 198          self::log("Creating Module $this->name ... DONE");
 199      }
 200  
 201      /**
 202       * Update this instance
 203       * @access private
 204       */
 205  	function __update() {
 206          self::log("Updating Module $this->name ... DONE");
 207      }
 208  
 209      /**
 210       * Delete this instance
 211       * @access private
 212       */
 213  	function __delete() {
 214          Vtiger_Module::fireEvent($this->name,
 215              Vtiger_Module::EVENT_MODULE_PREUNINSTALL);
 216  
 217          global $adb;
 218          if($this->isentitytype) {
 219              $this->unsetEntityIdentifier();
 220              $this->deleteRelatedLists();
 221          }
 222  
 223          $adb->pquery("DELETE FROM vtiger_tab WHERE tabid=?", Array($this->id));
 224          self::log("Deleting Module $this->name ... DONE");
 225      }
 226  
 227      /**
 228       * Update module version information
 229       * @access private
 230       */
 231  	function __updateVersion($newversion) {
 232          $this->__handleVtigerCoreSchemaChanges();
 233          global $adb;
 234          $adb->pquery("UPDATE vtiger_tab SET version=? WHERE tabid=?", Array($newversion, $this->id));
 235          $this->version = $newversion;
 236          self::log("Updating version to $newversion ... DONE");
 237      }
 238  
 239      /**
 240       * Save this instance
 241       */
 242  	function save() {
 243          if($this->id) $this->__update();
 244          else $this->__create();
 245          return $this->id;
 246      }
 247  
 248      /**
 249       * Delete this instance
 250       */
 251  	function delete() {
 252          if($this->isentitytype) {
 253              Vtiger_Access::deleteSharing($this);
 254              Vtiger_Access::deleteTools($this);
 255              Vtiger_Filter::deleteForModule($this);
 256              Vtiger_Block::deleteForModule($this);
 257              if(method_exists($this, 'deinitWebservice')) {
 258                  $this->deinitWebservice();
 259              }
 260          }
 261          $this->__delete();
 262          Vtiger_Profile::deleteForModule($this);
 263          Vtiger_Link::deleteAll($this->id);
 264          Vtiger_Menu::detachModule($this);
 265          self::syncfile();
 266      }
 267  
 268      /**
 269       * Initialize table required for the module
 270       * @param String Base table name (default modulename in lowercase)
 271       * @param String Base table column (default modulenameid in lowercase)
 272       *
 273       * Creates basetable, customtable, grouptable <br>
 274       * customtable name is basetable + 'cf'<br>
 275       * grouptable name is basetable + 'grouprel'<br>
 276       */
 277  	function initTables($basetable=false, $basetableid=false) {
 278          $this->basetable = $basetable;
 279          $this->basetableid=$basetableid;
 280  
 281          // Initialize tablename and index column names
 282          $lcasemodname = strtolower($this->name);
 283          if(!$this->basetable) $this->basetable = "vtiger_$lcasemodname";
 284          if(!$this->basetableid)$this->basetableid=$lcasemodname . "id";
 285  
 286          if(!$this->customtable)$this->customtable = $this->basetable . "cf";
 287          if(!$this->grouptable)$this->grouptable = $this->basetable."grouprel";
 288  
 289          Vtiger_Utils::CreateTable($this->basetable,"($this->basetableid INT)",true);
 290          Vtiger_Utils::CreateTable($this->customtable,
 291              "($this->basetableid INT PRIMARY KEY)", true);
 292          if(Vtiger_Version::check('5.0.4', '<=')) {
 293              Vtiger_Utils::CreateTable($this->grouptable,
 294                  "($this->basetableid INT PRIMARY KEY, groupname varchar(100))",true);
 295          }
 296      }
 297  
 298      /**
 299       * Set entity identifier field for this module
 300       * @param Vtiger_Field Instance of field to use
 301       */
 302  	function setEntityIdentifier($fieldInstance) {
 303          global $adb;
 304  
 305          if($this->basetableid) {
 306              if(!$this->entityidfield) $this->entityidfield = $this->basetableid;
 307              if(!$this->entityidcolumn)$this->entityidcolumn= $this->basetableid;
 308          }
 309          if($this->entityidfield && $this->entityidcolumn) {
 310                           $result=$adb->pquery("SELECT tabid FROM vtiger_entityname WHERE tablename=? AND tabid=?",array($fieldInstance->table,$this->id)); 
 311                          if($adb->num_rows($result)==0){
 312                              $adb->pquery("INSERT INTO vtiger_entityname(tabid, modulename, tablename, fieldname, entityidfield, entityidcolumn) VALUES(?,?,?,?,?,?)",
 313                                      Array($this->id, $this->name, $fieldInstance->table, $fieldInstance->name, $this->entityidfield, $this->entityidcolumn));
 314                              self::log("Setting entity identifier ... DONE");
 315                          }else{ 
 316                              $adb->pquery("UPDATE vtiger_entityname SET fieldname=?,entityidfield=?,entityidcolumn=? WHERE tablename=? AND tabid=?", 
 317                                 array($fieldInstance->name,$this->entityidfield,$this->entityidcolumn,$fieldInstance->table,$this->id)); 
 318                             self::log("Updating entity identifier ... DONE"); 
 319                          } 
 320          }
 321      }
 322  
 323      /**
 324       * Unset entity identifier information
 325       */
 326  	function unsetEntityIdentifier() {
 327          global $adb;
 328          $adb->pquery("DELETE FROM vtiger_entityname WHERE tabid=?", Array($this->id));
 329          self::log("Unsetting entity identifier ... DONE");
 330      }
 331  
 332      /**
 333       * Delete related lists information
 334       */
 335  	function deleteRelatedLists() {
 336          global $adb;
 337          $adb->pquery("DELETE FROM vtiger_relatedlists WHERE tabid=?", Array($this->id));
 338          self::log("Deleting related lists ... DONE");
 339      }
 340  
 341      /**
 342       * Delete links information
 343       */
 344  	function deleteLinks() {
 345          global $adb;
 346          $adb->pquery("DELETE FROM vtiger_links WHERE tabid=?", Array($this->id));
 347          self::log("Deleting links ... DONE");
 348      }
 349  
 350      /**
 351       * Configure default sharing access for the module
 352       * @param String Permission text should be one of ['Public_ReadWriteDelete', 'Public_ReadOnly', 'Public_ReadWrite', 'Private']
 353       */
 354  	function setDefaultSharing($permission_text='Public_ReadWriteDelete') {
 355          Vtiger_Access::setDefaultSharing($this, $permission_text);
 356      }
 357  
 358      /**
 359       * Allow module sharing control
 360       */
 361  	function allowSharing() {
 362          Vtiger_Access::allowSharing($this, true);
 363      }
 364      /**
 365       * Disallow module sharing control
 366       */
 367  	function disallowSharing() {
 368          Vtiger_Access::allowSharing($this, false);
 369      }
 370  
 371      /**
 372       * Enable tools for this module
 373       * @param mixed String or Array with value ['Import', 'Export', 'Merge']
 374       */
 375  	function enableTools($tools) {
 376          if(is_string($tools)) {
 377              $tools = Array(0 => $tools);
 378          }
 379  
 380          foreach($tools as $tool) {
 381              Vtiger_Access::updateTool($this, $tool, true);
 382          }
 383      }
 384  
 385      /**
 386       * Disable tools for this module
 387       * @param mixed String or Array with value ['Import', 'Export', 'Merge']
 388       */
 389  	function disableTools($tools) {
 390          if(is_string($tools)) {
 391              $tools = Array(0 => $tools);
 392          }
 393          foreach($tools as $tool) {
 394              Vtiger_Access::updateTool($this, $tool, false);
 395          }
 396      }
 397  
 398      /**
 399       * Add block to this module
 400       * @param Vtiger_Block Instance of block to add
 401       */
 402  	function addBlock($blockInstance) {
 403          $blockInstance->save($this);
 404          return $this;
 405      }
 406  
 407      /**
 408       * Add filter to this module
 409       * @param Vtiger_Filter Instance of filter to add
 410       */
 411  	function addFilter($filterInstance) {
 412          $filterInstance->save($this);
 413          return $this;
 414      }
 415  
 416      /**
 417       * Get all the fields of the module or block
 418       * @param Vtiger_Block Instance of block to use to get fields, false to get all the block fields
 419       */
 420  	function getFields($blockInstance=false) {
 421          $fields = false;
 422          if($blockInstance) $fields = Vtiger_Field::getAllForBlock($blockInstance, $this);
 423          else $fields = Vtiger_Field::getAllForModule($this);
 424          return $fields;
 425      }
 426  
 427      /**
 428       * Helper function to log messages
 429       * @param String Message to log
 430       * @param Boolean true appends linebreak, false to avoid it
 431       * @access private
 432       */
 433  	static function log($message, $delimit=true) {
 434          Vtiger_Utils::Log($message, $delimit);
 435      }
 436  
 437      /**
 438       * Synchronize the menu information to flat file
 439       * @access private
 440       */
 441  	static function syncfile() {
 442          self::log("Updating tabdata file ... ", false);
 443          create_tab_data_file();
 444          self::log("DONE");
 445      }
 446  }
 447  ?>


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