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: * Made for the purposes of getting pagingation to work properly.
39: *
40: * @package application.components
41: */
42: class SmartDataProviderBehavior extends CBehavior {
43:
44: public $settingsBehavior;
45:
46: private $_pagination;
47:
48: public function attach ($owner) {
49: parent::attach ($owner);
50: $this->attachBehaviors (array (
51: 'settingsBehavior' => array (
52: 'class' => $this->settingsBehavior,
53: 'uid' => $this->owner->uid,
54: 'modelClass' => $this->owner->modelClass,
55: )
56: ));
57: }
58:
59: /**
60: * Unsets sort order if sort is on an attribute not in list of specified attributes
61: * @param array $attrs names of model attributes
62: * @return bool true if sort order was unset, false otherwise
63: */
64: // public function unsetSortOrderIfNotIn (array $attrs) {
65: // $sortOrder = $this->getSetting ('sort');
66: // $sortOrderUnset = false;
67: // if (!empty ($sortOrder) && !in_array (preg_replace ('/\.desc$/', '', $sortOrder), $attrs)) {
68: // $sortOrder = '';
69: // unset ($_GET[$this->owner->modelClass][$this->getSortKey ()]);
70: // $sortOrderUnset = true;
71: // }
72: // $this->saveSetting ('sort', $sortOrder);
73: // return $sortOrderUnset;
74: //
75: // }
76:
77: public function getSortKey () {
78: return $this->owner->getId()!='' ? $this->owner->getId().'_sort' : 'sort';
79: }
80:
81: public function getPageKey () {
82: return $this->owner->getId()!='' ? $this->owner->getId().'_page' : 'page';
83: }
84:
85: public function getSessionPageKey () {
86: return $this->getStatePrefix ().$this->getPageKey ();
87: }
88:
89: public function storeSettings () {
90:
91: //Sort and page saving code modified from:
92: //http://www.stupidannoyingproblems.com/2012/04/yii-grid-view-remembering-filters-pagination-and-sort-settings/
93:
94: // sort order gets saved in db or session depending on settingsBehavior
95: $key = $this->getSortKey ();
96:
97: if(!empty($_GET[$key])){
98: if (!$this->owner->disablePersistentGridSettings)
99: $val = $this->saveSetting ('sort', $_GET[$key]);
100: } else {
101: if (!$this->owner->disablePersistentGridSettings)
102: $val = $this->getSetting ('sort');
103: if(!empty($val))
104: $_GET[$key] = $val;
105: }
106:
107: // active page always gets stored in session
108: $key = $this->getPageKey ();
109: $statePrefix = $this->getStatePrefix ();
110: if(!empty($_GET[$key])){
111: Yii::app()->user->setState($this->getSessionPageKey (), $_GET[$key]);
112: } elseif(!empty($_GET["ajax"])){
113: // page 1 passes no page number, just an ajax flag
114: Yii::app()->user->setState($this->getSessionPageKey (), 1);
115: } else {
116: $val = Yii::app()->user->getState($this->getSessionPageKey ());
117: if(!empty($val))
118: $_GET[$key] = $val;
119: }
120:
121: }
122:
123: /**
124: * Returns the pagination object.
125: * @return CPagination the pagination object. If this is false, it means the pagination is
126: * disabled.
127: */
128: public function getSmartPagination() {
129: if($this->_pagination===null) {
130: $this->_pagination=new RememberPagination;
131: if(($id=$this->owner->getId())!='')
132: $this->_pagination->pageVar=$id.'_page';
133: }
134: return $this->_pagination;
135: }
136:
137: }
138: