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: Yii::import ('application.components.sortableWidget.ChartWidget');
38:
39: 40: 41:
42: class EventsChartProfileWidget extends ChartWidget {
43:
44: public $canBeDeleted = true;
45:
46: public $defaultTitle = 'Events';
47:
48: public $relabelingEnabled = true;
49:
50: public $template = '<div class="submenu-title-bar widget-title-bar">{widgetLabel}{chartSubtypeSelector}{closeButton}{minimizeButton}{settingsMenu}</div>{widgetContents}';
51:
52: public $chartType = 'eventsChart';
53:
54: private static $_JSONPropertiesStructure;
55:
56: public static function getJSONPropertiesStructure () {
57: if (!isset (self::$_JSONPropertiesStructure)) {
58: self::$_JSONPropertiesStructure = array_merge (
59: parent::getJSONPropertiesStructure (),
60: array (
61: 'label' => 'Events',
62: 'chartSettings' => array (
63: 'startDate' => null,
64: 'endDate' => null,
65: 'dateRange' => null,
66: 'dateRangeType' => null,
67: 'binSize' => null,
68: 'firstMetric' => null,
69: 'showRelationships' => null,
70: 'chartSetting' => null,
71: 'usersFilter' => null,
72: 'socialSubtypesFilter' => null,
73: 'visibilityFilter' => null,
74: ),
75: )
76: );
77: }
78: return self::$_JSONPropertiesStructure;
79: }
80:
81: 82: 83: 84:
85: public static function getChartData ($startTimestamp, $endTimestamp){
86: $command = Yii::app()->db->createCommand()
87: ->select(
88: 'type, subtype, visibility, user,'.
89: 'timestamp, COUNT(type) AS count,'.
90: 'YEAR(FROM_UNIXTIME(timestamp)) AS year,'.
91: 'MONTH(FROM_UNIXTIME(timestamp)) AS month,'.
92: 'WEEK(FROM_UNIXTIME(timestamp)) AS week,'.
93: 'DAY(FROM_UNIXTIME(timestamp)) AS day,'.
94: 'HOUR(from_unixtime(timestamp)) as hour')
95: ->from('x2_events');
96: $command->where(
97: 'timestamp BETWEEN :startTimestamp AND :endTimestamp',
98: array('startTimestamp' => $startTimestamp, 'endTimestamp' => $endTimestamp));
99: $events = $command->group(
100: 'HOUR(FROM_UNIXTIME(timestamp)),'.
101: 'DAY(FROM_UNIXTIME(timestamp)),'.
102: 'WEEK(FROM_UNIXTIME(timestamp)),'.
103: 'MONTH(FROM_UNIXTIME(timestamp)),'.
104: 'YEAR(FROM_UNIXTIME(timestamp)),'.
105: 'timestamp, type, subtype, visibility, user')
106: ->order('year DESC, month DESC, week DESC, day DESC, hour desc')
107: ->queryAll();
108: return $events;
109: }
110:
111:
112: 113: 114:
115: public static function getChartSettingsProvider () {
116: $chartSettingsDataProvider = new CActiveDataProvider('ChartSetting', array(
117: 'criteria' => array(
118: 'condition' =>
119: 'userId='.Yii::app()->user->id.' AND '.
120: 'chartType="eventsChart"',
121: 'order' => 'name ASC'
122: )
123: ));
124: return $chartSettingsDataProvider;
125: }
126:
127: 128: 129: 130:
131: public function getSetupScript () {
132: if (!isset ($this->_setupScript)) {
133: $widgetClass = get_called_class ();
134: $chartData = $this->getInitialChartData ();
135: $userNames = User::getNames ();
136: $socialSubtypes = Dropdowns::getSocialSubtypes ();
137: $visibilityFilters = array (
138: '1'=>'Public',
139: '0'=>'Private',
140: );
141: $chartSettingsData = self::getChartSettingsProvider ($this->chartType)->data;
142: $this->_setupScript = parent::getSetupScript ()."
143: $(function () {
144: var chartUID = '$this->chartType$this->widgetUID';
145: x2[chartUID] = {};
146: x2[chartUID].chart = X2Chart.instantiateTemporarySubtype (
147: X2EventsChart, {
148: ".(isset ($chartData) ?
149: "chartData :".CJSON::encode ($chartData)."," : '')."
150: actionParams: ".CJSON::encode (array (
151: 'widgetType' => get_called_class (),
152: )).",
153: socialSubtypes:".CJSON::encode (array_keys ($socialSubtypes)).",
154: visibilityTypes:".CJSON::encode (array_keys ($visibilityFilters)).",
155: eventTypes:".CJSON::encode (array_keys ($userNames)).",
156: translations: ".CJSON::encode ($this->getTranslations ()).",
157: getChartDataActionName: 'getEventsBetween',
158: saveChartSetting: function (key, value, callback) {
159: this.lastChartSettings[key] = value;
160: x2.$widgetClass$this->widgetUID.setProperty (
161: 'chartSettings', this.lastChartSettings, callback);
162: },
163: suppressDateRangeSelector: false,
164: suppressChartSettings: false,
165: lastChartSettings: ".CJSON::encode ($this->getChartSettings ()).",
166: widgetUID: '$this->widgetUID',
167: chartType: '$this->chartType',
168: chartSubtype: '".self::getJSONProperty (
169: $this->profile, 'chartSubtype', $this->widgetType, $this->widgetUID)."',
170: chartSettings: ".CJSON::encode (
171: count ($chartSettingsData) ? array_combine (
172: array_map (function ($setting) {
173: return $setting->name;
174: }, $chartSettingsData),
175: $chartSettingsData) : array ())."
176: });
177: $(document).trigger ('$this->chartType' + 'Ready');
178: });
179: ";
180: }
181: return $this->_setupScript;
182: }
183:
184: public function getViewFileParams () {
185: if (!isset ($this->_viewFileParams)) {
186: $this->_viewFileParams = array_merge (
187: parent::getViewFileParams (),
188: array (
189: 'userNames' => User::getNames (),
190: 'socialSubtypes' => Dropdowns::getSocialSubtypes (),
191: 'visibilityFilters' => array (
192: '1'=>'Public',
193: '0'=>'Private',
194: ),
195: 'chartSettingsDataProvider' => self::getChartSettingsProvider (
196: $this->chartType),
197: 'suppressChartSettings' => false,
198: 'metricTypes' => array (
199: 'any'=>Yii::t('app', 'All Events'),
200: 'notif'=>Yii::t('app', 'Notifications'),
201: 'feed'=>Yii::t('app', 'Feed Events'),
202: 'comment'=>Yii::t('app', 'Comments'),
203: 'record_create'=>Yii::t('app', 'Records Created'),
204: 'record_deleted'=>Yii::t('app', 'Records Deleted'),
205: 'weblead_create'=>Yii::t('app', 'Webleads Created'),
206: 'workflow_start'=>Yii::t('app', '{Process} Started', array(
207: '{Process}' => Modules::displayName(false, 'Workflow')
208: )),
209: 'workflow_complete'=>Yii::t('app', '{Process} Complete', array(
210: '{Process}' => Modules::displayName(false, 'Workflow')
211: )),
212: 'workflow_revert'=>Yii::t('app', '{Process} Reverted', array(
213: '{Process}' => Modules::displayName(false, 'Workflow')
214: )),
215: 'email_sent'=>Yii::t('app', 'Emails Sent'),
216: 'email_opened'=>Yii::t('app', 'Emails Opened'),
217: 'web_activity'=>Yii::t('app', 'Web Activity'),
218: 'case_escalated'=>Yii::t('app', 'Cases Escalated'),
219: 'calendar_event'=>Yii::t('app', '{Calendar} Events', array(
220: '{Calendar}' => Modules::displayName(false, 'Calendar')
221: )),
222: 'action_reminder'=>Yii::t('app', '{Action} Reminders', array(
223: '{Action}' => Modules::displayName(false, 'Actions')
224: )),
225: 'action_complete'=>Yii::t('app', '{Actions} Completed', array(
226: '{Actions}' => Modules::displayName(true, 'Actions')
227: )),
228: 'doc_update'=>Yii::t('app', 'Doc Updates'),
229: 'email_from'=>Yii::t('app', 'Email Received'),
230: 'voip_calls'=>Yii::t('app', 'VOIP Calls'),
231: 'media'=>Yii::t('app', '{Media}', array(
232: '{Media}' => Modules::displayName(true, 'Media')
233: ))
234: ),
235: 'chartType' => 'eventsChart',
236: 'widgetUID' => $this->widgetUID,
237: )
238: );
239: }
240: return $this->_viewFileParams;
241: }
242:
243: 244: 245:
246: public function getPackages () {
247: if (!isset ($this->_packages)) {
248: $this->_packages = array_merge (
249: parent::getPackages (),
250: array (
251: 'EventsChartProfileWidgetJS' => array(
252: 'baseUrl' => Yii::app()->request->baseUrl,
253: 'js' => array(
254: 'js/X2Chart/X2EventsChart.js',
255: ),
256: 'depends' => array ('ChartWidgetJS')
257: ),
258: )
259: );
260: }
261: return $this->_packages;
262: }
263:
264: 265: 266:
267: protected function getTranslations () {
268: if (!isset ($this->_translations )) {
269: $this->_translations = array_merge (
270: parent::getTranslations (),
271: array (
272: 'metric1Label' => Yii::t('app', 'metric(s) selected'),
273: 'user(s) selected' => Yii::t('app', 'user(s) selected'),
274: 'event subtype(s) selected' => Yii::t('app', 'event subtype(s) selected'),
275: 'visibility setting(s) selected' => Yii::t(
276: 'app', 'visibility setting(s) selected'),
277: )
278: );
279: }
280: return $this->_translations;
281: }
282:
283: 284: 285: 286:
287: protected function getInitialChartData () {
288: 289: 290: 291:
292: if (self::getJSONProperty (
293: $this->profile, 'minimized', $this->widgetType, $this->widgetUID)) {
294:
295: $tsDict = $this->getStartEndTimestamp (time () - self::SECPERWEEK, time ());
296: $startDate = $tsDict[0];
297: $endDate = $tsDict[1];
298: $events = self::getChartData ($startDate, $endDate);
299: return $events;
300: }
301: }
302:
303: }
304: ?>
305: