PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/app/protected/modules/workflows/models/Workflow.php

https://bitbucket.org/zurmo/zurmo/
PHP | 499 lines | 231 code | 59 blank | 209 comment | 9 complexity | a6176f4767430cf2b17f12d03ed38570 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, GPL-2.0, LGPL-3.0, LGPL-2.1, BSD-2-Clause
  1. <?php
  2. /*********************************************************************************
  3. * Zurmo is a customer relationship management program developed by
  4. * Zurmo, Inc. Copyright (C) 2015 Zurmo Inc.
  5. *
  6. * Zurmo is free software; you can redistribute it and/or modify it under
  7. * the terms of the GNU Affero General Public License version 3 as published by the
  8. * Free Software Foundation with the addition of the following permission added
  9. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  10. * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
  11. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  12. *
  13. * Zurmo is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  15. * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License along with
  19. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  20. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301 USA.
  22. *
  23. * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
  24. * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
  25. *
  26. * The interactive user interfaces in original and modified versions
  27. * of this program must display Appropriate Legal Notices, as required under
  28. * Section 5 of the GNU Affero General Public License version 3.
  29. *
  30. * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
  31. * these Appropriate Legal Notices must retain the display of the Zurmo
  32. * logo and Zurmo copyright notice. If the display of the logo is not reasonably
  33. * feasible for technical reasons, the Appropriate Legal Notices must display the words
  34. * "Copyright Zurmo Inc. 2015. All rights reserved".
  35. ********************************************************************************/
  36. /**
  37. * Class for interacting with Workflow definitions. Gets information from either a SavedWorkflow or via a POST.
  38. * Contains information about how a workflow should be constructed including how it looks in the user interface
  39. * when run. The components of a workflow are a time trigger, triggers, actions, and email messages
  40. *
  41. * There are 2 different types of workflows: TYPE_ON_SAVE and TYPE_BY_TIME
  42. */
  43. class Workflow extends CComponent
  44. {
  45. const TYPE_ON_SAVE = 'OnSave';
  46. const TYPE_BY_TIME = 'ByTime';
  47. const TRIGGER_ON_NEW = 'New';
  48. const TRIGGER_ON_NEW_AND_EXISTING = 'NewAndExisting';
  49. const TRIGGER_ON_EXISTING = 'Existing';
  50. private $description;
  51. /**
  52. * Id of the saved workflow if it has already been saved
  53. * @var integer
  54. */
  55. private $id;
  56. /**
  57. * If the workflow is active or not, if not it will not be fired during processing
  58. * @var boolean
  59. */
  60. private $isActive;
  61. /**
  62. * @var string
  63. */
  64. private $moduleClassName;
  65. /**
  66. * @var string
  67. */
  68. private $name;
  69. /**
  70. * The firing order of workflow when processing.
  71. * @var integer
  72. */
  73. private $order;
  74. /**
  75. * Workflows can be fired either only when a new model exists, only an existing model, or both cases
  76. * @var string
  77. */
  78. private $triggerOn;
  79. /**
  80. * TYPE_ON_SAVE or TYPE_BY_TIME
  81. * @var string
  82. */
  83. private $type;
  84. /**
  85. * @var string
  86. */
  87. private $triggersStructure;
  88. /**
  89. * Defines the attribute that the time trigger fires on.
  90. * @var string
  91. */
  92. private $timeTriggerAttribute;
  93. /**
  94. * @var TimeTriggerForWorkflowForm, used when the type is TYPE_BY_TIME
  95. */
  96. private $timeTrigger;
  97. /**
  98. * @var array of TriggerForWorkflowForm models
  99. */
  100. private $triggers = array();
  101. /**
  102. * @var array of ActionForWorkflowForm models
  103. */
  104. private $actions = array();
  105. /**
  106. * @var array of EmailMessageForWorkflowForm models
  107. */
  108. private $emailMessages = array();
  109. /**
  110. * @var bool
  111. */
  112. private $timeTriggerRequireChangeToProcess = true;
  113. /**
  114. * @return array
  115. */
  116. public static function getTypeDropDownArray()
  117. {
  118. return array(self::TYPE_ON_SAVE => Zurmo::t('WorkflowsModule', 'On-Save'),
  119. self::TYPE_BY_TIME => Zurmo::t('WorkflowsModule', 'Time-Based'));
  120. }
  121. /**
  122. * Based on the current user, return the workflow supported modules and their display labels. Only include modules
  123. * that the user has a right to access.
  124. * @return array of module class names and display labels.
  125. */
  126. public static function getWorkflowSupportedModulesAndLabelsForCurrentUser()
  127. {
  128. $moduleClassNamesAndLabels = array();
  129. $modules = Module::getModuleObjects();
  130. foreach (self::getWorkflowSupportedModulesClassNamesCurrentUserHasAccessTo() as $moduleClassName)
  131. {
  132. if ($moduleClassName::getStateMetadataAdapterClassName() != null)
  133. {
  134. $workflowRules = WorkflowRules::makeByModuleClassName($moduleClassName);
  135. $label = $workflowRules->getVariableStateModuleLabel(Yii::app()->user->userModel);
  136. }
  137. else
  138. {
  139. $label = $moduleClassName::getModuleLabelByTypeAndLanguage('Plural');
  140. }
  141. if ($label != null)
  142. {
  143. $moduleClassNamesAndLabels[$moduleClassName] = $label;
  144. }
  145. }
  146. return $moduleClassNamesAndLabels;
  147. }
  148. /**
  149. * @return array
  150. */
  151. public static function getWorkflowSupportedModulesClassNamesCurrentUserHasAccessTo()
  152. {
  153. $moduleClassNames = array();
  154. $modules = Module::getModuleObjects();
  155. foreach ($modules as $module)
  156. {
  157. if ($module::canHaveWorkflow())
  158. {
  159. if (WorkflowSecurityUtil::canCurrentUserCanAccessModule(get_class($module)))
  160. {
  161. $moduleClassNames[] = get_class($module);
  162. }
  163. }
  164. }
  165. return $moduleClassNames;
  166. }
  167. /**
  168. * @return string
  169. */
  170. public function __toString()
  171. {
  172. if (trim($this->name) == '')
  173. {
  174. return Yii::t('Core', '(Unnamed)');
  175. }
  176. return $this->name;
  177. }
  178. /**
  179. * @return mixed
  180. */
  181. public function getDescription()
  182. {
  183. return $this->description;
  184. }
  185. /**
  186. * @param $description
  187. */
  188. public function setDescription($description)
  189. {
  190. assert('is_string($description)');
  191. $this->description = $description;
  192. }
  193. /**
  194. * @return bool
  195. */
  196. public function getIsActive()
  197. {
  198. return $this->isActive;
  199. }
  200. /**
  201. * @param bool $isActive
  202. */
  203. public function setIsActive($isActive)
  204. {
  205. assert('is_bool($isActive)');
  206. $this->isActive = $isActive;
  207. }
  208. /**
  209. * @return string
  210. */
  211. public function getModuleClassName()
  212. {
  213. return $this->moduleClassName;
  214. }
  215. /**
  216. * @param $moduleClassName
  217. */
  218. public function setModuleClassName($moduleClassName)
  219. {
  220. assert('is_string($moduleClassName)');
  221. $this->moduleClassName = $moduleClassName;
  222. }
  223. /**
  224. * @return int
  225. */
  226. public function getOrder()
  227. {
  228. return $this->order;
  229. }
  230. /**
  231. * @param integer $order
  232. */
  233. public function setOrder($order)
  234. {
  235. assert('is_int($order)');
  236. $this->order = $order;
  237. }
  238. /**
  239. * @return string
  240. */
  241. public function getTriggerOn()
  242. {
  243. return $this->triggerOn;
  244. }
  245. /**
  246. * @param string $triggerOn
  247. */
  248. public function setTriggerOn($triggerOn)
  249. {
  250. assert('$triggerOn == self::TRIGGER_ON_NEW || $triggerOn == self::TRIGGER_ON_NEW_AND_EXISTING ||
  251. $triggerOn == self::TRIGGER_ON_EXISTING');
  252. $this->triggerOn = $triggerOn;
  253. }
  254. /**
  255. * @param string $triggersStructure
  256. */
  257. public function setTriggersStructure($triggersStructure)
  258. {
  259. assert('is_string($triggersStructure)');
  260. $this->triggersStructure = $triggersStructure;
  261. }
  262. /**
  263. * @return string
  264. */
  265. public function getTriggersStructure()
  266. {
  267. return $this->triggersStructure;
  268. }
  269. /**
  270. * @return int
  271. */
  272. public function getId()
  273. {
  274. return $this->id;
  275. }
  276. /**
  277. * @param integer $id
  278. */
  279. public function setId($id)
  280. {
  281. assert('is_int($id)');
  282. $this->id = $id;
  283. }
  284. /**
  285. * @return string
  286. */
  287. public function getName()
  288. {
  289. return $this->name;
  290. }
  291. /**
  292. * @param $name
  293. */
  294. public function setName($name)
  295. {
  296. assert('is_string($name)');
  297. $this->name = $name;
  298. }
  299. /**
  300. * @return string
  301. */
  302. public function getType()
  303. {
  304. return $this->type;
  305. }
  306. /**
  307. * @param $type
  308. */
  309. public function setType($type)
  310. {
  311. assert('$type == self::TYPE_ON_SAVE || $type == self::TYPE_BY_TIME');
  312. $this->type = $type;
  313. }
  314. /**
  315. * @return bool
  316. */
  317. public function isNew()
  318. {
  319. if ($this->id > 0)
  320. {
  321. return false;
  322. }
  323. return true;
  324. }
  325. /**
  326. * @param string $timeTriggerAttribute
  327. */
  328. public function setTimeTriggerAttribute($timeTriggerAttribute)
  329. {
  330. assert('is_string($timeTriggerAttribute)');
  331. $this->timeTriggerAttribute = $timeTriggerAttribute;
  332. }
  333. /**
  334. * @return string
  335. */
  336. public function getTimeTriggerAttribute()
  337. {
  338. return $this->timeTriggerAttribute;
  339. }
  340. /**
  341. * @return TimeTriggerForWorkflowForm
  342. */
  343. public function getTimeTrigger()
  344. {
  345. return $this->timeTrigger;
  346. }
  347. /**
  348. * @param TimeTriggerForWorkflowForm $timeTrigger
  349. */
  350. public function setTimeTrigger(TimeTriggerForWorkflowForm $timeTrigger)
  351. {
  352. $this->timeTrigger = $timeTrigger;
  353. }
  354. /**
  355. * Resets timeTrigger to null
  356. */
  357. public function removeTimeTrigger()
  358. {
  359. $this->timeTrigger = null;
  360. }
  361. /**
  362. * @return array
  363. */
  364. public function getTriggers()
  365. {
  366. return $this->triggers;
  367. }
  368. /**
  369. * @param TriggerForWorkflowForm $trigger
  370. */
  371. public function addTrigger(TriggerForWorkflowForm $trigger)
  372. {
  373. $this->triggers[] = $trigger;
  374. }
  375. /**
  376. * Resets triggers to an empty array
  377. */
  378. public function removeAllTriggers()
  379. {
  380. $this->triggers = array();
  381. }
  382. /**
  383. * @return array
  384. */
  385. public function getActions()
  386. {
  387. return $this->actions;
  388. }
  389. /**
  390. * @param ActionForWorkflowForm $action
  391. */
  392. public function addAction(ActionForWorkflowForm $action)
  393. {
  394. $this->actions[] = $action;
  395. }
  396. /**
  397. * Resets actions to an empty array
  398. */
  399. public function removeAllActions()
  400. {
  401. $this->actions = array();
  402. }
  403. /**
  404. * @return array
  405. */
  406. public function getEmailMessages()
  407. {
  408. return $this->emailMessages;
  409. }
  410. /**
  411. * @param EmailMessageForWorkflowForm $emailMessage
  412. */
  413. public function addEmailMessage(EmailMessageForWorkflowForm $emailMessage)
  414. {
  415. $this->emailMessages[] = $emailMessage;
  416. }
  417. /**
  418. * Resets emailMessages to an empty array
  419. */
  420. public function removeAllEmailMessages()
  421. {
  422. $this->emailMessages = array();
  423. }
  424. /**
  425. * @return bool
  426. */
  427. public function doesTimeTriggerRequireChangeToProcess()
  428. {
  429. return $this->timeTriggerRequireChangeToProcess;
  430. }
  431. /**
  432. * When processing ByTime workflow in @see ByTimeWorkflowInQueueJob this should be changed to false
  433. * so the time trigger can be evaluated correctly.
  434. */
  435. public function setTimeTriggerRequireChangeToProcessToFalse()
  436. {
  437. $this->timeTriggerRequireChangeToProcess = false;
  438. }
  439. }
  440. ?>