1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16:
17: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
18:
19: 20: 21: 22: 23: 24:
25: class NewsletterRecipientCollection extends ItemCollection {
26:
27: 28: 29: 30: 31:
32: public function __construct() {
33: global $cfg;
34: parent::__construct($cfg["tab"]["news_rcp"], "idnewsrcp");
35: $this->_setItemClass("NewsletterRecipient");
36: }
37:
38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49:
50: public function create($sEMail, $sName = "", $iConfirmed = 0, $sJoinID = "", $iMessageType = 0) {
51: global $client, $lang, $auth;
52:
53: $iConfirmed = (int) $iConfirmed;
54: $iMessageType = (int) $iMessageType;
55:
56:
57: $email = strtolower($email);
58: $this->setWhere("idclient", $client);
59: $this->setWhere("idlang", $lang);
60: $this->setWhere("email", $sEMail);
61: $this->query();
62:
63: if ($this->next()) {
64: return $this->create($sEMail . "_" . substr(md5(rand()), 0, 10), $sName, 0, $sJoinID, $iMessageType);
65:
66:
67: }
68: $oItem = parent::createNewItem();
69: $oItem->set("idclient", $client);
70: $oItem->set("idlang", $lang);
71: $oItem->set("name", $sName);
72: $oItem->set("email", $sEMail);
73: $oItem->set("hash", substr(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: public function delete($itemID) {
125: $oAssociations = new NewsletterRecipientGroupMemberCollection();
126: $oAssociations->setWhere("idnewsrcp", $itemID);
127: $oAssociations->query();
128:
129: While ($oItem = $oAssociations->next()) {
130: $oAssociations->delete($oItem->get("idnewsgroupmember"));
131: }
132: parent::delete($itemID);
133: }
134:
135: 136: 137: 138: 139: 140: 141: 142:
143: public function purge($timeframe) {
144: global $client, $lang;
145:
146: $oRecipientCollection = new NewsletterRecipientCollection();
147:
148:
149:
150:
151:
152: $oRecipientCollection->setWhere("idclient", $client);
153: $oRecipientCollection->setWhere("idlang", $lang);
154: $oRecipientCollection->setWhere("confirmed", 0);
155: $oRecipientCollection->setWhere("(TO_DAYS(NOW()) - TO_DAYS(created))", $timeframe, ">");
156: $oRecipientCollection->query();
157:
158: while ($oItem = $oRecipientCollection->next()) {
159: $oRecipientCollection->delete($oItem->get("idnewsrcp"));
160: }
161: return $oRecipientCollection->count();
162: }
163:
164: 165: 166: 167: 168: 169: 170:
171: public function emailExists($sEmail) {
172: global $client, $lang;
173:
174: $oRecipientCollection = new NewsletterRecipientCollection();
175:
176: $oRecipientCollection->setWhere("idclient", $client);
177: $oRecipientCollection->setWhere("idlang", $lang);
178: $oRecipientCollection->setWhere("email", strtolower($sEmail));
179: $oRecipientCollection->query();
180:
181: if ($oItem = $oRecipientCollection->next()) {
182: return $oItem;
183: } else {
184: return false;
185: }
186: }
187:
188: 189: 190: 191: 192:
193: public function updateKeys() {
194: $this->setWhere("LENGTH(hash)", 30, "<>");
195: $this->query();
196:
197: $iUpdated = $this->count();
198: while ($oItem = $this->next()) {
199: $oItem->set("hash", substr(md5(rand()), 0, 17) . uniqid("")); 200: 201: 202: 203: 204: 205:
206: $oItem->store();
207: }
208:
209: return $iUpdated;
210: }
211:
212: }
213:
214: 215: 216:
217: class NewsletterRecipient extends Item {
218:
219: 220: 221: 222: 223:
224: public function __construct($mId = false) {
225: global $cfg;
226: parent::__construct($cfg["tab"]["news_rcp"], "idnewsrcp");
227: if ($mId !== false) {
228: $this->loadByPrimaryKey($mId);
229: }
230: }
231:
232: public function store() {
233: global $auth;
234:
235: $this->set("lastmodified", date('Y-m-d H:i:s'), false);
236: $this->set("modifiedby", $auth->auth["uid"]);
237: $success = parent::store();
238:
239:
240:
241:
242:
243: $sName = $this->get("name");
244: $sEmail = $this->get("email");
245: if ($sName == "") {
246: $sName = $sEmail;
247: }
248: $iNewsType = $this->get("news_type");
249:
250: $oLogs = new NewsletterLogCollection();
251: $oLogs->setWhere("idnewsrcp", $this->get($this->primaryKey));
252: $oLogs->setWhere("status", "pending");
253: $oLogs->query();
254:
255: while ($oLog = $oLogs->next()) {
256: $oLog->set("rcpname", $sName);
257: $oLog->set("rcpemail", $sEmail);
258: $oLog->set("rcpnewstype", $iNewsType);
259: $oLog->store();
260: }
261:
262: return $success;
263: }
264:
265: }
266:
267: ?>