[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/modules/com_vtiger_workflow/tasks/ -> VTCreateEntityTask.inc (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  require_once ('modules/com_vtiger_workflow/VTEntityCache.inc');
  11  require_once ('modules/com_vtiger_workflow/VTWorkflowUtils.php');
  12  
  13  class VTCreateEntityTask extends VTTask {
  14  
  15      public $executeImmediately = true;
  16  
  17  	public function getFieldNames() {
  18          return array('entity_type', 'reference_field', 'field_value_mapping');
  19      }
  20  
  21  	public function doTask($entity) {
  22          global $adb, $current_user;
  23          $util = new VTWorkflowUtils();
  24  
  25          $admin = $util->adminUser();
  26          $moduleName = $entity->getModuleName();
  27          $entityId = $entity->getId();
  28          $recordId = vtws_getIdComponents($entityId);
  29          $recordId = $recordId[1];
  30  
  31          $entityType = $this->entity_type;        
  32          if(!vtlib_isModuleActive($entityType)) {
  33              return;
  34          }
  35          
  36          $fieldValueMapping = array();
  37          if (!empty($this->field_value_mapping)) {
  38              $fieldValueMapping = Zend_Json::decode($this->field_value_mapping);
  39          }
  40  
  41          if (!empty($entityType) && !empty($fieldValueMapping) && count($fieldValueMapping) > 0) {
  42              require_once ('data/CRMEntity.php');
  43  
  44              $newEntity = CRMEntity::getInstance($entityType);
  45              $newEntity->mode = '';
  46              $newEntityData = VTEntityData::fromCRMEntity($newEntity);
  47              $entityModuleHandler = vtws_getModuleHandlerFromName($entityType, $current_user);
  48              $handlerMeta = $entityModuleHandler->getMeta();
  49              $ownerFields = $handlerMeta->getOwnerFields();
  50  
  51              $focus = CRMEntity::getInstance($moduleName);
  52              $focus->id = $recordId;
  53              $focus->mode = 'edit';
  54              $focus->retrieve_entity_info($recordId, $moduleName);
  55  
  56              foreach ($fieldValueMapping as $fieldInfo) {
  57                  $fieldName = $fieldInfo['fieldname'];
  58                  $referenceModule = $fieldInfo['modulename'];
  59                  $fieldType = '';
  60                  $fieldValueType = $fieldInfo['valuetype'];
  61                  $fieldValue = trim($fieldInfo['value']);
  62  
  63                  if ($fieldValueType == 'fieldname') {
  64                      if ($referenceModule == $entityType) {
  65                          $fieldValue = $newEntity->column_fields[$fieldValue];
  66                      } else {
  67                          $fieldValue = $focus->column_fields[$fieldValue];
  68                      }
  69                  } elseif ($fieldValueType == 'expression') {
  70                      require_once  'modules/com_vtiger_workflow/expression_engine/include.inc';
  71  
  72                      $parser = new VTExpressionParser(new VTExpressionSpaceFilter(new VTExpressionTokenizer($fieldValue)));
  73                      $expression = $parser->expression();
  74                      $exprEvaluater = new VTFieldExpressionEvaluater($expression);
  75                      if ($referenceModule == $entityType) {
  76                          $fieldValue = $exprEvaluater->evaluate($newEntityData);
  77                      } else {
  78                          $fieldValue = $exprEvaluater->evaluate($entity);
  79                      }
  80                  } elseif (preg_match('/([^:]+):boolean$/', $fieldValue, $match)) {
  81                      $fieldValue = $match[1];
  82                      if ($fieldValue == 'true') {
  83                          $fieldValue = '1';
  84                      } else {
  85                          $fieldValue = '0';
  86                      }
  87                  }
  88  
  89                  if (in_array($fieldName, $ownerFields) && !is_numeric($fieldValue)) {
  90                      $userId = getUserId_Ol($fieldValue);
  91                      $groupId = getGrpId($fieldValue);
  92  
  93                      if ($userId == 0 && $groupId == 0) {
  94                          $fieldValue = $focus->column_fields[$fieldName];
  95                      } else {
  96                          $fieldValue = ($userId == 0) ? $groupId : $userId;
  97                      }
  98                  }
  99  
 100                  $newEntity->column_fields[$fieldName] = $fieldValue;
 101              }
 102              $newEntity->column_fields[$this->reference_field] = $focus->id;
 103              // To handle cyclic process
 104              $newEntity->_from_workflow = true;
 105              
 106              $newEntity->save($entityType);
 107              relateEntities($focus, $moduleName, $recordId, $entityType, $newEntity->id);
 108          }
 109  
 110          $util->revertUser();
 111      }
 112  
 113  }
 114  
 115  ?>


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