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: * Handles relationship grid attribute rendering and filtering
39: */
40:
41: class RelationshipsGridModel extends CModel {
42:
43: /**
44: * @var CActiveRecord $relatedModel
45: */
46: public $relatedModel;
47:
48: /**
49: * @var string $myModelName
50: */
51: public $myModel;
52:
53: /**
54: * @var int $id
55: */
56: public $id;
57:
58: /**
59: * Added these to fix the readonly error
60: */
61: public $name;
62:
63: public $assignedTo;
64:
65: public $label;
66:
67: public $createDate;
68:
69: public function __construct ($scenario=null) {
70: if ($scenario) {
71: $this->setScenario ($scenario);
72: }
73: }
74:
75: /**
76: * Set attributes with attributes of embedded model or values specified in GET params
77: */
78: public function init () {
79: $scenario = $this->getScenario ();
80: if ($scenario === 'search' && isset ($_GET[get_called_class ()])) {
81: $this->setAttributes ($_GET[get_called_class ()], false);
82: } elseif ($this->relatedModel) {
83: $this->setAttributes ($this->relatedModel->getAttributes (), false);
84: $this->label = $this->relatedModel->getRelationshipLabel ($this->myModel);
85: }
86: }
87:
88: public function attributeNames () {
89: return array (
90: 'id',
91: 'name',
92: 'relatedModelName',
93: 'assignedTo',
94: 'label',
95: 'createDate',
96: );
97: }
98:
99: public function renderAttribute ($name) {
100: switch ($name) {
101: case 'name':
102: echo $this->relatedModel->getLink (/*array (
103: 'class' => 'quick-read-link',
104: 'data-id' => $this->relatedModel->id,
105: 'data-class' => get_class ($this->relatedModel),
106: 'data-name' => CHtml::encode ($this->relatedModel->name),
107: )*/);
108: break;
109: case 'relatedModelName':
110: echo $this->getRelatedModelName ();
111: break;
112: case 'assignedTo':
113: echo $this->relatedModel->renderAttribute ('assignedTo');
114: break;
115: case 'label':
116: echo $this->label;
117: break;
118: case 'createDate':
119: echo isset ($this->createDate) ?
120: X2Html::dynamicDate ($this->createDate) : '';
121: break;
122: }
123: }
124:
125: public function filterModels (array $gridModels) {
126: $filteredModels = array ();
127: $that = $this;
128: $filters = array_filter ($this->attributeNames (), function ($a) use ($that) {
129: return $that->$a !== '' && $that->$a !== null;
130: });
131:
132: foreach ($gridModels as $model) {
133: $filterOut = false;
134: foreach ($filters as $filter) {
135: $val = $this->$filter;
136: switch ($filter) {
137: case 'name':
138: $filterOut = !preg_match (
139: '/'.$val.'/i',
140: $model->relatedModel->getAttribute ('name'));
141: break;
142: case 'relatedModelName':
143: $filterOut = $val !== get_class ($model->relatedModel);
144: break;
145: case 'assignedTo':
146: $filterOut = !preg_match (
147: '/'.$val.'/i',
148: $model->relatedModel->getAttribute ('assignedTo.fullName'));
149: break;
150: case 'label':
151: $filterOut = !preg_match (
152: '/'.$val.'/i',
153: $model->relatedModel->getRelationshipLabel ($this->myModel));
154: break;
155: case 'createDate':
156: $timestampA = Formatter::parseDate ($val);
157: $timestampB = $model->relatedModel->getAttribute ('createDate');
158:
159: // compare days since UNIX epoch
160: $filterOut = floor ($timestampA / (3600 * 24)) !==
161: floor ($timestampB / (3600 * 24));
162: break;
163: }
164: if ($filterOut) break;
165: }
166: if (!$filterOut)
167: $filteredModels[] = $model;
168: }
169: return $filteredModels;
170: }
171:
172: // public function sortModels (array $gridModels, $sortKey) {
173: // if (!isset ($_GET[$sortKey])) return;
174: // $sortOrder = explode ('.', $_GET[$sortKey]);
175: // if (count ($sortOrder) > 1) $direction = $sortOrder[1];
176: // else $direction = 'asc';
177: // $sortAttr = $sortOrder[0];
178: // @usort ($gridModels, function ($a, $b) use ($sortAttr, $direction) {
179: // if ($a->$sortAttr < $b->$sortAttr) {
180: // return ($direction === 'asc' ? -1 : 1);
181: // } elseif ($a->$sortAttr > $b->$sortAttr) {
182: // return ($direction === 'asc' ? 1 : -1);
183: // } else {
184: // return 0;
185: // }
186: // });
187: // return $gridModels;
188: // }
189:
190: public function setRelatedModelName ($name) {
191: $this->_relatedModelType = $name;
192: }
193:
194: private $_relatedModelType;
195: public function getRelatedModelName () {
196: if (!isset ($this->_relatedModelType)) {
197: if (!isset ($this->relatedModel)) {
198: return ($this->_relatedModelType = null);
199: }
200: $title = Yii::app()->db->createCommand()
201: ->select("title")
202: ->from("x2_modules")
203: ->where("name = :name AND custom = 1")
204: ->bindValues(array(":name" => get_class ($this->relatedModel)))
205: ->queryScalar();
206: if ($title)
207: $this->_relatedModelType = $title;
208: else
209: $this->_relatedModelType = X2Model::getModelTitle (get_class ($this->relatedModel));
210: }
211: return $this->_relatedModelType;
212: }
213:
214: }
215:
216: ?>
217:
218:
219: