1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
15:
16: 17: 18: 19: 20: 21: 22: 23:
24: class NewsletterLogCollection extends ItemCollection {
25: 26: 27: 28: 29:
30: public function __construct() {
31: global $cfg;
32: parent::__construct($cfg["tab"]["news_log"], "idnewslog");
33: $this->_setItemClass("NewsletterLog");
34: }
35:
36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46:
47: public function create($idnewsjob, $idnewsrcp) {
48:
49: $this->resetQuery();
50: $this->setWhere("idnewsjob", $idnewsjob);
51: $this->setWhere("idnewsrcp", $idnewsrcp);
52: $this->query();
53:
54: if ($oItem = $this->next()) {
55: return $oItem;
56: }
57:
58: $oRecipient = new NewsletterRecipient();
59: if ($oRecipient->loadByPrimaryKey($idnewsrcp)) {
60: $oItem = $this->createNewItem();
61:
62: $oItem->set("idnewsjob", $idnewsjob);
63: $oItem->set("idnewsrcp", $idnewsrcp);
64:
65: $sEMail = $oRecipient->get("email");
66: $sName = $oRecipient->get("name");
67:
68: if ($sName == "") {
69: $oItem->set("rcpname", $sEMail);
70: } else {
71: $oItem->set("rcpname", $sName);
72: }
73:
74: $oItem->set("rcpemail", $sEMail);
75: $oItem->set("rcphash", $oRecipient->get("hash"));
76: $oItem->set("rcpnewstype", $oRecipient->get("news_type"));
77: $oItem->set("status", "pending");
78: $oItem->set("created", date('Y-m-d H:i:s'), false);
79: $oItem->store();
80:
81: return $oItem;
82: } else {
83: return false;
84: }
85: }
86:
87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98:
99: public function initializeJob($idnewsjob, $idnews) {
100: global $cfg;
101:
102: $idnewsjob = cSecurity::toInteger($idnewsjob);
103: $idnews = cSecurity::toInteger($idnews);
104:
105: $oNewsletter = new Newsletter();
106: if ($oNewsletter->loadByPrimaryKey($idnews)) {
107: $sDestination = $oNewsletter->get("send_to");
108: $iIDClient = $oNewsletter->get("idclient");
109: $iIDLang = $oNewsletter->get("idlang");
110: $nrc = new NewsletterRecipientCollection();
111: $nrcClassName = cString::toLowerCase(get_class($nrc));
112:
113: switch ($sDestination) {
114: case "all":
115: $sDistinct = "";
116: $sFrom = "";
117: $sSQL = "deactivated='0' AND confirmed='1' AND idclient='" . $iIDClient . "' AND idlang='" . $iIDLang . "'";
118: break;
119: case "default":
120: $sDistinct = "distinct";
121: $sFrom = $cfg["tab"]["news_groups"] . " AS groups, " . $cfg["tab"]["news_groupmembers"] . " AS groupmembers ";
122: $sSQL = $nrcClassName . ".idclient = '" . $iIDClient . "' AND " . $nrcClassName . ".idlang = '" . $iIDLang . "' AND " . $nrcClassName . ".deactivated = '0' AND " . $nrcClassName . ".confirmed = '1' AND " . $nrcClassName . ".idnewsrcp = groupmembers.idnewsrcp AND " . "groupmembers.idnewsgroup = groups.idnewsgroup AND " . "groups.defaultgroup = '1' AND groups.idclient = '" . $iIDClient . "' AND " . "groups.idlang = '" . $iIDLang . "'";
123: break;
124: case "selection":
125: $aGroups = unserialize($oNewsletter->get("send_ids"));
126:
127: if (is_array($aGroups) && count($aGroups) > 0) {
128: $sGroups = "'" . implode("','", $aGroups) . "'";
129:
130: $sDistinct = "distinct";
131: $sFrom = $cfg["tab"]["news_groupmembers"] . " AS groupmembers ";
132: $sSQL = "newsletterrecipientcollection.idclient = '" . $iIDClient . "' AND newsletterrecipientcollection.idlang = '" . $iIDLang . "' AND newsletterrecipientcollection.deactivated = '0' AND newsletterrecipientcollection.confirmed = '1' AND newsletterrecipientcollection.idnewsrcp = groupmembers.idnewsrcp AND " . "groupmembers.idnewsgroup IN (" . $sGroups . ")";
133: } else {
134: $sDestination = "unknown";
135: }
136: break;
137: case "single":
138: $iID = $oNewsletter->get("send_ids");
139: if (is_numeric($iID)) {
140: $sDistinct = "";
141: $sFrom = "";
142: $sSQL = "idnewsrcp = '" . $iID . "'";
143: } else {
144: $sDestination = "unknown";
145: }
146: break;
147: default:
148: $sDestination = "unknown";
149: }
150: unset($oNewsletter);
151:
152: if ($sDestination == "unknown") {
153: return 0;
154: } else {
155: $oRecipients = new NewsletterRecipientCollection();
156: $oRecipients->flexSelect($sDistinct, $sFrom, $sSQL, "", "", "");
157:
158: $iRecipients = $oRecipients->count();
159:
160: while ($oRecipient = $oRecipients->next()) {
161: $this->create($idnewsjob, $oRecipient->get($oRecipient->getPrimaryKeyName()));
162: }
163:
164: return $iRecipients;
165: }
166: } else {
167: return 0;
168: }
169: }
170:
171: 172: 173: 174: 175: 176:
177: public function delete($idnewslog) {
178: $idnewslog = cSecurity::toInteger($idnewslog);
179:
180: $oLog = new NewsletterLog($idnewslog);
181: $iIDNewsJob = $oLog->get("idnewsjob");
182: unset($oLog);
183:
184: $oJob = new NewsletterJob($iIDNewsJob);
185: $oJob->set("rcpcount", $oJob->get("rcpcount") - 1);
186: $oJob->store();
187: unset($oJob);
188:
189: parent::delete($idnewslog);
190: }
191:
192: 193: 194: 195: 196: 197:
198: public function deleteJob($idnewsjob) {
199: $idnewsjob = cSecurity::toInteger($idnewsjob);
200: $this->setWhere("idnewsjob", $idnewsjob);
201: $this->query();
202:
203: while ($oItem = $this->next()) {
204: $this->delete($oItem->get($oItem->getPrimaryKeyName()));
205: }
206:
207: return true;
208: }
209:
210: }
211:
212: 213: 214:
215: class NewsletterLog extends Item {
216: 217: 218: 219: 220: 221: 222: 223:
224: public function __construct($mId = false) {
225: global $cfg;
226: parent::__construct($cfg["tab"]["news_log"], "idnewslog");
227: if ($mId !== false) {
228: $this->loadByPrimaryKey($mId);
229: }
230: }
231:
232: 233: 234: 235: 236: 237: 238: 239: 240:
241: public function setField($name, $value, $bSafe = true) {
242: switch ($name) {
243: case 'idnewsjob':
244: $value = (int) $value;
245: break;
246: case 'idnewsrcp':
247: $value = (int) $value;
248: break;
249: }
250:
251: return parent::setField($name, $value, $bSafe);
252: }
253:
254: }
255:
256: ?>