[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/modules/Settings/LayoutEditor/models/ -> Module.php (source)

   1  <?php
   2  
   3  /*+**********************************************************************************
   4   * The contents of this file are subject to the vtiger CRM Public License Version 1.1
   5   * ("License"); You may not use this file except in compliance with the License
   6   * The Original Code is:  vtiger CRM Open Source
   7   * The Initial Developer of the Original Code is vtiger.
   8   * Portions created by vtiger are Copyright (C) vtiger.
   9   * All Rights Reserved.
  10   ************************************************************************************/
  11  
  12  class Settings_LayoutEditor_Module_Model extends Vtiger_Module_Model {
  13  
  14      public static $supportedModules = false;
  15  
  16      /**
  17       * Function that returns all the fields for the module
  18       * @return <Array of Vtiger_Field_Model> - list of field models
  19       */
  20  	public function getFields() {
  21          if(empty($this->fields)){
  22              $fieldList = array();
  23              $blocks = $this->getBlocks();
  24              $blockId = array();
  25              foreach ($blocks as $block) {
  26                  //to skip events hardcoded block id
  27                  if($block->get('id') == 'EVENT_INVITE_USER_BLOCK_ID') {
  28                      continue;
  29                  }
  30                  $blockId[] = $block->get('id');
  31              }
  32              if(count($blockId) > 0) {
  33                  $fieldList = Settings_LayoutEditor_Field_Model::getInstanceFromBlockIdList($blockId,$moduleModel);
  34              }
  35              //To handle special case for invite users
  36              if($this->getName() == 'Events') {
  37                  $blockModel = new Settings_LayoutEditor_Block_Model();
  38                  $blockModel->set('id','EVENT_INVITE_USER_BLOCK_ID');
  39                  $blockModel->set('label','LBL_INVITE_USER_BLOCK');
  40                  $blockModel->set('module', $this);
  41  
  42                  $fieldModel = new Settings_LayoutEditor_Field_Model();
  43                  $fieldModel->set('name','selectedusers');
  44                  $fieldModel->set('label','LBL_INVITE_USERS');
  45                  $fieldModel->set('block',$blockModel);
  46                  $fieldModel->setModule($this);
  47                  $fieldList[] = $fieldModel;
  48              }
  49              $this->fields = $fieldList;
  50          }
  51          return $this->fields;
  52      }
  53  
  54      /**
  55       * Function returns all the blocks for the module
  56       * @return <Array of Vtiger_Block_Model> - list of block models
  57       */
  58  	public function getBlocks() {
  59          if(empty($this->blocks)) {
  60              $blocksList = array();
  61              $moduleBlocks = Settings_LayoutEditor_Block_Model::getAllForModule($this);
  62              foreach($moduleBlocks as $block){
  63                  if(!$block->get('label')) {
  64                      continue;
  65                  }
  66                  if($this->getName() == 'HelpDesk' && $block->get('label') == 'LBL_COMMENTS'){
  67                      continue;
  68                  }
  69  
  70                  if($block->get('label') != 'LBL_ITEM_DETAILS') {
  71                      $blocksList[$block->get('label')] = $block;
  72                  }
  73              }
  74              //To handle special case for invite users block
  75              if($this->getName() == 'Events') {
  76                  $blockModel = new Settings_LayoutEditor_Block_Model();
  77                  $blockModel->set('id','EVENT_INVITE_USER_BLOCK_ID');
  78                  $blockModel->set('label','LBL_INVITE_USER_BLOCK');
  79                  $blockModel->set('module', $this);
  80                  $blocksList['LBL_INVITE_USER_BLOCK'] = $blockModel;
  81              }
  82              $this->blocks = $blocksList;
  83          }
  84          return $this->blocks;
  85      }
  86  
  87      public function getAddSupportedFieldTypes() {
  88          return array(
  89              'Text','Decimal','Integer','Percent','Currency','Date','Email','Phone','Picklist',
  90              'URL','Checkbox','TextArea','MultiSelectCombo','Skype','Time'
  91          );
  92      }
  93  
  94      /**
  95       * Function whcih will give information about the field types that are supported for add
  96       * @return <Array>
  97       */
  98      public function getAddFieldTypeInfo() {
  99          $fieldTypesInfo = array();
 100          $addFieldSupportedTypes = $this->getAddSupportedFieldTypes();
 101          $lengthSupportedFieldTypes = array('Text','Decimal','Integer','Currency');
 102          foreach($addFieldSupportedTypes as $fieldType) {
 103              $details = array();
 104              if(in_array($fieldType,$lengthSupportedFieldTypes)) {
 105                  $details['lengthsupported'] = true;
 106              }
 107              if($fieldType == 'Decimal' || $fieldType == 'Currency') {
 108                  $details['decimalSupported']  = true;
 109                  $details['maxFloatingDigits'] = 5;
 110                  if($fieldType == 'Currency') {
 111                      $details['decimalReadonly'] = true;
 112                  }
 113                  //including mantisaa and integer part
 114                  $details['maxLength'] = 64;
 115              }
 116              if($fieldType == 'Picklist' || $fieldType == 'MultiSelectCombo') {
 117                  $details['preDefinedValueExists'] = true;
 118                  //text area value type , can give multiple values
 119                  $details['preDefinedValueType'] = 'text';
 120                  if($fieldType == 'Picklist')
 121                      $details['picklistoption'] = true;
 122              }
 123              $fieldTypesInfo[$fieldType] = $details;
 124          }
 125          return $fieldTypesInfo;
 126      }
 127  
 128      public function addField($fieldType, $blockId, $params) {
 129  
 130          $db = PearDatabase::getInstance();
 131  
 132          $label = $params['fieldLabel'];
 133          if($this->checkFIeldExists($label)){
 134              throw new Exception(vtranslate('LBL_DUPLICATE_FIELD_EXISTS', 'Settings::LayoutEditor'), 513);
 135          }
 136          $supportedFieldTypes = $this->getAddSupportedFieldTypes();
 137          if(!in_array($fieldType, $supportedFieldTypes)) {
 138              throw new Exception(vtranslate('LBL_WRONG_FIELD_TYPE', 'Settings::LayoutEditor'), 513);
 139          }
 140  
 141          $max_fieldid = $db->getUniqueID("vtiger_field");
 142          $columnName = 'cf_'.$max_fieldid;
 143          $custfld_fieldid = $max_fieldid;
 144          $moduleName = $this->getName();
 145  
 146          $focus = CRMEntity::getInstance($moduleName);
 147          if (isset($focus->customFieldTable)) {
 148              $tableName=$focus->customFieldTable[0];
 149          } else {
 150              $tableName= 'vtiger_'.strtolower($moduleName).'cf';
 151          }
 152  
 153          $details = $this->getTypeDetailsForAddField($fieldType, $params);
 154          $uitype = $details['uitype'];
 155          $typeofdata = $details['typeofdata'];
 156          $dbType = $details['dbType'];
 157  
 158          $quickCreate = in_array($moduleName,  getInventoryModules()) ? 3 : 1;
 159  
 160          $fieldModel = new Settings_LayoutEditor_Field_Model();
 161          $fieldModel->set('name', $columnName)
 162                     ->set('table', $tableName)
 163                     ->set('generatedtype',2)
 164                     ->set('uitype', $uitype)
 165                     ->set('label', $label)
 166                     ->set('typeofdata',$typeofdata)
 167                     ->set('quickcreate',$quickCreate)
 168                     ->set('columntype', $dbType);
 169  
 170          $blockModel = Vtiger_Block_Model::getInstance($blockId, $this);
 171          $blockModel->addField($fieldModel);
 172  
 173          if($fieldType == 'Picklist' || $fieldType == 'MultiSelectCombo') {
 174              $pickListValues = explode(',',$params['pickListValues']);
 175              $fieldModel->setPicklistValues($pickListValues);
 176          }
 177          return $fieldModel;
 178      }
 179  
 180      public function getTypeDetailsForAddField($fieldType,$params) {
 181          switch ($fieldType) {
 182                  Case 'Text' :
 183                                 $fieldLength = $params['fieldLength'];
 184                                 $uichekdata='V~O~LE~'.$fieldLength;
 185                                 $uitype = 1;
 186                                 $type = "VARCHAR(".$fieldLength.") default ''"; // adodb type
 187                                 break;
 188                  Case 'Decimal' :
 189                                 $fieldLength = $params['fieldLength'];
 190                                 $decimal = $params['decimal'];
 191                                 $uitype = 7;
 192                                 //this may sound ridiculous passing decimal but that is the way adodb wants
 193                                 $dbfldlength = $fieldLength + $decimal + 1;
 194                                 $type="NUMERIC(".$dbfldlength.",".$decimal.")";    // adodb type
 195                                 // Fix for http://trac.vtiger.com/cgi-bin/trac.cgi/ticket/6363
 196                                 $uichekdata='NN~O';
 197                                 break;
 198                 Case 'Percent' :
 199                                 $uitype = 9;
 200                                 $type="NUMERIC(5,2)"; //adodb type
 201                                 $uichekdata='N~O~2~2';
 202                                 break;
 203                 Case 'Currency' :
 204                                 $fieldLength = $params['fieldLength'];
 205                                 $decimal = $params['decimal'];
 206                                 $uitype = 71;
 207                                 $dbfldlength = $fieldLength + $decimal + 1;
 208                                 $decimal = $decimal + 3;
 209                                 $type="NUMERIC(".$dbfldlength.",".$decimal.")"; //adodb type
 210                                 $uichekdata='N~O';
 211                                 break;
 212                 Case 'Date' :
 213                                 $uichekdata='D~O';
 214                                 $uitype = 5;
 215                                 $type = "DATE"; // adodb type
 216                                 break;
 217                 Case 'Email' :
 218                                 $uitype = 13;
 219                                 $type = "VARCHAR(50) default '' "; //adodb type
 220                                 $uichekdata='E~O';
 221                                 break;
 222                 Case 'Time' :
 223                                 $uitype = 14;
 224                                 $type = "TIME";
 225                                 $uichekdata='T~O';
 226                                 break;
 227                 Case 'Phone' :
 228                                 $uitype = 11;
 229                                 $type = "VARCHAR(30) default '' "; //adodb type
 230                                 $uichekdata='V~O';
 231                                 break;
 232                 Case 'Picklist' :
 233                                 $uitype = 16;
 234                                 if(!empty($params['isRoleBasedPickList']))
 235                                      $uitype = 15;
 236                                 $type = "VARCHAR(255) default '' "; //adodb type
 237                                 $uichekdata='V~O';
 238                                 break;
 239                 Case 'URL' :
 240                                 $uitype = 17;
 241                                 $type = "VARCHAR(255) default '' "; //adodb type
 242                                 $uichekdata='V~O';
 243                                 break;
 244                 Case 'Checkbox' :
 245                                 $uitype = 56;
 246                                 $type = "VARCHAR(3) default 0"; //adodb type
 247                                 $uichekdata='C~O';
 248                                 break;
 249                 Case 'TextArea' :
 250                                 $uitype = 21;
 251                                 $type = "TEXT"; //adodb type
 252                                 $uichekdata='V~O';
 253                                 break;
 254                 Case 'MultiSelectCombo' :
 255                                 $uitype = 33;
 256                                 $type = "TEXT"; //adodb type
 257                                 $uichekdata='V~O';
 258                                 break;
 259                 Case 'Skype' :
 260                                 $uitype = 85;
 261                                 $type = "VARCHAR(255) default '' "; //adodb type
 262                                 $uichekdata='V~O';
 263                                 break;
 264                 Case 'Integer' :
 265                                 $fieldLength = $params['fieldLength'];
 266                                 $uitype = 7;
 267                                 if ($fieldLength > 10) {
 268                                      $type = "BIGINT(".$fieldLength.")"; //adodb type
 269                                 } else {
 270                                      $type = "INTEGER(".$fieldLength.")"; //adodb type
 271                                 }
 272                                 $uichekdata='I~O';
 273                                 break;
 274          }
 275          return array(
 276              'uitype' => $uitype,
 277              'typeofdata' => $uichekdata,
 278              'dbType' => $type,
 279          );
 280  
 281      }
 282  
 283      public function checkFIeldExists($fieldLabel) {
 284          $db = PearDatabase::getInstance();
 285          $tabId = array($this->getId());
 286          if($this->getName() == 'Calendar' || $this->getName() == 'Events') {
 287              //Check for fiel exists in both calendar and events module
 288              $tabId = array('9','16');
 289          }
 290          $query = 'SELECT 1 FROM vtiger_field WHERE tabid IN ('.  generateQuestionMarks($tabId).') AND fieldlabel=?';
 291          $result = $db->pquery($query, array($tabId,$fieldLabel));
 292          return ($db->num_rows($result) > 0 ) ? true : false;
 293      }
 294  
 295      public static function getSupportedModules() {
 296          if(empty(self::$supportedModules)) {
 297             self::$supportedModules = self::getEntityModulesList();
 298          }
 299          return self::$supportedModules;
 300      }
 301  
 302  
 303      public static function getInstanceByName($moduleName) {
 304          $moduleInstance = Vtiger_Module_Model::getInstance($moduleName);
 305          $objectProperties = get_object_vars($moduleInstance);
 306          $selfInstance = new self();
 307          foreach($objectProperties as $properName=>$propertyValue){
 308              $selfInstance->$properName = $propertyValue;
 309          }
 310          return $selfInstance;
 311      }
 312  
 313      /**
 314       * Function to get Entity module names list
 315       * @return <Array> List of Entity modules
 316       */
 317  	public static function getEntityModulesList() {
 318          $db = PearDatabase::getInstance();
 319          self::preModuleInitialize2();
 320  
 321          $presence = array(0, 2);
 322          $restrictedModules = array('Webmails', 'SMSNotifier', 'Emails', 'Integration', 'Dashboard', 'ModComments', 'vtmessages', 'vttwitter');
 323  
 324          $query = 'SELECT name FROM vtiger_tab WHERE
 325                          presence IN ('. generateQuestionMarks($presence) .')
 326                          AND isentitytype = ?
 327                          AND name NOT IN ('. generateQuestionMarks($restrictedModules) .')';
 328          $result = $db->pquery($query, array($presence, 1, $restrictedModules));
 329          $numOfRows = $db->num_rows($result);
 330  
 331          $modulesList = array();
 332          for($i=0; $i<$numOfRows; $i++) {
 333              $moduleName = $db->query_result($result, $i, 'name');
 334              $modulesList[$moduleName] = $moduleName;
 335          }
 336          // If calendar is disabled we should not show events module too
 337          // in layout editor
 338          if(!array_key_exists('Calendar', $modulesList)) {
 339              unset($modulesList['Events']);
 340          }
 341          return $modulesList;
 342      }
 343  
 344      /**
 345       * Function to check field is editable or not
 346       * @return <Boolean> true/false
 347       */
 348  	public function isSortableAllowed() {
 349          $moduleName = $this->getName();
 350          if (in_array($moduleName, array('Calendar', 'Events'))) {
 351              return false;
 352          }
 353          return true;
 354      }
 355  
 356      /**
 357       * Function to check blocks are sortable for the module
 358       * @return <Boolean> true/false
 359       */
 360  	public function isBlockSortableAllowed() {
 361          $moduleName = $this->getName();
 362          if (in_array($moduleName, array('Calendar', 'Events'))) {
 363              return false;
 364          }
 365          return true;
 366      }
 367  
 368      /**
 369       * Function to check fields are sortable for the block
 370       * @return <Boolean> true/false
 371       */
 372  	public function isFieldsSortableAllowed($blockName) {
 373          $moduleName = $this->getName();
 374          $blocksEliminatedArray = array('HelpDesk' => array('LBL_TICKET_RESOLUTION', 'LBL_COMMENTS'),
 375                                          'Faq' => array('LBL_COMMENT_INFORMATION'),
 376                                          'Calendar' => array('LBL_TASK_INFORMATION', 'LBL_DESCRIPTION_INFORMATION'),
 377                                          'Events' => array('LBL_EVENT_INFORMATION', 'LBL_REMINDER_INFORMATION', 'LBL_RECURRENCE_INFORMATION', 'LBL_RELATED_TO', 'LBL_DESCRIPTION_INFORMATION', 'LBL_INVITE_USER_BLOCK'));
 378          if (in_array($moduleName, array('Calendar', 'Events', 'HelpDesk', 'Faq'))) {
 379              if(!empty($blocksEliminatedArray[$moduleName])) {
 380                  if(in_array($blockName, $blocksEliminatedArray[$moduleName])) {
 381                      return false;
 382                  }
 383              } else {
 384                  return false;
 385              }
 386          }
 387          return true;
 388      }
 389  
 390  	public function getRelations() {
 391          if($this->relations === null) {
 392              $this->relations = Vtiger_Relation_Model::getAllRelations($this, false);
 393          }
 394  
 395          // Contacts relation-tab is turned into custom block on DetailView.
 396          if ($this->getName() == 'Calendar') {
 397              $contactsIndex = false;
 398              foreach ($this->relations as $index => $model) {
 399                  if ($model->getRelationModuleName() == 'Contacts') {
 400                      $contactsIndex = $index;
 401                      break;
 402                  }
 403              }
 404              if ($contactsIndex !== false) {
 405                  array_splice($this->relations, $contactsIndex, 1);
 406              }
 407          }
 408  
 409          return $this->relations;
 410      }
 411  }


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