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 = (int) $item->get("position");
 42:         $idworkflow = (int) $item->get("idworkflow");
 43:         $oDb = cRegistry::getDb();
 44: 
 45:         $this->select("position > {$pos} AND idworkflow = {$idworkflow}");
 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 = ' . (int) $id;
 53:         $oDb->query($sSql);
 54:         while ($oDb->nextRecord()) {
 55:             $aUserSequencesDelete[] = (int) $oDb->f('idusersequence');
 56:         }
 57: 
 58:         $sSql = 'DELETE FROM ' . $cfg["tab"]["workflow_actions"] . ' WHERE idworkflowitem = ' . (int) $id;
 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 = ' . (int) $idworkflowitem;
 75: 
 76:         $oDb->query($sSql);
 77:         while ($oDb->nextRecord()) {
 78:             $aUserSequences[] = (int) $oDb->f('idusersequence');
 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:                 $aIdArtLang[] = (int) $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:         $idworkflow = (int) $idworkflow;
103:         $pos1 = (int) $pos1;
104:         $pos2 = (int) $pos2;
105: 
106:         $this->select("idworkflow = {$idworkflow} AND position = {$pos1}");
107:         if (($item = $this->next()) === false) {
108:             $this->lasterror = i18n("Swapping items failed: Item doesn't exist", "workflow");
109:             return false;
110:         }
111: 
112:         $pos1ID = $item->getField("idworkflowitem");
113: 
114:         $this->select("idworkflow = {$idworkflow} AND position = {$pos2}");
115:         if (($item = $this->next()) === false) {
116:             $this->lasterror = i18n("Swapping items failed: Item doesn't exist", "workflow");
117:             return false;
118:         }
119: 
120:         $pos2ID = $item->getField("idworkflowitem");
121: 
122:         $item = new WorkflowItem();
123:         $item->loadByPrimaryKey($pos1ID);
124:         $item->setPosition($pos2);
125:         $item->store();
126:         $item->loadByPrimaryKey($pos2ID);
127:         $item->setPosition($pos1);
128:         $item->store();
129: 
130:         $this->updateArtAllocation($pos1ID);
131:         $this->updateArtAllocation($pos2ID);
132:         return (true);
133:     }
134: 
135:     public function create($idworkflow) {
136:         $idworkflow = (int) $idworkflow;
137: 
138:         $workflows = new Workflows();
139:         $workflows->select("idworkflow = {$idworkflow}");
140: 
141:         if ($workflows->next() === false) {
142:             $this->lasterror = i18n("Can't add item to workflow: Workflow doesn't exist", "workflow");
143:             return false;
144:         }
145: 
146:         $this->select("idworkflow = {$idworkflow}", "", "position DESC", "1");
147: 
148:         $item = $this->next();
149: 
150:         if ($item === false) {
151:             $lastPos = 1;
152:         } else {
153:             $lastPos = $item->getField("position") + 1;
154:         }
155: 
156:         $newItem = parent::createNewItem();
157:         if ($newItem->init($idworkflow, $lastPos) === false) {
158:             $this->delete($newItem->getField("idworkflowitem"));
159:             $this->lasterror = $newItem->lasterror;
160:             return false;
161:         }
162: 
163:         if ($item === false) {
164:             $this->updateArtAllocation(0);
165:         }
166: 
167:         return ($newItem);
168:     }
169: 
170: }
171: 
172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 
182: class WorkflowItem extends Item {
183: 
184:     185: 186: 187: 188: 
189:     public function __construct() {
190:         global $cfg;
191: 
192:         parent::__construct($cfg["tab"]["workflow_items"], "idworkflowitem");
193:     }
194: 
195:     public function getStepRights() {
196:         $idwfi = $this->values["idworkflowitem"];
197:         $workflowActions = new WorkflowActions();
198: 
199:         $actions = $workflowActions->getAvailableWorkflowActions();
200: 
201:         foreach ($actions as $key => $value) {
202:             $rights[$key] = $workflowActions->get($idwfi, $key);
203:         }
204: 
205:         return $rights;
206:     }
207: 
208:     209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 
219:     public function setField($field, $value, $safe = true) {
220:         if ($this->virgin == true) {
221:             $this->lasterror = i18n("No item loaded", "workflow");
222:             return false;
223:         }
224: 
225:         if ($field == "idsequence") {
226:             throw new cInvalidArgumentException("You can't set the idsequence field using this method. Use 'create' in the WorkflowItems class.");
227:         }
228: 
229:         if ($field == "idworkflow") {
230:             throw new cInvalidArgumentException("You can't set the workflow ID using this method. Use 'create' in the WorkflowItems class!");
231:         }
232: 
233:         if ($field == "position") {
234:             throw new cInvalidArgumentException("You can't set the position ID using this method. Use 'create' or 'swap' to create or move items!");
235:         }
236: 
237:         if ($field == "idtask" && $value != 0) {
238:             $taskCollection = new WorkflowTasks();
239:             $taskCollection->select("idtask = '$value'");
240:             if ($taskCollection->next() === false) {
241:                 $this->lasterror = i18n("Requested task doesn't exist, can't assign", "workflow");
242:                 return false;
243:             }
244:         }
245: 
246:         parent::setField($field, $value, $safe);
247:     }
248: 
249:     250: 251: 252: 253: 254: 255: 
256:     public function init($idworkflow, $idposition) {
257:         global $cfg;
258: 
259:         $workflows = new Workflows();
260: 
261:         $workflows->select("idworkflow = '$idworkflow'");
262: 
263:         if ($workflows->next() === false) {
264:             $this->lasterror = i18n("Workflow doesn't exist", "workflow");
265:             return false;
266:         }
267: 
268:         $workflowItems = new WorkflowItems();
269:         $workflowItems->select("position = '$idposition' AND idworkflow = '$idworkflow'");
270:         if ($workflowItems->next()) {
271:             $this->lasterror = i18n("Position in this workflow already exists.", "workflow");
272:             return false;
273:         }
274: 
275:         parent::setField("idworkflow", $idworkflow);
276:         parent::setField("position", $idposition);
277:         parent::store();
278:         return true;
279:     }
280: 
281:     282: 283: 284: 285: 286: 287: 
288:     public function setPosition($idposition) {
289:         parent::setField("position", $idposition);
290:         parent::store();
291:         return true;
292:     }
293: 
294: }
295: 
296: ?>