[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/modules/com_vtiger_workflow/ -> VTTaskManager.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  
  11      /**
  12       * Functionality to save and retrieve Tasks from the database.
  13       */
  14      class VTTaskManager{
  15  		function __construct($adb){
  16              $this->adb = $adb;
  17          }
  18  
  19          /**
  20           * Save the task into the database.
  21           *
  22           * When a new task is saved for the first time a field is added to it called
  23           * id that stores the task id used in the database.
  24           *
  25           * @param $summary A summary of the task instance.
  26           * @param $task The task instance to save.
  27           * @return The id of the task
  28           */
  29  		public function saveTask($task){
  30              $adb = $this->adb;
  31              if(is_numeric($task->id)){//How do I check whether a member exists in php?
  32                  $taskId = $task->id;
  33                  $adb->pquery("update com_vtiger_workflowtasks set summary=?, task=? where task_id=?",
  34                              array($task->summary, serialize($task), $taskId));
  35                  return $taskId;
  36              }else{
  37                  $taskId = $adb->getUniqueID("com_vtiger_workflowtasks");
  38                  $task->id = $taskId;
  39                  $adb->pquery("insert into com_vtiger_workflowtasks
  40                              (task_id, workflow_id, summary, task)
  41                              values (?, ?, ?, ?)",
  42                      array($taskId, $task->workflowId, $task->summary, serialize($task)));
  43                  return $taskId;
  44              }
  45          }
  46  
  47  		public function deleteTask($taskId){
  48              $adb = $this->adb;
  49              $adb->pquery("delete from com_vtiger_workflowtasks where task_id=?", array($taskId));
  50          }
  51  
  52  
  53          /**
  54           * Create a new class instance
  55           */
  56  		public function createTask($taskType, $workflowId) {
  57              $taskTypeInstance = VTTaskType::getInstanceFromTaskType($taskType);
  58              $taskClass = $taskTypeInstance->get('classname');
  59              $this->requireTask($taskClass, $taskTypeInstance);
  60              $task = new $taskClass();
  61              $task->workflowId=$workflowId;
  62              $task->summary = "";
  63              $task->active=true;
  64              return $task;
  65          }
  66  
  67  
  68          /**
  69           * Retrieve a task from the database
  70           *
  71           * @param $taskId The id of the task to retrieve.
  72           * @return VTTask The retrieved task.
  73           */
  74  		public function retrieveTask($taskId){
  75              $adb = $this->adb;
  76              $result = $adb->pquery("select task from com_vtiger_workflowtasks where task_id=?", array($taskId));
  77              $data = $adb->raw_query_result_rowdata($result, 0);
  78              $task = $data["task"];
  79              $task = $this->unserializeTask($task);
  80              
  81              return $task;
  82          }
  83  
  84  
  85          /**
  86           *
  87           */
  88  		public function getTasksForWorkflow($workflowId){
  89              $adb = $this->adb;
  90              $result = $adb->pquery("select task from com_vtiger_workflowtasks
  91                                      where workflow_id=?",
  92                  array($workflowId));
  93              return $this->getTasksForResult($result);
  94          }
  95  
  96  
  97          /**
  98           *
  99           */
 100  		public function unserializeTask($str){
 101              $this->requireTask(self::taskName($str));
 102              return unserialize($str);
 103  
 104          }
 105          /**
 106           *
 107           */
 108  		function getTasks(){
 109              $adb = $this->adb;
 110              $result = $adb->query("select task from com_vtiger_workflowtasks");
 111              return $this->getTasksForResult($result);
 112          }
 113  
 114  		private function getTasksForResult($result){
 115              $adb = $this->adb;
 116              $it = new SqlResultIterator($adb, $result);
 117              $tasks = array();
 118              foreach($it as $row){
 119                  $text = $row->task;
 120  
 121                  $this->requireTask(self::taskName($text));
 122                  $tasks[] = unserialize($text);
 123              }
 124              return $tasks;
 125          }
 126  
 127  		private function taskName($serializedTask){
 128              $matches = array();
 129              preg_match ('/"([^"]+)"/', $serializedTask, $matches);
 130              return $matches[1];
 131          }
 132  
 133  		private function requireTask($taskType, $taskTypeInstance='') {
 134              if(!empty($taskTypeInstance)) {
 135                  $taskClassPath = $taskTypeInstance->get('classpath');
 136                  require_once($taskClassPath);
 137              } else {
 138                  if(!empty($taskType)) {
 139                      require_once("tasks/".$taskType.".inc");
 140                  }
 141              }
 142          }
 143  
 144  		public function retrieveTemplatePath($moduleName, $taskTypeInstance) {
 145              $taskTemplatePath = $taskTypeInstance->get('templatepath');
 146              if(!empty($taskTemplatePath)) {
 147                  return $taskTemplatePath;
 148              } else {
 149                  $taskType = $taskTypeInstance->get('classname');
 150                  return $moduleName."/taskforms/".$taskType.".tpl";
 151              }
 152          }
 153  
 154      }
 155  
 156      abstract class VTTask{
 157          public abstract function doTask($data);
 158          public abstract function getFieldNames();
 159  
 160  		public function getTimeFieldList() {
 161              return array();
 162          }
 163  
 164  		public function getContents($entity, $entityCache=false) {
 165              return $this->contents;
 166          }
 167  
 168  		public function setContents($contents) {
 169              $this->contents = $contents;
 170          }
 171  
 172  		public function hasContents($entity) {
 173              $taskContents = $this->getContents($entity);
 174              if ($taskContents) {
 175                  return true;
 176              }
 177              return false;
 178          }
 179  
 180  		public function formatTimeForTimePicker($time) {
 181              list($h, $m, $s) = explode(':', $time);
 182              $mn = str_pad($m - $m % 15, 2, 0, STR_PAD_LEFT);
 183              $AM_PM = array('am', 'pm');
 184              return str_pad(($h%12), 2, 0, STR_PAD_LEFT).':'.$mn.$AM_PM[($h/12)%2];
 185          }
 186  
 187  		public function getMetaVariables() {
 188              return array(
 189                  'Current Date' => '(general : (__VtigerMeta__) date) ($_DATE_FORMAT_)',
 190                  'Current Time' => '(general : (__VtigerMeta__) time)',
 191                  'System Timezone' => '(general : (__VtigerMeta__) dbtimezone)',
 192                  'User Timezone' => '(general : (__VtigerMeta__) usertimezone)',
 193                  'CRM Detail View URL' => '(general : (__VtigerMeta__) crmdetailviewurl)',
 194                  'Portal Detail View URL' => '(general : (__VtigerMeta__) portaldetailviewurl)',
 195                  'Site Url' => '(general : (__VtigerMeta__) siteurl)',
 196                  'Portal Url' => '(general : (__VtigerMeta__) portalurl)',
 197                  'Record Id' => '(general : (__VtigerMeta__) recordId)',
 198                  'LBL_HELPDESK_SUPPORT_NAME' => '(general : (__VtigerMeta__) supportName)',
 199                  'LBL_HELPDESK_SUPPORT_EMAILID' => '(general : (__VtigerMeta__) supportEmailid)',
 200              );
 201          }
 202  
 203      }
 204  
 205  class VTTaskType {
 206      var $data;
 207  
 208  	public function get($key) {
 209          return $this->data[$key];
 210      }
 211  
 212  	public function set($key, $value) {
 213          $this->data[$key] = $value;
 214          return $this;
 215      }
 216  
 217  	public function setData($valueMap) {
 218          $this->data = $valueMap;
 219          return $this;
 220      }
 221  
 222  	public static function getInstance($values) {
 223          $instance = new self();
 224          return $instance->setData($values);
 225      }
 226  
 227  	public static function registerTaskType($taskType) {
 228          $adb = PearDatabase::getInstance();
 229          if (!Vtiger_Utils::CheckTable('com_vtiger_workflow_tasktypes')) {
 230              Vtiger_Utils::CreateTable(
 231                      'com_vtiger_workflow_tasktypes',
 232                      "(id int(11) NOTNULL,
 233                          tasktypename varchar(255) NOTNULL,
 234                          label varchar(255),
 235                          classname varchar(255),
 236                          classpath varchar(255),
 237                          templatepath varchar(255),
 238                          modules text(500),
 239                          sourcemodule varchar(255)) ENGINE=InnoDB DEFAULT CHARSET=utf8",
 240                      true);
 241          }
 242  
 243          $modules = Zend_Json::encode($taskType['modules']);
 244  
 245          $taskTypeId = $adb->getUniqueID("com_vtiger_workflow_tasktypes");
 246          $taskType['id'] = $taskTypeId;
 247  
 248          $adb->pquery("INSERT INTO com_vtiger_workflow_tasktypes
 249                                      (id, tasktypename, label, classname, classpath, templatepath, modules, sourcemodule)
 250                                      values (?,?,?,?,?,?,?,?)",
 251                  array($taskTypeId, $taskType['name'], $taskType['label'], $taskType['classname'],  $taskType['classpath'], $taskType['templatepath'], $modules, $taskType['sourcemodule']));
 252  
 253  
 254  
 255      }
 256  
 257  	public static function getAll($moduleName='') {
 258          $adb = PearDatabase::getInstance();
 259  
 260          $result = $adb->pquery("SELECT * FROM com_vtiger_workflow_tasktypes",array());
 261          $numrows = $adb->num_rows($result);
 262          for ($i = 0; $i < $numrows; $i++) {
 263              $rawData = $adb->raw_query_result_rowdata($result,$i);
 264              $taskName = $rawData['tasktypename'];
 265              $moduleslist = $rawData['modules'];
 266              $sourceModule = $rawData['sourcemodule'];
 267              $modules = Zend_Json::decode($moduleslist);
 268              $includeModules = $modules['include'];
 269              $excludeModules = $modules['exclude'];
 270  
 271              if(!empty ($sourceModule)) {
 272                  if(getTabid($sourceModule) == null || !vtlib_isModuleActive($sourceModule)) {
 273                      continue;
 274                  }
 275              }
 276  
 277              if(empty($includeModules) && empty($excludeModules)) {
 278                  $taskTypeInstances[$taskName] = self::getInstance($rawData);
 279                  continue;
 280              } elseif(!empty($includeModules)) {
 281                  if(in_array($moduleName, $includeModules)) {
 282                      $taskTypeInstances[$taskName] = self::getInstance($rawData);
 283                  }
 284                  continue;
 285              } elseif(!empty($excludeModules)) {
 286                  if(!(in_array($moduleName, $excludeModules))) {
 287                      $taskTypeInstances[$taskName] = self::getInstance($rawData);
 288                  }
 289                  continue;
 290              }
 291          }
 292          return $taskTypeInstances;
 293      }
 294  
 295  	public static function getInstanceFromTaskType($taskType) {
 296          $adb = PearDatabase::getInstance();
 297  
 298          $result = $adb->pquery("SELECT * FROM com_vtiger_workflow_tasktypes where tasktypename=?",array($taskType));
 299          $taskTypes['name'] = $adb->query_result($result, 0, 'tasktypename');
 300          $taskTypes['label'] = $adb->query_result($result, 0, 'label');
 301          $taskTypes['classname'] = $adb->query_result($result, 0, 'classname');
 302          $taskTypes['classpath'] = $adb->query_result($result, 0, 'classpath');
 303          $taskTypes['templatepath'] = $adb->query_result($result, 0, 'templatepath');
 304          $taskTypes['sourcemodule'] = $adb->query_result($result, 0, 'sourcemodule');
 305  
 306          $taskDetails = self::getInstance($taskTypes);
 307          return $taskDetails;
 308      }
 309  }
 310  
 311  ?>


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