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 NewsletterRecipientGroupCollection extends ItemCollection {
 25: 
 26:      27:  28:  29:  30: 
 31:     public function __construct() {
 32:         global $cfg;
 33:         parent::__construct($cfg["tab"]["news_groups"], "idnewsgroup");
 34:         $this->_setItemClass("NewsletterRecipientGroup");
 35:     }
 36: 
 37:      38:  39:  40:  41:  42:  43: 
 44:     public function create($groupname, $defaultgroup = 0) {
 45:         global $client, $lang;
 46: 
 47:         $group = new NewsletterRecipientGroup();
 48: 
 49:         
 50: 
 51:         $mangledGroupName = $group->_inFilter($groupname);
 52:         $this->setWhere("idclient", $client);
 53:         $this->setWhere("idlang", $lang);
 54:         $this->setWhere("groupname", $mangledGroupName);
 55:         $this->query();
 56: 
 57:         if ($obj = $this->next()) {
 58:             $groupname = $groupname . md5(rand());
 59:         }
 60: 
 61:         $item = $this->createNewItem();
 62: 
 63:         $item->set("idclient", $client);
 64:         $item->set("idlang", $lang);
 65:         $item->set("groupname", $groupname);
 66:         $item->set("defaultgroup", $defaultgroup);
 67:         $item->store();
 68: 
 69:         return $item;
 70:     }
 71: 
 72:      73:  74:  75:  76:  77: 
 78:     public function delete($itemID) {
 79:         $oAssociations = new NewsletterRecipientGroupMemberCollection();
 80:         $oAssociations->setWhere("idnewsgroup", $itemID);
 81:         $oAssociations->query();
 82: 
 83:         while ($oItem = $oAssociations->next()) {
 84:             $oAssociations->delete($oItem->get("idnewsgroupmember"));
 85:         }
 86:         parent::delete($itemID);
 87:     }
 88: 
 89: }
 90: 
 91:  92:  93: 
 94: class NewsletterRecipientGroup extends Item {
 95: 
 96:      97:  98:  99: 100: 
101:     public function __construct($mId = false) {
102:         global $cfg;
103:         parent::__construct($cfg["tab"]["news_groups"], "idnewsgroup");
104:         if ($mId !== false) {
105:             $this->loadByPrimaryKey($mId);
106:         }
107:     }
108: 
109:     110: 111: 
112:     public function store() {
113:         global $client, $lang;
114: 
115:         $client = cSecurity::toInteger($client);
116:         $lang = cSecurity::toInteger($lang);
117: 
118:         if ($this->get("defaultgroup") == 1) {
119:             $oItems = new NewsletterRecipientGroupCollection();
120:             $oItems->setWhere("idclient", $client);
121:             $oItems->setWhere("idlang", $lang);
122:             $oItems->setWhere("defaultgroup", 1);
123:             $oItems->setWhere("idnewsgroup", $this->get("idnewsgroup"), "<>");
124:             $oItems->query();
125: 
126:             while ($oItem = $oItems->next()) {
127:                 $oItem->set("defaultgroup", 0);
128:                 $oItem->store();
129:             }
130:         }
131:         return parent::store();
132:     }
133:     
134:     135: 136: 137: 138: 139: 140: 
141:     public function setField($name, $value, $bSafe = true) {
142:         switch ($name) {
143:             case 'idclient':
144:                 $value = (int) $value;
145:                 break;
146:             case 'idlang':
147:                 $value = (int) $value;
148:                 break;
149:         }
150: 
151:         return parent::setField($name, $value, $bSafe);
152:     }
153: 
154: }
155: 
156: 157: 158: 
159: class NewsletterRecipientGroupMemberCollection extends ItemCollection {
160: 
161:     162: 163: 164: 165: 
166:     public function __construct() {
167:         global $cfg;
168:         parent::__construct($cfg["tab"]["news_groupmembers"], "idnewsgroupmember");
169:         $this->_setJoinPartner('NewsletterRecipientGroupCollection');
170:         $this->_setJoinPartner('NewsletterRecipientCollection');
171:         $this->_setItemClass("NewsletterRecipientGroupMember");
172:     }
173: 
174:     175: 176: 177: 178: 179: 
180:     public function create($idrecipientgroup, $idrecipient) {
181: 
182:         $this->setWhere("idnewsgroup", $idrecipientgroup);
183:         $this->setWhere("idnewsrcp", $idrecipient);
184:         $this->query();
185: 
186:         if ($this->next()) {
187:             return false;
188:         }
189: 
190:         $oItem = parent::createNewItem();
191: 
192:         $oItem->set("idnewsrcp", $idrecipient);
193:         $oItem->set("idnewsgroup", $idrecipientgroup);
194:         $oItem->store();
195: 
196:         return $oItem;
197:     }
198: 
199:     200: 201: 202: 203: 204: 
205:     public function remove($idrecipientgroup, $idrecipient) {
206:         $idrecipientgroup = cSecurity::toInteger($idrecipientgroup);
207:         $idrecipient = cSecurity::toInteger($idrecipient);
208: 
209:         $this->setWhere("idnewsgroup", $idrecipientgroup);
210:         $this->setWhere("idnewsrcp", $idrecipient);
211:         $this->query();
212: 
213:         if ($oItem = $this->next()) {
214:             $this->delete($oItem->get("idnewsgroupmember"));
215:         }
216:     }
217: 
218:     219: 220: 221: 222: 
223:     public function removeRecipientFromGroups($idrecipient) {
224:         $idrecipient = cSecurity::toInteger($idrecipient);
225: 
226:         $this->setWhere("idnewsrcp", $idrecipient);
227:         $this->query();
228: 
229:         while ($oItem = $this->next()) {
230:             $this->delete($oItem->get("idnewsgroupmember"));
231:         }
232:     }
233: 
234:     235: 236: 237: 238: 
239:     public function removeGroup($idgroup) {
240:         $idgroup = cSecurity::toInteger($idgroup);
241: 
242:         $this->setWhere("idnewsgroup", $idgroup);
243:         $this->query();
244: 
245:         while ($oItem = $this->next()) {
246:             $this->delete($oItem->get("idnewsgroupmember"));
247:         }
248:     }
249: 
250:     251: 252: 253: 254: 255: 256: 
257:     public function getRecipientsInGroup($idrecipientgroup, $asObjects = true) {
258:         $idrecipientgroup = cSecurity::toInteger($idrecipientgroup);
259: 
260:         $this->setWhere("idnewsgroup", $idrecipientgroup);
261:         $this->query();
262: 
263:         $aObjects = array();
264: 
265:         while ($oItem = $this->next()) {
266:             if ($asObjects) {
267:                 $oRecipient = new NewsletterRecipient();
268:                 $oRecipient->loadByPrimaryKey($oItem->get("idnewsrcp"));
269: 
270:                 $aObjects[] = $oRecipient;
271:             } else {
272:                 $aObjects[] = $oItem->get("idnewsrcp");
273:             }
274:         }
275: 
276:         return ($aObjects);
277:     }
278: 
279: }
280: 
281: 282: 283: 
284: class NewsletterRecipientGroupMember extends Item {
285: 
286:     287: 288: 289: 290: 
291:     public function __construct($mId = false) {
292:         global $cfg;
293:         parent::__construct($cfg["tab"]["news_groupmembers"], "idnewsgroupmember");
294:         if ($mId !== false) {
295:             $this->loadByPrimaryKey($mId);
296:         }
297:     }
298: 
299:     300: 301: 302: 303: 304: 305: 
306:     public function setField($name, $value, $bSafe = true) {
307:         switch ($name) {
308:             case 'idnewsgroup':
309:                 $value = (int) $value;
310:                 break;
311:             case 'idnewsrcp':
312:                 $value = (int) $value;
313:                 break;
314:         }
315: 
316:         return parent::setField($name, $value, $bSafe);
317:     }
318:     
319: }
320: ?>