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