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