1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: 19: 20: 21: 22: 23:
24: class WorkflowItems extends ItemCollection {
25:
26: 27: 28: 29: 30:
31: public function __construct() {
32: global $cfg;
33: parent::__construct($cfg["tab"]["workflow_items"], "idworkflowitem");
34: $this->_setItemClass("WorkflowItem");
35: }
36:
37: public function delete($id) {
38: global $cfg;
39: $item = new WorkflowItem();
40: $item->loadByPrimaryKey($id);
41: $pos = $item->get("position");
42: $idworkflow = $item->get("idworkflow");
43: $oDb = cRegistry::getDb();
44:
45: $this->select("position > $pos AND idworkflow = '" . cSecurity::escapeDB($idworkflow, $oDb) . "'");
46: while (($obj = $this->next()) !== false) {
47: $obj->setPosition($obj->get("position") - 1);
48: $obj->store();
49: }
50:
51: $aUserSequencesDelete = array();
52: $sSql = 'SELECT idusersequence FROM ' . $cfg["tab"]["workflow_user_sequences"] . ' WHERE idworkflowitem = ' . $id . ';';
53: $oDb->query($sSql);
54: while ($oDb->nextRecord()) {
55: array_push($aUserSequencesDelete, cSecurity::escapeDB($oDb->f('idusersequence'), $oDb));
56: }
57:
58: $sSql = 'DELETE FROM ' . $cfg["tab"]["workflow_actions"] . ' WHERE idworkflowitem = ' . cSecurity::escapeDB($id, $oDb) . ';';
59: $oDb->query($sSql);
60:
61: $this->updateArtAllocation($id, 1);
62:
63: if (count($aUserSequencesDelete) > 0) {
64: $sSql = 'DELETE FROM ' . $cfg["tab"]["workflow_user_sequences"] . ' WHERE idusersequence in (' . implode(',', $aUserSequencesDelete) . ');';
65: $oDb->query($sSql);
66: }
67: }
68:
69: public function updateArtAllocation($idworkflowitem, $delete = false) {
70: global $idworkflow, $cfg;
71: $oDb = cRegistry::getDb();
72:
73: $aUserSequences = array();
74: $sSql = 'SELECT idusersequence FROM ' . $cfg["tab"]["workflow_user_sequences"] . ' WHERE idworkflowitem = ' . cSecurity::escapeDB($idworkflowitem, $oDb) . ';';
75:
76: $oDb->query($sSql);
77: while ($oDb->nextRecord()) {
78: array_push($aUserSequences, cSecurity::escapeDB($oDb->f('idusersequence'), $oDb));
79: }
80:
81: $aIdArtLang = array();
82: if (count($aUserSequences) > 0) {
83: $sSql = 'SELECT idartlang FROM ' . $cfg["tab"]["workflow_art_allocation"] . ' WHERE idusersequence in (' . implode(',', $aUserSequences) . ');';
84: $oDb->query($sSql);
85: while ($oDb->nextRecord()) {
86: array_push($aIdArtLang, $oDb->f('idartlang'));
87: }
88: $sSql = 'DELETE FROM ' . $cfg["tab"]["workflow_art_allocation"] . ' WHERE idusersequence in (' . implode(',', $aUserSequences) . ');';
89: $oDb->query($sSql);
90: }
91:
92: if ($delete) {
93: parent::delete($idworkflowitem);
94: }
95:
96: foreach ($aIdArtLang as $iIdArtLang) {
97: setUserSequence($iIdArtLang, $idworkflow);
98: }
99: }
100:
101: public function swap($idworkflow, $pos1, $pos2) {
102: $this->select("idworkflow = '$idworkflow' AND position = '$pos1'");
103: if (($item = $this->next()) === false) {
104: $this->lasterror = i18n("Swapping items failed: Item doesn't exist", "workflow");
105: return false;
106: }
107:
108: $pos1ID = $item->getField("idworkflowitem");
109:
110: $this->select("idworkflow = '$idworkflow' AND position = '$pos2'");
111: if (($item = $this->next()) === false) {
112: $this->lasterror = i18n("Swapping items failed: Item doesn't exist", "workflow");
113: return false;
114: }
115:
116: $pos2ID = $item->getField("idworkflowitem");
117:
118: $item = new WorkflowItem();
119: $item->loadByPrimaryKey($pos1ID);
120: $item->setPosition($pos2);
121: $item->store();
122: $item->loadByPrimaryKey($pos2ID);
123: $item->setPosition($pos1);
124: $item->store();
125:
126: $this->updateArtAllocation($pos1ID);
127: $this->updateArtAllocation($pos2ID);
128: return (true);
129: }
130:
131: public function create($idworkflow) {
132: $workflows = new Workflows();
133:
134: $workflows->select("idworkflow = '$idworkflow'");
135:
136: if ($workflows->next() === false) {
137: $this->lasterror = i18n("Can't add item to workflow: Workflow doesn't exist", "workflow");
138: return false;
139: }
140:
141: $this->select("idworkflow = '$idworkflow'", "", "position DESC", "1");
142:
143: $item = $this->next();
144:
145: if ($item === false) {
146: $lastPos = 1;
147: } else {
148: $lastPos = $item->getField("position") + 1;
149: }
150:
151: $newItem = parent::createNewItem();
152: if ($newItem->init($idworkflow, $lastPos) === false) {
153: $this->delete($newItem->getField("idworkflowitem"));
154: $this->lasterror = $newItem->lasterror;
155: return false;
156: }
157:
158: if ($item === false) {
159: $this->updateArtAllocation(0);
160: }
161:
162: return ($newItem);
163: }
164:
165: }
166:
167: 168: 169: 170: 171: 172: 173: 174: 175: 176:
177: class WorkflowItem extends Item {
178:
179: 180: 181: 182: 183:
184: public function __construct() {
185: global $cfg;
186:
187: parent::__construct($cfg["tab"]["workflow_items"], "idworkflowitem");
188: }
189:
190: public function getStepRights() {
191: $idwfi = $this->values["idworkflowitem"];
192: $workflowActions = new WorkflowActions();
193:
194: $actions = $workflowActions->getAvailableWorkflowActions();
195:
196: foreach ($actions as $key => $value) {
197: $rights[$key] = $workflowActions->get($idwfi, $key);
198: }
199:
200: return $rights;
201: }
202:
203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213:
214: public function setField($field, $value, $safe = true) {
215: if ($this->virgin == true) {
216: $this->lasterror = i18n("No item loaded", "workflow");
217: return false;
218: }
219:
220: if ($field == "idsequence") {
221: throw new cInvalidArgumentException("You can't set the idsequence field using this method. Use 'create' in the WorkflowItems class.");
222: }
223:
224: if ($field == "idworkflow") {
225: throw new cInvalidArgumentException("You can't set the workflow ID using this method. Use 'create' in the WorkflowItems class!");
226: }
227:
228: if ($field == "position") {
229: throw new cInvalidArgumentException("You can't set the position ID using this method. Use 'create' or 'swap' to create or move items!");
230: }
231:
232: if ($field == "idtask" && $value != 0) {
233: $taskCollection = new WorkflowTasks();
234: $taskCollection->select("idtask = '$value'");
235: if ($taskCollection->next() === false) {
236: $this->lasterror = i18n("Requested task doesn't exist, can't assign", "workflow");
237: return false;
238: }
239: }
240:
241: parent::setField($field, $value, $safe);
242: }
243:
244: 245: 246: 247: 248: 249: 250:
251: public function init($idworkflow, $idposition) {
252: global $cfg;
253:
254: $workflows = new Workflows();
255:
256: $workflows->select("idworkflow = '$idworkflow'");
257:
258: if ($workflows->next() === false) {
259: $this->lasterror = i18n("Workflow doesn't exist", "workflow");
260: return false;
261: }
262:
263: $workflowItems = new WorkflowItems();
264: $workflowItems->select("position = '$idposition' AND idworkflow = '$idworkflow'");
265: if ($workflowItems->next()) {
266: $this->lasterror = i18n("Position in this workflow already exists.", "workflow");
267: return false;
268: }
269:
270: parent::setField("idworkflow", $idworkflow);
271: parent::setField("position", $idposition);
272: parent::store();
273: return true;
274: }
275:
276: 277: 278: 279: 280: 281: 282:
283: public function setPosition($idposition) {
284: parent::setField("position", $idposition);
285: parent::store();
286: return true;
287: }
288:
289: }
290:
291: ?>