1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: 19: 20: 21: 22: 23:
24: class NewsletterLogCollection extends ItemCollection {
25:
26: 27: 28: 29: 30:
31: public function __construct() {
32: global $cfg;
33: parent::__construct($cfg["tab"]["news_log"], "idnewslog");
34: $this->_setItemClass("NewsletterLog");
35: }
36:
37: 38: 39: 40: 41: 42: 43: 44: 45: 46:
47: public function create($idnewsjob, $idnewsrcp) {
48: global $client, $lang, $auth;
49:
50: $idnewsjob = cSecurity::toInteger($idnewsjob);
51: $idnewsrcp = cSecurity::toInteger($idnewsrcp);
52: $client = cSecurity::toInteger($client);
53: $lang = cSecurity::toInteger($lang);
54:
55: $this->resetQuery();
56: $this->setWhere("idnewsjob", $idnewsjob);
57: $this->setWhere("idnewsrcp", $idnewsrcp);
58: $this->query();
59:
60: if ($oItem = $this->next()) {
61: return $oItem;
62: }
63:
64: $oRecipient = new NewsletterRecipient();
65: if ($oRecipient->loadByPrimaryKey($idnewsrcp)) {
66: $oItem = parent::createNewItem();
67:
68: $oItem->set("idnewsjob", $idnewsjob);
69: $oItem->set("idnewsrcp", $idnewsrcp);
70:
71: $sEMail = $oRecipient->get("email");
72: $sName = $oRecipient->get("name");
73:
74: if ($sName == "") {
75: $oItem->set("rcpname", $sEMail);
76: } else {
77: $oItem->set("rcpname", $sName);
78: }
79:
80: $oItem->set("rcpemail", $sEMail);
81: $oItem->set("rcphash", $oRecipient->get("hash"));
82: $oItem->set("rcpnewstype", $oRecipient->get("news_type"));
83: $oItem->set("status", "pending");
84: $oItem->set("created", date('Y-m-d H:i:s'), false);
85: $oItem->store();
86:
87: return $oItem;
88: } else {
89: return false;
90: }
91: }
92:
93: 94: 95: 96: 97: 98: 99: 100:
101: public function initializeJob($idnewsjob, $idnews) {
102: global $cfg;
103:
104: $idnewsjob = cSecurity::toInteger($idnewsjob);
105: $idnews = cSecurity::toInteger($idnews);
106:
107: $oNewsletter = new Newsletter();
108: if ($oNewsletter->loadByPrimaryKey($idnews)) {
109: $sDestination = $oNewsletter->get("send_to");
110: $iIDClient = $oNewsletter->get("idclient");
111: $iIDLang = $oNewsletter->get("idlang");
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 = $cfg['tab']['news_rcp'] . ".idclient = '" . $iIDClient . "' AND " . $cfg['tab']['news_rcp'] . ".idlang = '" . $iIDLang . "' AND " . $cfg['tab']['news_rcp'] . ".deactivated = '0' AND " . $cfg['tab']['news_rcp'] . ".confirmed = '1' AND " . $cfg['tab']['news_rcp'] . ".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->primaryKey));
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: public function deleteJob($idnewsjob) {
193: $idnewsjob = cSecurity::toInteger($idnewsjob);
194: $this->setWhere("idnewsjob", $idnewsjob);
195: $this->query();
196:
197: while ($oItem = $this->next()) {
198: $this->delete($oItem->get($oItem->primaryKey));
199: }
200:
201: return true;
202: }
203:
204: }
205:
206: 207: 208:
209: class NewsletterLog extends Item {
210:
211: 212: 213: 214: 215:
216: public function __construct($mId = false) {
217: global $cfg;
218: parent::__construct($cfg["tab"]["news_log"], "idnewslog");
219: if ($mId !== false) {
220: $this->loadByPrimaryKey($mId);
221: }
222: }
223:
224: }
225:
226: ?>