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