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