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