1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36:
37:
38: 39: 40: 41: 42:
43: class History extends X2Widget {
44:
45: public $associationType;
46: public $associationId = '';
47: public $filters = true;
48: public $historyType = 'all';
49: public $pageSize = 10;
50: public $relationships = 0;
51:
52: public static function getCriteria(
53: $associationId, $associationType, $relationships, $historyType) {
54:
55:
56: $historyCriteria = array(
57: 'all' => '',
58: 'action' => ' AND type IS NULL',
59: 'overdueActions' => ' AND type IS NULL AND complete="NO" AND dueDate <= ' . time(),
60: 'incompleteActions' => ' AND type IS NULL AND complete="NO"',
61: 'call' => ' AND type="call"',
62: 'note' => ' AND type="note"',
63: 'attachments' => ' AND type="attachment"',
64: 'event' => ' AND type="event"',
65: 'email' =>
66: ' AND type IN ("email","email_staged","emailFrom",' .
67: '"email_opened","email_clicked","email_unsubscribed")',
68: 'marketing' =>
69: ' AND type IN ("email","webactivity","weblead","email_staged",' .
70: '"email_opened","email_clicked","email_unsubscribed","event")',
71:
72: 'quotes' => 'AND type like "quotes%"',
73: 'time' => ' AND type="time"',
74: 'webactivity' => 'AND type IN ("weblead","webactivity")',
75: 'workflow' => ' AND type="workflow"',
76: );
77: $multiAssociationIds = array($associationId);
78: if ($relationships) {
79:
80: $type = $associationType;
81: $model = X2Model::model($type)->findByPk($associationId);
82: if (count($model->relatedX2Models) > 0) {
83: $associationCondition = "((associationId={$associationId} AND " .
84: "associationType='{$associationType}')";
85:
86: foreach ($model->relatedX2Models as $relatedModel) {
87: if ($relatedModel instanceof X2Model) {
88: $multiAssociationIds[] = $relatedModel->id;
89: $associationCondition .=
90: " OR (associationId={$relatedModel->id} AND " .
91: "associationType='{$relatedModel->myModelName}')";
92: }
93: }
94: $associationCondition.=")";
95: } else {
96: $associationCondition = 'associationId=' . $associationId . ' AND ' .
97: 'associationType="' . $associationType . '"';
98: }
99: } else {
100: $associationCondition = 'associationId=' . $associationId . ' AND ' .
101: 'associationType="' . $associationType . '"';
102: }
103: 104:
105: $associationCondition = str_replace('Opportunity', 'opportunities', $associationCondition);
106: $associationCondition = str_replace('Quote', 'quotes', $associationCondition);
107: $visibilityCondition = '';
108: $module = isset(Yii::app()->controller->module) ?
109: Yii::app()->controller->module->getId() : Yii::app()->controller->getId();
110:
111: if (!Yii::app()->user->checkAccess(ucfirst ($module) . 'Admin')) {
112: if (Yii::app()->settings->historyPrivacy == 'user') {
113: $visibilityCondition = ' AND (assignedTo="' . Yii::app()->user->getName() . '")';
114: } elseif (Yii::app()->settings->historyPrivacy == 'group') {
115: $visibilityCondition = ' AND (
116: t.assignedTo IN (
117: SELECT DISTINCT b.username
118: FROM x2_group_to_user a
119: INNER JOIN x2_group_to_user b ON a.groupId=b.groupId
120: WHERE a.username="' . Yii::app()->user->getName() . '") OR
121: (t.assignedTo="' . Yii::app()->user->getName() . '"))';
122: } else {
123: $visibilityCondition = ' AND (visibility="1" OR assignedTo="' . Yii::app()->user->getName() . '")';
124: }
125: }
126: $orderStr = 'IF(complete="No", GREATEST(createDate, IFNULL(dueDate,0), ' .
127: 'IFNULL(lastUpdated,0)), GREATEST(createDate, ' .
128: 'IFNULL(completeDate,0), IFNULL(lastUpdated,0))) DESC';
129: $mainCountCmd = Yii::app()->db->createCommand()
130: ->select('COUNT(*)')
131: ->from('x2_actions t')
132: ->where($associationCondition .
133: $visibilityCondition . $historyCriteria[$historyType]);
134: $mainCmd = Yii::app()->db->createCommand()
135: ->select('*')
136: ->from('x2_actions t')
137: ->where($associationCondition .
138: $visibilityCondition . $historyCriteria[$historyType])
139: ->order($orderStr);
140:
141: $multiAssociationIdParams = AuxLib::bindArray($multiAssociationIds);
142: $associationCondition = '((' . $associationCondition . ') OR ' .
143: 'x2_action_to_record.recordId in ' . AuxLib::arrToStrList(
144: array_keys($multiAssociationIdParams)) . ')';
145:
146: $associationCondition = 'x2_action_to_record.recordId in ' . AuxLib::arrToStrList(
147: array_keys($multiAssociationIdParams));
148: $joinCountCmd = Yii::app()->db->createCommand()
149: ->select('COUNT(*)')
150: ->from('x2_actions t')
151: ->join('x2_action_to_record', 'actionId=t.id')
152: ->where($associationCondition . $visibilityCondition . $historyCriteria[$historyType] . ' AND
153: x2_action_to_record.recordType=:recordType');
154: $joinCmd = Yii::app()->db->createCommand()
155: ->select('t.*')
156: ->from('x2_actions t')
157: ->join('x2_action_to_record', 'actionId=t.id')
158: ->where($associationCondition . $visibilityCondition . $historyCriteria[$historyType] . ' AND
159: x2_action_to_record.recordType=:recordType');
160:
161: $count = $mainCountCmd->union($joinCountCmd->getText())->queryScalar(array_merge(array(':recordType' => X2Model::getModelName($associationType)), $multiAssociationIdParams));
162: return array(
163: 'cmd' => $mainCmd->union($joinCmd->getText()),
164: 'count' => $count,
165: 'params' => array_merge(array(':recordType' => X2Model::getModelName($associationType)), $multiAssociationIdParams));
166: }
167:
168: 169: 170:
171: public function run() {
172: if ($this->filters) {
173:
174: $historyTabs = array(
175: 'all' => Yii::t('app', 'All'),
176: 'action' => Yii::t('app', '{actions}', array(
177: '{actions}' => Modules::displayName(true, 'Actions'),
178: )),
179: 'overdueActions' => Yii::t('app', 'Overdue {actions}', array(
180: '{actions}' => Modules::displayName(true, 'Actions'),
181: )),
182: 'incompleteActions' => Yii::t('app', 'Incomplete {actions}', array(
183: '{actions}' => Modules::displayName(true, 'Actions'),
184: )),
185: 'attachments' => Yii::t('app', 'Attachments'),
186: 'call' => Yii::t('app', 'Calls'),
187: 'note' => Yii::t('app', 'Comments'),
188: 'email' => Yii::t('app', 'Emails'),
189: 'event' => Yii::t('app', 'Events'),
190: 'marketing' => Yii::t('app', 'Marketing'),
191: 'time' => Yii::t('app', 'Logged Time'),
192:
193: 'quotes' => Yii::t('app', 'Quotes'),
194: 'webactivity' => Yii::t('app', 'Web Activity'),
195: 'workflow' => Yii::t('app', '{process}', array(
196: '{process}' => Modules::displayName(true, 'Workflow'),
197: )),
198: );
199: $profile = Yii::app()->params->profile;
200: if (isset($profile)) {
201:
202: $this->pageSize = $profile->historyShowAll ? 10000 : 10;
203: $this->relationships = $profile->historyShowRels;
204: }
205:
206: if (isset($_GET['history']) && array_key_exists($_GET['history'], $historyTabs)) {
207: $this->historyType = $_GET['history'];
208: }
209: if (isset($_GET['pageSize'])) {
210: $this->pageSize = $_GET['pageSize'];
211: if (isset($profile)) {
212:
213: $profile->historyShowAll = $this->pageSize > 10 ? 1 : 0;
214: $profile->update(array('historyShowAll'));
215: }
216: }
217: if (isset($_GET['relationships'])) {
218: $this->relationships = $_GET['relationships'];
219: if (isset($profile)) {
220: $profile->historyShowRels = $this->relationships;
221: $profile->update(array('historyShowRels'));
222: }
223: }
224: } else {
225: $historyTabs = array();
226: }
227:
228: Yii::app()->clientScript->registerScriptFile(
229: Yii::app()->getBaseUrl() . '/js/ActionHistory.js');
230: Yii::app()->clientScript->registerScriptFile(
231: Yii::app()->getBaseUrl() . '/js/EnlargeableImage.js',
232: CClientScript::POS_END);
233: Yii::app()->clientScript->registerScript('history-tabs', "
234: x2.actionHistory = new x2.ActionHistory ({
235: relationshipFlag: {$this->relationships}
236: });
237: ", CClientScript::POS_END);
238:
239: $this->widget('application.components.X2ListView', array(
240: 'pager' => array(
241: 'class' => 'CLinkPager',
242: 'header' => '',
243: 'firstPageCssClass' => '',
244: 'lastPageCssClass' => '',
245: 'prevPageLabel' => '<',
246: 'nextPageLabel' => '>',
247: 'firstPageLabel' => '<<',
248: 'lastPageLabel' => '>>',
249: ),
250: 'id' => 'history',
251: 'dataProvider' => $this->getHistory(),
252: 'viewData' => array(
253:
254: 'relationshipFlag' => $this->relationships,
255: ),
256: 'itemView' => 'application.modules.actions.views.actions._historyView',
257: 'htmlOptions' => array('class' => 'action list-view'),
258: 'template' =>
259: '<div class="form action-history-controls">' .
260: CHtml::dropDownList(
261: 'history-selector', $this->historyType, $historyTabs, array(
262: 'class' => 'x2-select'
263: )
264: ) .
265: '<span style="margin-top:5px;" class="right">' .
266: CHtml::link(
267: Yii::t('app', 'Toggle Text'), '#', array(
268: 'id' => 'history-collapse', 'class' => 'x2-hint',
269: 'title' =>
270: Yii::t('app', 'Click to toggle showing the full text of History items.')
271: )
272: )
273: . ' | ' . CHtml::link(
274: Yii::t('app', 'Show All'), '#', array(
275: 'id' => 'show-history-link', 'class' => 'x2-hint',
276: 'title' =>
277: Yii::t('app', 'Click to increase the number of History items shown.'),
278: 'style' => $this->pageSize > 10 ? 'display:none;' : ''
279: )
280: )
281: . CHtml::link(
282: Yii::t('app', 'Show Less'), '#', array(
283: 'id' => 'hide-history-link', 'class' => 'x2-hint', '
284: title' =>
285: Yii::t('app', 'Click to decrease the number of History items shown.'),
286: 'style' => $this->pageSize > 10 ? '' : 'display:none;'
287: )
288: )
289: . ((!Yii::app()->user->isGuest) ?
290: ' | ' . CHtml::link(
291: Yii::t('app', 'Relationships'), '#', array(
292: 'id' => 'show-relationships-link', 'class' => 'x2-hint',
293: 'title' =>
294: Yii::t('app', 'Click to toggle showing actions associated with related records.'))) : '')
295: . '</span></div> {sorter}{items}{pager}',
296: ));
297: }
298:
299: 300: 301: 302: 303:
304: public function getHistory() {
305: $historyCmd = self::getCriteria(
306: $this->associationId, $this->associationType, $this->relationships, $this->historyType);
307: return new CSqlDataProvider($historyCmd['cmd'], array(
308: 'totalItemCount' => $historyCmd['count'],
309: 'params' => $historyCmd['params'],
310: 'pagination' => array(
311: 'pageSize' => $this->pageSize,
312: ),
313: ));
314: }
315:
316: }
317: