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