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