1: <?php
2: /*****************************************************************************************
3: * X2Engine Open Source Edition is a customer relationship management program developed by
4: * X2Engine, Inc. Copyright (C) 2011-2016 X2Engine Inc.
5: *
6: * This program is free software; you can redistribute it and/or modify it under
7: * the terms of the GNU Affero General Public License version 3 as published by the
8: * Free Software Foundation with the addition of the following permission added
9: * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10: * IN WHICH THE COPYRIGHT IS OWNED BY X2ENGINE, X2ENGINE DISCLAIMS THE WARRANTY
11: * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12: *
13: * This program is distributed in the hope that it will be useful, but WITHOUT
14: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15: * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16: * details.
17: *
18: * You should have received a copy of the GNU Affero General Public License along with
19: * this program; if not, see http://www.gnu.org/licenses or write to the Free
20: * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21: * 02110-1301 USA.
22: *
23: * You can contact X2Engine, Inc. P.O. Box 66752, Scotts Valley,
24: * California 95067, USA. or at email address contact@x2engine.com.
25: *
26: * The interactive user interfaces in modified source and object code versions
27: * of this program must display Appropriate Legal Notices, as required under
28: * Section 5 of the GNU Affero General Public License version 3.
29: *
30: * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31: * these Appropriate Legal Notices must retain the display of the "Powered by
32: * X2Engine" logo. If the display of the logo is not reasonably feasible for
33: * technical reasons, the Appropriate Legal Notices must display the words
34: * "Powered by X2Engine".
35: *****************************************************************************************/
36:
37: /**
38: * This is the model class for table "x2_list_items".
39: *
40: * @package application.models
41: * @property integer $contactId
42: * @property integer $listId
43: * @property string $code
44: * @property integer $result
45: */
46: class X2ListItem extends CActiveRecord {
47: /**
48: * Returns the static model of the specified AR class.
49: * @return ContactListItem the static model class
50: */
51: public static function model($className=__CLASS__) {
52: return parent::model($className);
53: }
54:
55: /**
56: * @return string the associated database table name
57: */
58: public function tableName() {
59: return 'x2_list_items';
60: }
61:
62: /**
63: * @return array validation rules for model attributes.
64: */
65: public function rules() {
66: // NOTE: you should only define rules for those attributes that
67: // will receive user inputs.
68: return array(
69: array('listId', 'required'),
70: array(
71: 'contactId, listId, sent, opened, clicked, unsubscribed',
72: 'numerical',
73: 'integerOnly'=>true
74: ),
75: array('uniqueId', 'length', 'max'=>32),
76: // The following rule is used by search().
77: // Please remove those attributes that should not be searched.
78: array('contactId, listId, uniqueId, result, opened', 'safe', 'on'=>'search'),
79: );
80: }
81:
82: /**
83: * @return array relational rules.
84: */
85: public function relations() {
86: // NOTE: you may need to adjust the relation name and the related
87: // class name for the relations automatically generated below.
88: return array(
89: 'list'=>array(self::BELONGS_TO, 'X2List', 'listId'),
90: 'contact'=>array(self::BELONGS_TO, 'Contacts', 'contactId'),
91: );
92: }
93:
94: /**
95: * Yii needs this since this model does not have a primary key column in db
96: * If this isn't here, referring to this as a relation in other models will fail
97: * -Commented out since this started causing issues in Yii 1.1.16-
98: */
99: // public function primaryKey() {
100: // return array('id','contactId','listId');
101: // }
102:
103: /**
104: * @return array customized attribute labels (name=>label)
105: */
106: public function attributeLabels() {
107: return array(
108: 'contactId' => 'Contact',
109: 'listId' => 'List',
110: 'uniqueId' => 'Code',
111: 'error' => 'Error',
112: 'sent' => 'Email Sent',
113: 'opened' => 'Opened Emal',
114: 'clicked' => 'Clicked Link',
115: 'unsubscribed' => 'Unsubscribed',
116: );
117: }
118:
119: /**
120: * Retrieves a list of models based on the current search/filter conditions.
121: * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
122: */
123: public function search() {
124: // Warning: Please modify the following code to remove attributes that
125: // should not be searched.
126: $criteria=new CDbCriteria;
127:
128: $criteria->compare('contactId',$this->contactId,true);
129: $criteria->compare('listId',$this->listId,true);
130: $criteria->compare('uniqueId',$this->uniqueId,true);
131: $criteria->compare('error',$this->error,true);
132: $criteria->compare('sent',$this->sent,true);
133: $criteria->compare('opened',$this->opened,true);
134: $criteria->compare('clicked',$this->clicked,true);
135: $criteria->compare('unsubscribed',$this->unsubscribed,true);
136:
137: return new CActiveDataProvider(get_class($this), array(
138: 'criteria'=>$criteria,
139: ));
140: }
141:
142: /**
143: * Marks this campaign/newsletter item as opened.
144: */
145: public function markOpened() {
146: if($this->opened == 0) {
147: $this->opened = time();
148: $this->update(array('opened'));
149: }
150:
151: if($this->list->campaign !== null) {
152: if($this->contact !== null) {
153: X2Flow::trigger('CampaignEmailOpenTrigger',array(
154: 'model'=>$this->contact,
155: 'campaign'=>$this->list->campaign->name
156: ));
157: } else {
158: X2Flow::trigger('NewsletterEmailOpenTrigger',array(
159: 'item'=>$this,
160: 'email'=>$this->emailAddress,
161: 'campaign'=>$this->list->campaign->name,
162: ));
163: }
164: }
165: }
166:
167: /**
168: * Marks this campaign/newsletter item as clicked.
169: *
170: *@param string $url the URL of the link clicked on
171: */
172: public function markClicked($url) {
173: if($this->opened == 0)
174: $this->markOpened(); // mark as opened, run automation for email open
175:
176: if($this->clicked == 0) {
177: $this->clicked = time();
178: $this->update(array('clicked'));
179: }
180:
181: if($this->list->campaign !== null) {
182: if($this->contact !== null) {
183: X2Flow::trigger('CampaignEmailClickTrigger',array(
184: 'model'=>$this->contact,
185: 'campaign'=>$this->list->campaign->name,
186: 'url'=>$url
187: ));
188: } else {
189: X2Flow::trigger('NewsletterEmailClickTrigger',array(
190: 'item'=>$this,
191: 'email'=>$this->emailAddress,
192: 'campaign'=>$this->list->campaign->name,
193: 'url'=>$url
194: ));
195: }
196: }
197: }
198:
199: /**
200: * Marks this campaign/newsletter item as unsubscribed.
201: * If a contact record is available, unsubscribe them from all other lists as well.
202: */
203: public function unsubscribe($email=null) {
204: if($this->opened == 0)
205: $this->markOpened(); // mark as opened, run automation for email open
206:
207: if($this->unsubscribed == 0) {
208: $this->unsubscribed = time();
209: $this->update(array('unsubscribed'));
210: }
211: // unsubscribe this email from all other newsletters
212: CActiveRecord::model('X2ListItem')->updateAll(array('unsubscribed'=>time()),'emailAddress=:email AND unsubscribed=0',array('email'=>$this->emailAddress));
213:
214: if($this->list->campaign !== null) {
215: if($this->contact !== null) { // regular campaign
216: // update the contact
217: $this->contact->doNotEmail = true;
218: $this->contact->lastActivity = time();
219: $this->contact->update(array('doNotEmail','lastActivity'));
220:
221: X2Flow::trigger('CampaignUnsubscribeTrigger',array(
222: 'model'=>$this->contact,
223: 'campaign'=>$this->list->campaign->name
224: ));
225: } elseif(isset($this->list)) { // no contact, must be a newsletter
226:
227: X2Flow::trigger('NewsletterUnsubscribeTrigger',array(
228: 'item'=>$this,
229: 'email'=>$this->emailAddress,
230: 'campaign'=>$this->list->campaign->name
231: ));
232: }
233: }
234: }
235: }
236: