[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/modules/Settings/Workflows/models/ -> Record.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  
  11  /*
  12   * Workflow Record Model Class
  13   */
  14  require_once  'modules/com_vtiger_workflow/include.inc';
  15  require_once  'modules/com_vtiger_workflow/expression_engine/VTExpressionsManager.inc';
  16  
  17  class Settings_Workflows_Record_Model extends Settings_Vtiger_Record_Model {
  18  
  19  	public function getId() {
  20          return $this->get('workflow_id');
  21      }
  22  
  23  	public function getName() {
  24          return $this->get('summary');
  25      }
  26  
  27  	public function get($key) {
  28  //        if($key == 'execution_condition') {
  29  //            $executionCondition = parent::get($key);
  30  //            $executionConditionAsLabel = Settings_Workflows_Module_Model::$triggerTypes[$executionCondition];
  31  //            return Vtiger_Language_Handler::getTranslatedString($executionConditionAsLabel, 'Settings:Workflows');
  32  //        }
  33  //        if($key == 'module_name') {
  34  //            $moduleName = parent::get($key);
  35  //            return Vtiger_Language_Handler::getTranslatedString($moduleName, $moduleName);
  36  //        }
  37          return parent::get($key);
  38      }
  39  
  40  	public function getEditViewUrl() {
  41          return 'index.php?module=Workflows&parent=Settings&view=Edit&record='.$this->getId();
  42      }
  43  
  44  	public function getTasksListUrl() {
  45          return 'index.php?module=Workflows&parent=Settings&view=TasksList&record='.$this->getId();
  46      }
  47  
  48  	public function getAddTaskUrl() {
  49          return 'index.php?module=Workflows&parent=Settings&view=EditTask&for_workflow='.$this->getId();
  50      }
  51  
  52  	protected function setWorkflowObject($wf) {
  53          $this->workflow_object = $wf;
  54          return $this;
  55      }
  56  
  57  	public function getWorkflowObject() {
  58          return $this->workflow_object;
  59      }
  60  
  61  	public function getModule() {
  62          return $this->module;
  63      }
  64  
  65  	public function setModule($moduleName) {
  66          $this->module = Vtiger_Module_Model::getInstance($moduleName);
  67          return $this;
  68      }
  69  
  70  	public function getTasks($active=false) {
  71          return Settings_Workflows_TaskRecord_Model::getAllForWorkflow($this, $active);
  72      }
  73  
  74  	public function getTaskTypes() {
  75          return Settings_Workflows_TaskType_Model::getAllForModule($this->getModule());
  76      }
  77  
  78  	public function isDefault() {
  79          $wf = $this->getWorkflowObject();
  80          if($wf->defaultworkflow == 1) {
  81              return true;
  82          }
  83          return false;
  84      }
  85  
  86  	public function save() {
  87          $db = PearDatabase::getInstance();
  88          $wm = new VTWorkflowManager($db);
  89  
  90          $wf = $this->getWorkflowObject();
  91          $wf->description = $this->get('summary');
  92          $wf->test = Zend_Json::encode($this->get('conditions'));
  93          $wf->moduleName = $this->get('module_name');
  94          $wf->executionCondition = $this->get('execution_condition');
  95          $wf->filtersavedinnew = $this->get('filtersavedinnew');
  96          $wf->schtypeid = $this->get('schtypeid');
  97          $wf->schtime = $this->get('schtime');
  98          $wf->schdayofmonth = $this->get('schdayofmonth');
  99          $wf->schdayofweek = $this->get('schdayofweek');
 100          $wf->schmonth = $this->get('schmonth');
 101          $wf->schmonth = $this->get('schmonth');
 102          $wf->schannualdates = $this->get('schannualdates');
 103          $wf->nexttrigger_time = $this->get('nexttrigger_time');
 104          $wm->save($wf);
 105  
 106          $this->set('workflow_id', $wf->id);
 107      }
 108  
 109  	public function delete() {
 110          $db = PearDatabase::getInstance();
 111          $wm = new VTWorkflowManager($db);
 112          $wm->delete($this->getId());
 113      }
 114  
 115      /**
 116       * Functions returns the Custom Entity Methods that are supported for a module
 117       * @return <Array>
 118       */
 119  	public function getEntityMethods() {
 120          $db = PearDatabase::getInstance();
 121          $emm = new VTEntityMethodManager($db);
 122          $methodNames = $emm->methodsForModule($this->get('module_name'));
 123          return $methodNames;
 124      }
 125  
 126      /**
 127       * Function to get the list view actions for the record
 128       * @return <Array> - Associate array of Vtiger_Link_Model instances
 129       */
 130  	public function getRecordLinks() {
 131  
 132          $links = array();
 133  
 134          $recordLinks = array(
 135              array(
 136                  'linktype' => 'LISTVIEWRECORD',
 137                  'linklabel' => 'LBL_EDIT_RECORD',
 138                  'linkurl' => $this->getEditViewUrl(),
 139                  'linkicon' => 'icon-pencil'
 140              ),
 141              array(
 142                  'linktype' => 'LISTVIEWRECORD',
 143                  'linklabel' => 'LBL_DELETE_RECORD',
 144                  'linkurl' => 'javascript:Vtiger_List_Js.deleteRecord('.$this->getId().');',
 145                  'linkicon' => 'icon-trash'
 146              )
 147          );
 148          foreach($recordLinks as $recordLink) {
 149              $links[] = Vtiger_Link_Model::getInstanceFromValues($recordLink);
 150          }
 151  
 152          return $links;
 153      }
 154  
 155  	public static function getInstance($workflowId) {
 156          $db = PearDatabase::getInstance();
 157          $wm = new VTWorkflowManager($db);
 158          $wf = $wm->retrieve($workflowId);
 159          return self::getInstanceFromWorkflowObject($wf);
 160      }
 161  
 162  	public static function getCleanInstance($moduleName) {
 163          $db = PearDatabase::getInstance();
 164          $wm = new VTWorkflowManager($db);
 165          $wf = $wm->newWorkflow($moduleName);
 166          $wf->filtersavedinnew = 6;
 167          return self::getInstanceFromWorkflowObject($wf);
 168      }
 169  
 170  	public static function getInstanceFromWorkflowObject($wf) {
 171          $workflowModel = new self();
 172  
 173          $workflowModel->set('summary', $wf->description);
 174          $workflowModel->set('conditions', Zend_Json::decode($wf->test));
 175          $workflowModel->set('execution_condition', $wf->executionCondition);
 176          $workflowModel->set('module_name', $wf->moduleName);
 177          $workflowModel->set('workflow_id', $wf->id);
 178          $workflowModel->set('filtersavedinnew', $wf->filtersavedinnew);
 179          $workflowModel->setWorkflowObject($wf);
 180          $workflowModel->setModule($wf->moduleName);
 181          return $workflowModel;
 182      }
 183  
 184  	function executionConditionAsLabel($executionCondition=null){
 185          if($executionCondition == null) {
 186              $executionCondition = $this->get('execution_condition');
 187          }
 188          $arr = array('ON_FIRST_SAVE', 'ONCE', 'ON_EVERY_SAVE', 'ON_MODIFY', '', 'ON_SCHEDULE', 'MANUAL');
 189          return $arr[$executionCondition-1];
 190      }
 191  
 192      /**
 193       * Function to get the count of active workflows
 194       * @return <Integer> count of acive workflows
 195       */
 196      public static function getActiveCount() {
 197  
 198          $db = PearDatabase::getInstance();
 199          vimport('~~/modules/com_vtiger_workflow/VTTaskManager.inc');
 200          $taskManager = new VTTaskManager($db);
 201          $taskList = $taskManager->getTasks();
 202  
 203          $examinedIdList = array();
 204          foreach($taskList as $taskDetails) {
 205              $workFlowId = $taskDetails->workflowId;
 206              if(in_array($workFlowId,$examinedIdList)) {
 207                  continue;
 208              }
 209              if($taskDetails->active) {
 210                  array_push($examinedIdList,$workFlowId);
 211              }
 212          }
 213          return count($examinedIdList);
 214      }
 215  
 216  	function isFilterSavedInNew() {
 217          $wf = $this->getWorkflowObject();
 218          if($wf->filtersavedinnew == '6') {
 219              return true;
 220          }
 221          return false;
 222      }
 223      /**
 224       * Functions transforms workflow filter to advanced filter
 225       * @return <Array>
 226       */
 227  	function transformToAdvancedFilterCondition() {
 228          $conditions = $this->get('conditions');
 229          $transformedConditions = array();
 230  
 231          if(!empty($conditions)) {
 232              foreach($conditions as $index => $info) {
 233                  if(!($info['groupid'])) {
 234                      $firstGroup[] = array('columnname' => $info['fieldname'], 'comparator' => $info['operation'], 'value' => $info['value'],
 235                          'column_condition' => $info['joincondition'], 'valuetype' => $info['valuetype'], 'groupid' => $info['groupid']);
 236                  } else {
 237                      $secondGroup[] = array('columnname' => $info['fieldname'], 'comparator' => $info['operation'], 'value' => $info['value'],
 238                          'column_condition' => $info['joincondition'], 'valuetype' => $info['valuetype'], 'groupid' => $info['groupid']);
 239                  }
 240              }
 241          }
 242          $transformedConditions[1] = array('columns'=>$firstGroup);
 243          $transformedConditions[2] = array('columns'=>$secondGroup);
 244          return $transformedConditions;
 245      }
 246  
 247      /**
 248       * Function returns valuetype of the field filter
 249       * @return <String>
 250       */
 251  	function getFieldFilterValueType($fieldname) {
 252          $conditions = $this->get('conditions');
 253          if(!empty($conditions) && is_array($conditions)) {
 254              foreach($conditions as $filter) {
 255                  if($fieldname == $filter['fieldname']) {
 256                      return $filter['valuetype'];
 257                  }
 258              }
 259          }
 260          return false;
 261      }
 262  
 263      /**
 264       * Function transforms Advance filter to workflow conditions
 265       */
 266  	function transformAdvanceFilterToWorkFlowFilter() {
 267          $conditions = $this->get('conditions');
 268          $wfCondition = array();
 269  
 270          if(!empty($conditions)) {
 271              foreach($conditions as $index => $condition) {
 272                  $columns = $condition['columns'];
 273                  if($index == '1' && empty($columns)) {
 274                      $wfCondition[] = array('fieldname'=>'', 'operation'=>'', 'value'=>'', 'valuetype'=>'',
 275                          'joincondition'=>'', 'groupid'=>'0');
 276                  }
 277                  if(!empty($columns) && is_array($columns)) {
 278                      foreach($columns as $column) {
 279                          $wfCondition[] = array('fieldname'=>$column['columnname'], 'operation'=>$column['comparator'],
 280                              'value'=>$column['value'], 'valuetype'=>$column['valuetype'], 'joincondition'=>$column['column_condition'],
 281                              'groupjoin'=>$condition['condition'], 'groupid'=>$column['groupid']);
 282                      }
 283                  }
 284              }
 285          }
 286          $this->set('conditions', $wfCondition);
 287      }
 288  
 289  
 290      /**
 291       * Function returns all the related modules for workflows create entity task
 292       * @return <JSON>
 293       */
 294  	public function getDependentModules() {
 295          $db = PearDatabase::getInstance();
 296          $moduleName = $this->getModule()->getName();
 297  
 298          $result = $db->pquery("SELECT fieldname, tabid, typeofdata, vtiger_ws_referencetype.type as reference_module FROM vtiger_field
 299                                  INNER JOIN vtiger_ws_fieldtype ON vtiger_field.uitype = vtiger_ws_fieldtype.uitype
 300                                  INNER JOIN vtiger_ws_referencetype ON vtiger_ws_fieldtype.fieldtypeid = vtiger_ws_referencetype.fieldtypeid
 301                              UNION
 302                              SELECT fieldname, tabid, typeofdata, relmodule as reference_module FROM vtiger_field
 303                                  INNER JOIN vtiger_fieldmodulerel ON vtiger_field.fieldid = vtiger_fieldmodulerel.fieldid", array());
 304  
 305          $noOfFields = $db->num_rows($result);
 306  
 307          $dependentFields = array();
 308          // List of modules which will not be supported by 'Create Entity' workflow task
 309          $filterModules = array('Invoice', 'Quotes', 'SalesOrder', 'PurchaseOrder', 'Emails', 'Calendar', 'Events', 'Accounts');
 310          $skipFieldsList = array();
 311          for ($i = 0; $i < $noOfFields; ++$i) {
 312              $tabId = $db->query_result($result, $i, 'tabid');
 313              $fieldName = $db->query_result($result, $i, 'fieldname');
 314              $typeOfData = $db->query_result($result, $i, 'typeofdata');
 315              $referenceModule = $db->query_result($result, $i, 'reference_module');
 316              $tabModuleName = getTabModuleName($tabId);
 317              if (in_array($tabModuleName, $filterModules))
 318                  continue;
 319              if ($referenceModule == $moduleName && $tabModuleName != $moduleName) {
 320                  if(!vtlib_isModuleActive($tabModuleName))continue;
 321                  $dependentFields[$tabModuleName] = array('fieldname' => $fieldName, 'modulelabel' => getTranslatedString($tabModuleName, $tabModuleName));
 322              } else {
 323                  $dataTypeInfo = explode('~', $typeOfData);
 324                  if ($dataTypeInfo[1] == 'M') { // If the current reference field is mandatory
 325                      $skipFieldsList[$tabModuleName] = array('fieldname' => $fieldName);
 326                  }
 327              }
 328          }
 329          foreach ($skipFieldsList as $tabModuleName => $fieldInfo) {
 330              $dependentFieldInfo = $dependentFields[$tabModuleName];
 331              if ($dependentFieldInfo['fieldname'] != $fieldInfo['fieldname']) {
 332                  unset($dependentFields[$tabModuleName]);
 333              }
 334          }
 335  
 336          return $dependentFields;
 337      }
 338  
 339      /**
 340       * Function to get reference field name
 341       * @param <String> $relatedModule
 342       * @return <String> fieldname
 343       */
 344  	public function getReferenceFieldName($relatedModule) {
 345          if ($relatedModule) {
 346              $db = PearDatabase::getInstance();
 347  
 348              $relatedModuleModel = Vtiger_Module_Model::getInstance($relatedModule);
 349              $referenceFieldsList = $relatedModuleModel->getFieldsByType('reference');
 350  
 351              foreach ($referenceFieldsList as $fieldName => $fieldModel) {
 352                  if (in_array($this->getModule()->getName(), $fieldModel->getReferenceList())) {
 353                      return $fieldName;
 354                  }
 355              }
 356          }
 357          return false;
 358      }
 359  	public function updateNextTriggerTime() {
 360          $db = PearDatabase::getInstance();
 361          $wm = new VTWorkflowManager($db);
 362          $wf = $this->getWorkflowObject();
 363          $wm->updateNexTriggerTime($wf);
 364      }
 365  }


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