1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: 18: 19: 20: 21: 22: 23: 24:
25: class NewsletterRecipientCollection extends ItemCollection {
26: 27: 28: 29: 30:
31: public function __construct() {
32: global $cfg;
33: parent::__construct($cfg["tab"]["news_rcp"], "idnewsrcp");
34: $this->_setItemClass("NewsletterRecipient");
35: }
36:
37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52:
53: public function create($sEMail, $sName = "", $iConfirmed = 0, $sJoinID = "", $iMessageType = 0) {
54: global $client, $lang, $auth;
55:
56:
57: $email = cString::toLowerCase($sEMail);
58: $this->setWhere("idclient", $client);
59: $this->setWhere("idlang", $lang);
60: $this->setWhere("email", $email);
61: $this->query();
62:
63: if ($this->next()) {
64: return $this->create($email . "_" . cString::getPartOfString(md5(rand()), 0, 10), $sName, 0, $sJoinID, $iMessageType);
65:
66:
67: }
68: $oItem = $this->createNewItem();
69: $oItem->set("idclient", $client);
70: $oItem->set("idlang", $lang);
71: $oItem->set("name", $sName);
72: $oItem->set("email", $email);
73: $oItem->set("hash", cString::getPartOfString(md5(rand()), 0, 17) . uniqid(""));
74:
75:
76: $oItem->set("confirmed", $iConfirmed);
77: $oItem->set("news_type", $iMessageType);
78:
79: if ($iConfirmed) {
80: $oItem->set("confirmeddate", date("Y-m-d H:i:s"), false);
81: }
82: $oItem->set("deactivated", 0);
83: $oItem->set("created", date('Y-m-d H:i:s'), false);
84: $oItem->set("author", $auth->auth["uid"]);
85: $oItem->store();
86:
87: $iIDRcp = $oItem->get("idnewsrcp");
88:
89:
90:
91: $oGroups = new NewsletterRecipientGroupCollection();
92: $oGroupMembers = new NewsletterRecipientGroupMemberCollection();
93:
94: $oGroups->setWhere("idclient", $client);
95: $oGroups->setWhere("idlang", $lang);
96: $oGroups->setWhere("defaultgroup", 1);
97: $oGroups->query();
98:
99: while ($oGroup = $oGroups->next()) {
100: $iIDGroup = $oGroup->get("idnewsgroup");
101: $oGroupMembers->create($iIDGroup, $iIDRcp);
102: }
103:
104:
105: if ($sJoinID != "") {
106: $aJoinID = explode(",", $sJoinID);
107:
108: if (count($aJoinID) > 0) {
109: foreach ($aJoinID as $iIDGroup) {
110: $oGroupMembers->create($iIDGroup, $iIDRcp);
111: }
112: }
113: }
114:
115: return $oItem;
116: }
117:
118: 119: 120: 121: 122: 123: 124: 125: 126: 127:
128: public function delete($itemID) {
129: $oAssociations = new NewsletterRecipientGroupMemberCollection();
130: $oAssociations->setWhere("idnewsrcp", $itemID);
131: $oAssociations->query();
132:
133: While ($oItem = $oAssociations->next()) {
134: $oAssociations->delete($oItem->get("idnewsgroupmember"));
135: }
136: parent::delete($itemID);
137: }
138:
139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150:
151: public function purge($timeframe) {
152: global $client, $lang;
153:
154: $oRecipientCollection = new NewsletterRecipientCollection();
155:
156:
157:
158:
159:
160: $oRecipientCollection->setWhere("idclient", $client);
161: $oRecipientCollection->setWhere("idlang", $lang);
162: $oRecipientCollection->setWhere("confirmed", 0);
163: $oRecipientCollection->setWhere("(TO_DAYS(NOW()) - TO_DAYS(created))", $timeframe, ">");
164: $oRecipientCollection->query();
165:
166: while ($oItem = $oRecipientCollection->next()) {
167: $oRecipientCollection->delete($oItem->get("idnewsrcp"));
168: }
169: return $oRecipientCollection->count();
170: }
171:
172: 173: 174: 175: 176: 177: 178: 179: 180:
181: public function emailExists($sEmail) {
182: global $client, $lang;
183:
184: $oRecipientCollection = new NewsletterRecipientCollection();
185: $oRecipientCollection->setWhere("idclient", $client);
186: $oRecipientCollection->setWhere("idlang", $lang);
187: $oRecipientCollection->setWhere("email", cString::toLowerCase($sEmail));
188: $oRecipientCollection->query();
189:
190: if ($oItem = $oRecipientCollection->next()) {
191: return $oItem;
192: } else {
193: return false;
194: }
195: }
196:
197: 198: 199: 200: 201: 202: 203:
204: public function updateKeys() {
205: $this->setWhere("LENGTH(hash)", 30, "<>");
206: $this->query();
207:
208: $iUpdated = $this->count();
209: while ($oItem = $this->next()) {
210: $oItem->set("hash", cString::getPartOfString(md5(rand()), 0, 17) . uniqid("")); 211: 212: 213: 214: 215: 216:
217: $oItem->store();
218: }
219:
220: return $iUpdated;
221: }
222:
223: }
224:
225: 226: 227:
228: class NewsletterRecipient extends Item {
229: 230: 231: 232: 233: 234: 235: 236:
237: public function __construct($mId = false) {
238: global $cfg;
239: parent::__construct($cfg["tab"]["news_rcp"], "idnewsrcp");
240: if ($mId !== false) {
241: $this->loadByPrimaryKey($mId);
242: }
243: }
244:
245: 246: 247: 248: 249:
250: public function store() {
251: global $auth;
252:
253: $this->set("lastmodified", date('Y-m-d H:i:s'), false);
254: $this->set("modifiedby", $auth->auth["uid"]);
255: $success = parent::store();
256:
257:
258:
259:
260:
261: $sName = $this->get("name");
262: $sEmail = $this->get("email");
263: if ($sName == "") {
264: $sName = $sEmail;
265: }
266: $iNewsType = $this->get("news_type");
267:
268: $oLogs = new NewsletterLogCollection();
269: $oLogs->setWhere("idnewsrcp", $this->get($this->getPrimaryKeyName()));
270: $oLogs->setWhere("status", "pending");
271: $oLogs->query();
272:
273: while ($oLog = $oLogs->next()) {
274: $oLog->set("rcpname", $sName);
275: $oLog->set("rcpemail", $sEmail);
276: $oLog->set("rcpnewstype", $iNewsType);
277: $oLog->store();
278: }
279:
280: return $success;
281: }
282:
283: 284: 285: 286: 287: 288: 289: 290: 291:
292: public function setField($name, $value, $bSafe = true) {
293: switch ($name) {
294: case 'confirmed':
295: $value = (int) $value;
296: break;
297: case 'news_type':
298: $value = (int) $value;
299: break;
300: }
301:
302: return parent::setField($name, $value, $bSafe);
303: }
304:
305: }
306:
307: ?>