1: <?php
2:
3: /*****************************************************************************************
4: * X2Engine Open Source Edition is a customer relationship management program developed by
5: * X2Engine, Inc. Copyright (C) 2011-2016 X2Engine Inc.
6: *
7: * This program is free software; you can redistribute it and/or modify it under
8: * the terms of the GNU Affero General Public License version 3 as published by the
9: * Free Software Foundation with the addition of the following permission added
10: * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11: * IN WHICH THE COPYRIGHT IS OWNED BY X2ENGINE, X2ENGINE DISCLAIMS THE WARRANTY
12: * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13: *
14: * This program is distributed in the hope that it will be useful, but WITHOUT
15: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16: * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
17: * details.
18: *
19: * You should have received a copy of the GNU Affero General Public License along with
20: * this program; if not, see http://www.gnu.org/licenses or write to the Free
21: * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22: * 02110-1301 USA.
23: *
24: * You can contact X2Engine, Inc. P.O. Box 66752, Scotts Valley,
25: * California 95067, USA. or at email address [email protected].
26: *
27: * The interactive user interfaces in modified source and object code versions
28: * of this program must display Appropriate Legal Notices, as required under
29: * Section 5 of the GNU Affero General Public License version 3.
30: *
31: * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32: * these Appropriate Legal Notices must retain the display of the "Powered by
33: * X2Engine" logo. If the display of the logo is not reasonably feasible for
34: * technical reasons, the Appropriate Legal Notices must display the words
35: * "Powered by X2Engine".
36: *****************************************************************************************/
37:
38: /**
39: * This is the model class for table "x2_track_emails". Inline Email Form
40: * uses this model to add a unique id to the action that was generated when
41: * the email was sent. This uniqueId is used in /actions/emailOpened($uid)
42: * to keep track of which emails have been opened by the Contact.
43: *
44: * @package application.models
45: * @property integer $id
46: * @property integer $actionId
47: * @property string $uniqueId
48: */
49: class TrackEmail extends CActiveRecord {
50:
51: /**
52: * Returns the static model of the specified AR class.
53: * @return Tags the static model class
54: */
55: public static function model($className = __CLASS__) {
56: return parent::model($className);
57: }
58:
59: /**
60: * @return string the associated database table name
61: */
62: public function tableName() {
63: return 'x2_track_emails';
64: }
65:
66: /**
67: * @return array validation rules for model attributes.
68: */
69: public function rules() {
70: // NOTE: you should only define rules for those attributes that
71: // will receive user inputs.
72: return array(
73: );
74: }
75:
76: /**
77: * @return array relational rules.
78: */
79: public function relations() {
80: // NOTE: you may need to adjust the relation name and the related
81: // class name for the relations automatically generated below.
82: return array(
83: 'action' => array(self::BELONGS_TO, 'Actions', 'actionId'),
84: );
85: }
86:
87: /**
88: * @return array customized attribute labels (name=>label)
89: */
90: public function attributeLabels() {
91: return array(
92: );
93: }
94:
95: /**
96: * Retrieves a list of models based on the current search/filter conditions.
97: * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
98: */
99: public function search() {
100: // Warning: Please modify the following code to remove attributes that
101: // should not be searched.
102:
103: $criteria = new CDbCriteria;
104:
105: return new CActiveDataProvider(get_class($this),
106: array(
107: 'criteria' => $criteria,
108: ));
109: }
110:
111: public function recordEmailOpen() {
112: if ($this->opened === null) {
113: $openedAction = $this->createEmailOpenedAction();
114: $event = $this->createEmailOpenedEvent();
115: if ($openedAction->save() && $event->save()) {
116: $this->opened = time();
117: $this->update();
118: $model = X2Model::getAssociationModel($openedAction->associationType,
119: $openedAction->associationId);
120: X2Flow::trigger('EmailOpenTrigger',
121: array(
122: 'model' => $model,
123: ));
124: }
125: }
126: }
127:
128: private function createEmailOpenedAction() {
129: $now = time();
130: $action = new Actions;
131: $action->type = 'emailOpened';
132: $action->associationType = $this->action->associationType;
133: $action->associationId = $this->action->associationId;
134: $action->createDate = $now;
135: $action->lastUpdated = $now;
136: $action->completeDate = $now;
137: $action->complete = 'Yes';
138: $action->updatedBy = 'admin';
139: $action->associationName = $this->action->associationName;
140: $action->visibility = $this->action->visibility;
141: $action->assignedTo = $this->action->assignedTo;
142: $action->actionDescription = Yii::t('marketing',
143: '{recordType} has opened the email sent on ', array(
144: '{recordType}' => Modules::displayName(false,
145: $this->action->associationType)
146: ));
147: $action->actionDescription .= Formatter::formatLongDateTime($this->action->createDate) . "<br>";
148: $action->actionDescription .= $this->action->actionDescription;
149:
150: return $action;
151: }
152:
153: private function createEmailOpenedEvent() {
154: $event = new Events;
155: $event->type = 'email_opened';
156: $event->subtype = 'email';
157: $event->user = $this->action->assignedTo;
158: $event->associationType = $this->action->associationType;
159: $event->associationId = $this->action->associationId;
160:
161: return $event;
162: }
163:
164: }
165: