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:
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: public function initializeJob($idnewsjob, $idnews) {
96: global $cfg;
97:
98: $idnewsjob = cSecurity::toInteger($idnewsjob);
99: $idnews = cSecurity::toInteger($idnews);
100:
101: $oNewsletter = new Newsletter();
102: if ($oNewsletter->loadByPrimaryKey($idnews)) {
103: $sDestination = $oNewsletter->get("send_to");
104: $iIDClient = $oNewsletter->get("idclient");
105: $iIDLang = $oNewsletter->get("idlang");
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 = $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 . "'";
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: ?>