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 WorkflowAllocations extends ItemCollection {
 25: 
 26:      27:  28:  29:  30: 
 31:     public function __construct() {
 32:         global $cfg;
 33:         parent::__construct($cfg["tab"]["workflow_allocation"], "idallocation");
 34:         $this->_setItemClass("WorkflowAllocation");
 35:     }
 36: 
 37:     public function delete($idallocation) {
 38:         global $cfg, $lang;
 39: 
 40:         $obj = new WorkflowAllocation();
 41:         $obj->loadByPrimaryKey($idallocation);
 42: 
 43:         $idcatlang = $obj->get("idcatlang");
 44: 
 45:         $db = cRegistry::getDb();
 46:         $sql = "SELECT idcat FROM " . $cfg["tab"]["cat_lang"] . " WHERE idcatlang = '" . cSecurity::toInteger($idcatlang) . "'";
 47:         $db->query($sql);
 48:         $db->nextRecord();
 49:         $idcat = $db->f("idcat");
 50: 
 51:         $sql = "SELECT idart FROM " . $cfg["tab"]["cat_art"] . " WHERE idcat = '" . cSecurity::toInteger($idcat) . "'";
 52:         $db->query($sql);
 53: 
 54:         while ($db->nextRecord()) {
 55:             $idarts[] = $db->f("idart");
 56:         }
 57: 
 58:         $idartlangs = array();
 59: 
 60:         if (is_array($idarts)) {
 61:             foreach ($idarts as $idart) {
 62:                 $sql = "SELECT idartlang FROM " . $cfg["tab"]["art_lang"] . " WHERE idart = '" . cSecurity::toInteger($idart) . "' and idlang = '" . cSecurity::toInteger($lang) . "'";
 63:                 $db->query($sql);
 64:                 if ($db->nextRecord()) {
 65:                     $idartlangs[] = $db->f("idartlang");
 66:                 }
 67:             }
 68:         }
 69: 
 70:         $workflowArtAllocation = new WorkflowArtAllocation();
 71:         $workflowArtAllocations = new WorkflowArtAllocations();
 72: 
 73:         foreach ($idartlangs as $idartlang) {
 74:             $workflowArtAllocation->loadBy("idartlang", $idartlang);
 75:             $workflowArtAllocations->delete($workflowArtAllocation->get("idartallocation"));
 76:         }
 77: 
 78:         parent::delete($idallocation);
 79:     }
 80: 
 81:     public function create($idworkflow, $idcatlang) {
 82:         $this->select("idcatlang = '$idcatlang'");
 83: 
 84:         if ($this->next() !== false) {
 85:             $this->lasterror = i18n("Category already has a workflow assigned", "workflow");
 86:             return false;
 87:         }
 88: 
 89:         $workflows = new Workflows();
 90:         $workflows->select("idworkflow = '$idworkflow'");
 91: 
 92:         if ($workflows->next() === false) {
 93:             $this->lasterror = i18n("Workflow doesn't exist", "workflow");
 94:             return false;
 95:         }
 96:         $newitem = $this->createNewItem();
 97:         if (!$newitem->setWorkflow($idworkflow)) {
 98:             $this->lasterror = $newitem->lasterror;
 99:             $workflows->delete($newitem->getField("idallocation"));
100:             return false;
101:         }
102: 
103:         if (!$newitem->setCatLang($idcatlang)) {
104:             $this->lasterror = $newitem->lasterror;
105:             $workflows->delete($newitem->getField("idallocation"));
106:             return false;
107:         }
108: 
109:         $newitem->store();
110: 
111:         return ($newitem);
112:     }
113: 
114: }
115: 
116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 
126: class WorkflowAllocation extends Item {
127: 
128:     129: 130: 131: 132: 
133:     public function __construct() {
134:         global $cfg;
135: 
136:         parent::__construct($cfg["tab"]["workflow_allocation"], "idallocation");
137:     }
138: 
139:     140: 141: 142: 143: 144: 145: 146: 147: 148: 
149:     public function setField($field, $value, $safe = true) {
150:         throw new cBadMethodCallException("Don't use setField for WorkflowAllocation items! Use setWorkflow instead!");
151:     }
152: 
153:     154: 155: 156: 157: 
158:     public function setWorkflow($idworkflow) {
159:         $workflows = new Workflows();
160: 
161:         $workflows->select("idworkflow = '$idworkflow'");
162: 
163:         if ($workflows->next() === false) {
164:             $this->lasterror = i18n("Workflow doesn't exist", "workflow");
165:             return false;
166:         }
167: 
168:         parent::setField("idworkflow", $idworkflow);
169:         $this->store();
170:         return true;
171:     }
172: 
173:     174: 175: 176: 177: 178: 179: 
180:     public function setCatLang($idcatlang) {
181:         global $cfg;
182: 
183:         $allocations = new WorkflowAllocations();
184: 
185:         $allocations->select("idcatlang = '$idcatlang'");
186: 
187:         if ($allocations->next() !== false) {
188:             $this->lasterror = i18n("Category already has a workflow assigned", "workflow");
189:             return false;
190:         }
191: 
192:         $db = cRegistry::getDb();
193:         $sql = "SELECT idcatlang FROM " . $cfg["tab"]["cat_lang"] . " WHERE idcatlang = '" . cSecurity::toInteger($idcatlang) . "'";
194:         $db->query($sql);
195: 
196:         if (!$db->nextRecord()) {
197:             $this->lasterror = i18n("Category doesn't exist, assignment failed", "workflow");
198:             return false;
199:         }
200: 
201:         parent::setField("idcatlang", $idcatlang);
202:         $this->store();
203:         return true;
204:     }
205: 
206: }
207: 
208: ?>