[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/modules/com_vtiger_workflow/ -> VTWorkflowTemplateManager.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  class VTWorkflowTemplateManager{
  12  	public function __construct($adb){
  13          $this->adb = $adb;
  14      }
  15      
  16      /**
  17       * Create anew template instance from a workflow
  18       *
  19       * This template instance will not be saved. The save 
  20       * will have to be done explicitly.
  21       *
  22       * @param $title The title of the template
  23       * @param $workflow A workflow instance.
  24       */
  25  	public function newTemplate($title, $workflow){
  26          $adb = $this->adb;
  27          $wms = new VTWorkflowManager($adb);
  28          $str = $wms->serializeWorkflow($workflow);
  29          $template = new VTWorkflowTemplate();
  30          $template->title = $title;
  31          $template->moduleName = $workflow->moduleName;
  32          $template->template = $str;
  33          return $template;
  34      }
  35      
  36      /**
  37       * Retrieve a template given it's id
  38       *
  39       * @param $templateId The id of the template
  40       * @return The template object
  41       */
  42  	public function retrieveTemplate($templateId){
  43          $adb = $this->adb;
  44          $result = $adb->pquery('select * from com_vtiger_workflowtemplates where template_id=?', array($templateId));
  45          $it = new SqlResultIterator($adb, $result);
  46          $data = $it->current();
  47          $template = new VTWorkflowTemplate();
  48          $template->id = $templateId;
  49          $template->title = $data->title;
  50          $template->moduleName = $data->module_name;
  51          $template->template = $data->template;
  52          return $template;
  53      }
  54  
  55      /**
  56       * Create a workflow from a template
  57       *
  58       * The new workflow will also be added to the database.
  59       *
  60       * @param $template The template to use
  61       * @return A workflow object.
  62       */
  63  	public function createWorkflow($template){
  64          $adb = $this->adb;
  65          $wfm = new VTWorkflowManager($adb);
  66          return $wfm->deserializeWorkflow($template->template);
  67      }
  68  
  69      /**
  70       * Get template objects for a particular module.
  71       *
  72       * @param $moduleName The name of the module
  73       * @return An array containing template objects
  74       */
  75  	public function getTemplatesForModule($moduleName){
  76          $adb = $this->adb;
  77          $result = $adb->pquery("select * from com_vtiger_workflowtemplates where module_name=?", array($moduleName));
  78          return $this->getTemplatesForResult($result);
  79      }
  80  
  81      /**
  82       * Get all templates
  83       * 
  84       * Get all the templates as an array
  85       *
  86       * @return An array containing template objects.
  87       */
  88  	public function getTemplates(){
  89          $adb = $this->adb;
  90          $result = $adb->query("select * from com_vtiger_workflowtemplates");
  91          return $this->getTemplatesForResult($result);
  92      }
  93  
  94      /**
  95       * Save a template
  96       *
  97       * If the object is a newly created template it
  98       * will be added to the database and a field id containing 
  99       * the new id will be added to the object.
 100       *
 101       * @param $template The template object to save.
 102       */
 103  	public function saveTemplate($template){
 104          $adb = $this->adb;
 105          if(is_numeric($template->id)){//How do I check whether a member exists in php?
 106                  $templateId = $template->id;
 107                  $adb->pquery("update com_vtiger_workflowtemplates set title=?,"+
 108                                           " module_name=?, template=? where template_id=?", 
 109                                           array($template->title, $template->moduleName, 
 110                                                       $template->template, $templateId));    
 111                  return $templateId;
 112              }else{
 113                  $templateId = $adb->getUniqueID("com_vtiger_workflowtemplates");
 114                  $template->id = $templateId;
 115                  $adb->pquery("insert into com_vtiger_workflowtemplates 
 116                              (template_id, title, module_name, template) 
 117                              values (?, ?, ?, ?)", 
 118                      array($templateId, $template->title, 
 119                                  $template->moduleName, $template->template));
 120                  return $templateId;
 121              }
 122      }
 123  
 124      /**
 125       * Delete a template
 126       *
 127       * $templateId The id of the template to delete.
 128       */
 129  	public function deleteTemplate($templateId){
 130          $adb = $this->adb;
 131          $adb->pquery('delete from com_vtiger_workflowtemplates where template_id=?',
 132                                   array($templateId));
 133      }
 134      
 135      
 136      /**
 137       * Dump all the templates in vtiger into a string
 138       *
 139       * This can be used for exporting templates from one 
 140       * machine to another
 141       *
 142       * @return The string dump of the templates.
 143       */
 144  	public function dumpAllTemplates(){
 145          $adb = $this->adb;
 146          $result = $adb->query("select * from com_vtiger_workflowtemplates");
 147          $it = new SqlResultIterator($adb, $result);
 148          $arr = array();
 149          foreach($it as $row){
 150              $el = array(
 151                  'moduleName'=>$row->module_name,
 152                  'title'=>$row->title,
 153                  'template'=>$row->template
 154              );
 155              $arr[] = $el;
 156          }
 157          return Zend_Json::encode($arr);
 158      }
 159      
 160      /**
 161       * Load templates form a dumped string
 162       *
 163       * @param $str The string dump generated from dumpAllTemplates
 164       */
 165  	public function loadTemplates($str){
 166              $arr = Zend_Json::decode($str);
 167              foreach($arr as $el){
 168                  $template = new VTWorkflowTemplate();
 169                  $template->moduleName = $el['moduleName'];
 170                  $template->title = $el['title'];
 171                  $template->template = $el['template'];
 172                  $this->save($template);
 173                  $this->createWorkflow($template);
 174              }
 175      }
 176  
 177  
 178  	private function getTemplatesForResult($result){
 179          $adb = $this->adb;
 180          $it = new SqlResultIterator($adb, $result);
 181          $templates = array();
 182          foreach($it as $row){
 183              $template = new VTWorkflowTemplate();
 184              $template->id = $row->template_id;
 185              $template->title = $row->title;
 186              $tempalte->moduleName = $row->module_name;
 187              $template->template = $row->template;
 188              $templates[] = $template;
 189          }
 190          return $templates;
 191      }
 192  }
 193  
 194  class VTWorkflowTemplate{
 195      
 196  }
 197  ?>


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