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