1: <?php
2: /*****************************************************************************************
3: * X2Engine Open Source Edition is a customer relationship management program developed by
4: * X2Engine, Inc. Copyright (C) 2011-2016 X2Engine Inc.
5: *
6: * This program 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 X2ENGINE, X2ENGINE DISCLAIMS THE WARRANTY
11: * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12: *
13: * This program 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 X2Engine, Inc. P.O. Box 66752, Scotts Valley,
24: * California 95067, USA. or at email address [email protected].
25: *
26: * The interactive user interfaces in modified source and object code 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 "Powered by
32: * X2Engine" logo. If the display of the logo is not reasonably feasible for
33: * technical reasons, the Appropriate Legal Notices must display the words
34: * "Powered by X2Engine".
35: *****************************************************************************************/
36:
37: /**
38: * WorkflowBehavior class file.
39: * Manages workflow operations for the owner record.
40: *
41: * @package application.components
42: */
43: class WorkflowBehavior extends CActiveRecordBehavior {
44:
45: protected $_workflows;
46:
47:
48: /**
49: * Responds to {@link CActiveRecord::onAfterDelete} event.
50: *
51: *
52: * @param CModelEvent $event event parameter
53: */
54: public function afterDelete($event) {
55: // $this->clearTags();
56: }
57:
58: /**
59: *
60: * @param integer $workflowId
61: * @param integer $stageNumber
62: * @return
63: */
64: public function startStage($workflowId,$stageNumber) {
65:
66: }
67:
68: /**
69: *
70: * @param integer $workflowId
71: * @param integer $stageNumber
72: * @return
73: */
74: public function completeStage($workflowId,$stageNumber) {
75:
76: }
77:
78: /**
79: *
80: * @param integer $workflowId
81: * @param integer $stageNumber
82: * @return
83: */
84: public function revertStage($workflowId,$stageNumber) {
85:
86: }
87:
88:
89: public function getWorkflowStatus($workflowId) {
90:
91: $workflowStatus = array(
92: 'id'=>$workflowId,
93: 'stages'=>array(),
94: 'started'=>false,
95: 'completed'=>false
96: );
97:
98: $workflowStages = CActiveRecord::model('WorkflowStage')->findAllByAttributes(array('workflowId'=>$workflowId),new CDbCriteria(array('order'=>'id ASC')));
99:
100: // $workflowStatus[] = $workflowId;
101: foreach($workflowStages as &$stage) { // load all WorkflowStage names into workflowStatus
102: $workflowStatus['stages'][$stage->stageNumber] = array(
103: 'name'=>$stage->name,
104: 'requirePrevious'=>$stage->requirePrevious,
105: 'roles'=>$stage->roles,
106: 'requireComment'=>$stage->requireComment
107: );
108: }
109: unset($stage);
110:
111: $workflowActions = array();
112:
113: if(!empty($modelId)) {
114: $workflowActions = CActiveRecord::model('Actions')->findAllByAttributes(
115: array('associationId'=>$modelId,'associationType'=>$modelType,'type'=>'workflow','workflowId'=>$workflowId),
116: new CDbCriteria(array('order'=>'createDate ASC'))
117: );
118: }
119:
120: foreach($workflowActions as &$action) {
121:
122: if($action->stageNumber < 1 || $action->stageNumber > count($workflowStages)) {
123: $action->delete();
124: continue;
125: }
126:
127: $workflowStatus['started'] = true; // clearly there's at least one stage up in here
128:
129: $stage = $action->stageNumber;
130: // if(!is_array($workflowStatus[$action->stageNumber]))
131: // $workflowStatus[$action->stageNumber] = array($workflowStatus[$action->stageNumber]);
132:
133: // decode workflowActions into a funnel list
134: $workflowStatus['stages'][$stage]['createDate'] = $action->createDate; // Note: multiple actions with the same stage will overwrite each other
135: $workflowStatus['stages'][$stage]['completeDate'] = $action->completeDate;
136: $workflowStatus['stages'][$stage]['complete'] = ($action->complete == 'Yes') || (!empty($action->completeDate) && $action->completeDate < time()); // determine whether stage is complete
137: $workflowStatus['stages'][$stage]['description'] = $action->actionDescription; // or the stage is beyond the possible range somehow
138:
139: /* $actionData = explode(':',$action->actionDescription);
140: // decode workflowActions into a funnel list
141: if(count($actionData) >= 2 && $actionData[0] == $workflowId && $actionData[1] <= count($workflowStages)) { // ignore action if it's for a different workflow
142: $workflowStatus[$actionData[1]]['createDate'] = $action->createDate; // or the stage is beyond the possible range somehow
143: $workflowStatus[$actionData[1]]['completeDate'] = $action->completeDate; // Note: multiple actions with the same stage will overwrite each other
144: $workflowStatus[$actionData[1]]['complete'] = ($action->complete == 'Yes') || (!empty($action->completeDate) && $action->completeDate < time()); // determine whether stage is complete
145: } */
146: }
147:
148: $workflowStatus['completed'] = true;
149: foreach($workflowStatus['stages'] as &$stage) { // now scan through and see if there are any incomplete stages
150: if(!isset($stage['completeDate'])) {
151: $workflowStatus['completed'] = false;
152: break;
153: }
154: }
155:
156: return $workflowStatus;
157: }
158: }