[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/modules/com_vtiger_workflow/tasks/ -> VTCreateEventTask.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 ('include/Webservices/Utils.php');
  11  require_once ("include/Webservices/VtigerCRMObject.php");
  12  require_once ("include/Webservices/VtigerCRMObjectMeta.php");
  13  require_once ("include/Webservices/DataTransform.php");
  14  require_once ("include/Webservices/WebServiceError.php");
  15  require_once  'include/Webservices/ModuleTypes.php';
  16  require_once ('include/Webservices/Create.php');
  17  require_once  'include/Webservices/DescribeObject.php';
  18  require_once  'include/Webservices/WebserviceField.php';
  19  require_once  'include/Webservices/EntityMeta.php';
  20  require_once  'include/Webservices/VtigerWebserviceObject.php';
  21  
  22  require_once ("modules/Users/Users.php");
  23  
  24  class VTCreateEventTask extends VTTask{
  25      public $executeImmediately = true;
  26  
  27  	public function getFieldNames(){
  28          return array('eventType', 'eventName', 'description', 'sendNotification',
  29                                   'startTime', 'startDays', 'startDirection', 'startDatefield',
  30                                   'endTime','endDays', 'endDirection', 'endDatefield',
  31                                   'status', 'priority','recurringcheck','repeat_frequency',
  32                                   'recurringtype','calendar_repeat_limit_date',
  33                                   'mon_flag','tue_flag','wed_flag','thu_flag','fri_flag','sat_flag','sun_flag',
  34                                   'repeatMonth','repeatMonth_date','repeatMonth_daytype','repeatMonth_day', 'assigned_user_id');
  35      }
  36  
  37  	function getAdmin(){
  38          $user = Users::getActiveAdminUser();
  39          global $current_user;
  40          $this->originalUser = $current_user;
  41          $current_user = $user;
  42          return $user;
  43      }
  44  
  45  	public function doTask($entityData){
  46          if(!vtlib_isModuleActive('Calendar')) {
  47              return;
  48          }
  49          global $adb, $current_user;
  50          $userId = $entityData->get('assigned_user_id');
  51          if($userId===null){
  52              $userId = vtws_getWebserviceEntityId('Users', 1);
  53          }
  54  
  55          $moduleName = $entityData->getModuleName();
  56          $adminUser = $this->getAdmin();
  57  
  58          $startDate = $this->calculateDate($entityData, $this->startDays,
  59                                                              $this->startDirection, $this->startDatefield);
  60          $endDate = $this->calculateDate($entityData, $this->endDays,
  61                                                              $this->endDirection, $this->endDatefield);
  62  
  63          // Added to check if the user/group is active
  64          if(!empty($this->assigned_user_id)) {
  65              $userExists = $adb->pquery('SELECT 1 FROM vtiger_users WHERE id = ? AND status = ?', array($this->assigned_user_id, 'Active'));
  66              if($adb->num_rows($userExists)) {
  67                  $assignedUserId = vtws_getWebserviceEntityId('Users', $this->assigned_user_id);
  68                  $userId = $assignedUserId;
  69              } else {
  70                  $groupExist = $adb->pquery('SELECT 1 FROM vtiger_groups WHERE groupid = ?', array($this->assigned_user_id));
  71                  if($adb->num_rows($groupExist)) {
  72                      $assignedGroupId = vtws_getWebserviceEntityId('Groups', $this->assigned_user_id);
  73                      $userId = $assignedGroupId;
  74                  }
  75              }
  76          }
  77  
  78          $fields = array(
  79              'activitytype'=>$this->eventType,
  80              'description'=>$this->description,
  81              'subject'=>$this->eventName,
  82              'taskpriority'=>$this->priority,
  83              'eventstatus'=>$this->status,
  84              'assigned_user_id'=>$userId,
  85              'time_start'=>self::convertToDBFormat($this->startTime),
  86              'date_start'=> $startDate,
  87              'time_end'=>self::convertToDBFormat($this->endTime),
  88              'due_date'=>$endDate,
  89              'taskstatus'=>'',
  90              'duration_hours'=>'0'
  91          );
  92          
  93          //Setting visibility value
  94          $assignedTo = explode('x', $userId);
  95          $sharedType = Calendar_Module_Model::getSharedType($assignedTo[1]);
  96          if($sharedType == 'selectedusers' || empty($sharedType)) {
  97              $sharedType = 'public';
  98          }
  99          $fields['visibility'] = ucfirst($sharedType);
 100          
 101          $id = $entityData->getId();
 102          if($moduleName=='Contacts'){
 103              $fields['contact_id'] = $id;
 104          }else{
 105              $data = vtws_describe('Calendar', $adminUser);
 106              $fieldInfo = $data['fields'];
 107              foreach($fieldInfo as $field){
 108                  if($field['name']=='parent_id'){
 109                      $parentIdField = $field;
 110                  }
 111              }
 112              $refersTo = $parentIdField['type']['refersTo'];
 113  
 114              if(in_array($moduleName, $refersTo)){
 115                  $fields['parent_id'] = $id;
 116              }
 117          }
 118  
 119          $event = vtws_create('Events', $fields, $adminUser);
 120          $eventIdDetails = vtws_getIdComponents($event['id']);
 121          $entityIdDetails = vtws_getIdComponents($id);
 122  
 123          relateEntities(CRMEntity::getInstance($moduleName), $moduleName, $entityIdDetails[1], 'Calendar', $eventIdDetails[1]);
 124  
 125          $handler = vtws_getModuleHandlerFromName('Events', $adminUser);
 126          $meta = $handler->getMeta();
 127          $recordValues = DataTransform::sanitizeForInsert($event,$meta);
 128          list($typeId, $id) = vtws_getIdComponents($event['id']);
 129          $event = CRMEntity::getInstance('Events');
 130          $event->id = $id;
 131          $event->column_fields = $recordValues;
 132  
 133          if($this->recurringcheck && !empty($startDate) &&
 134                  ($this->calendar_repeat_limit_date)) {
 135  
 136              $resultRow = array();
 137  
 138              $resultRow['date_start'] = $startDate;
 139              $resultRow['time_start'] = self::conv12to24hour($this->startTime);
 140              $resultRow['due_date'] = $this->calendar_repeat_limit_date;
 141              $resultRow['time_end'] = self::conv12to24hour($this->endTime);
 142              $resultRow['recurringtype'] = $this->recurringtype;
 143              $resultRow['recurringfreq'] = $this->repeat_frequency;
 144  
 145              if ($this->sun_flag) {
 146                  $daysOfWeekToRepeat[] = 0;
 147              }
 148              if ($this->mon_flag) {
 149                  $daysOfWeekToRepeat[] = 1;
 150              }
 151              if ($this->tue_flag) {
 152                  $daysOfWeekToRepeat[] = 2;
 153              }
 154              if ($this->wed_flag) {
 155                  $daysOfWeekToRepeat[] = 3;
 156              }
 157              if ($this->thu_flag) {
 158                  $daysOfWeekToRepeat[] = 4;
 159              }
 160              if ($this->fri_flag) {
 161                  $daysOfWeekToRepeat[] = 5;
 162              }
 163              if ($this->sat_flag) {
 164                  $daysOfWeekToRepeat[] = 6;
 165              }
 166  
 167              if ($this->recurringtype == 'Daily' || $this->recurringtype == 'Yearly') {
 168                  $recurringInfo = $this->recurringtype;
 169              } elseif ($this->recurringtype == 'Weekly') {
 170                  if ($daysOfWeekToRepeat != null) {
 171                      $recurringInfo = $this->recurringtype . '::' . implode('::', $daysOfWeekToRepeat);
 172                  } else {
 173                      $recurringInfo = $recurringType;
 174                  }
 175              } elseif ($this->recurringtype == 'Monthly') {
 176                  $recurringInfo = $this->recurringtype . '::' . $this->repeatMonth;
 177                  if ($this->repeatMonth == 'date') {
 178                      $recurringInfo = $recurringInfo . '::' . $this->repeatMonth_date;
 179                  } else {
 180                      $recurringInfo = $recurringInfo . '::' . $this->repeatMonth_daytype . '::' . $this->repeatMonth_day;
 181                  }
 182              }
 183              $resultRow['recurringinfo'] = $recurringInfo;
 184      
 185              // Added this to relate these events to parent module.
 186              $_REQUEST['createmode'] = 'link';
 187              $_REQUEST['return_module'] = $moduleName;
 188              $_REQUEST['return_id'] = $entityIdDetails[1];
 189  
 190              $recurObj = RecurringType::fromDBRequest($resultRow);
 191  
 192              include_once  'modules/Calendar/RepeatEvents.php';
 193              Calendar_RepeatEvents::repeat($event, $recurObj);
 194  
 195              $_REQUEST['createmode'] = '';
 196  
 197          }
 198          global $current_user;
 199          $current_user = $this->originalUser;
 200      }
 201  
 202  	private function calculateDate($entityData, $days, $direction, $datefield){
 203          $baseDate = $entityData->get($datefield);
 204          if($baseDate == '') {
 205              $baseDate = date('Y-m-d');
 206          }
 207          if($days == '') {
 208              $days = 0;
 209          }
 210          preg_match('/\d\d\d\d-\d\d-\d\d/', $baseDate, $match);
 211          $baseDate = strtotime($match[0]);
 212          $date = strftime('%Y-%m-%d', $baseDate+$days*24*60*60*
 213                                           (strtolower($direction)=='before'?-1:1));
 214          return $date;
 215      }
 216  
 217      /**
 218       * To convert time_start & time_end values to db format
 219       * @param type $timeStr
 220       * @return time
 221       */
 222      static function convertToDBFormat($timeStr){
 223          $date = new DateTime();
 224          $time = Vtiger_Time_UIType::getTimeValueWithSeconds($timeStr);
 225          $dbInsertDateTime = DateTimeField::convertToDBTimeZone($date->format('Y-m-d').' '.$time);
 226          return $dbInsertDateTime->format('H:i:s');
 227      }
 228      
 229  	static function conv12to24hour($timeStr){
 230          $arr = array();
 231          preg_match('/(\d{1,2}):(\d{1,2})(am|pm)/', $timeStr, $arr);
 232          if($arr[3]=='am'){
 233              $hours = ((int)$arr[1]) % 12;
 234          }else{
 235              $hours = ((int)$arr[1]) % 12 + 12;
 236          }
 237          return str_pad($hours, 2, '0', STR_PAD_LEFT).':'.str_pad($arr[2], 2, '0', STR_PAD_LEFT);
 238      }
 239  
 240  	public function getTimeFieldList() {
 241          return array('startTime', 'endTime');
 242      }
 243  
 244  }
 245  ?>


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