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: 44: 45: 46: 47: 48: 49:
50: class X2LinkableBehavior extends CActiveRecordBehavior {
51:
52: public $baseRoute;
53: public $viewRoute;
54: public $autoCompleteSource;
55: public $icon;
56:
57: 58: 59: 60:
61: private $_module;
62:
63: 64: 65: 66: 67:
68: public function attach($owner) {
69:
70: parent::attach($owner);
71:
72: if ($this->getModule() === null){
73:
74: if(isset($this->baseRoute)){
75:
76: $this->module = preg_replace(
77: '/\/.*/', '', preg_replace('/^\//', '', $this->baseRoute));
78: }else{
79:
80:
81: $this->module = strtolower(get_class($this->owner));
82: }
83: }
84:
85: if (!isset($this->baseRoute))
86: $this->baseRoute = '/' . $this->module;
87:
88: if (!isset($this->viewRoute)) {
89: $this->viewRoute = $this->baseRoute;
90: }
91: if (Yii::app()->params->isMobileApp) {
92: $this->viewRoute .= '/mobileView';
93: }
94:
95:
96: if(!isset($this->autoCompleteSource))
97: $this->autoCompleteSource =
98: $this->baseRoute.'/getItems?modelType='.get_class ($this->owner);
99: }
100:
101: 102: 103: 104:
105: public function getModule() {
106: if(isset($this->_module)) {
107: return $this->_module;
108: } else if(property_exists($this->owner,'module')) {
109: return $this->owner->module;
110: } else {
111: return null;
112: }
113: }
114:
115: 116: 117: 118: 119:
120: public function getUrl(){
121: $url = null;
122:
123: if(Yii::app()->controller instanceof CController && !Yii::app()->controller instanceof ProfileController) {
124: $url = Yii::app()->controller->createAbsoluteUrl($this->viewRoute, array('id' => $this->owner->id));
125: }
126: if(empty($url)) {
127: $url = Yii::app()->absoluteBaseUrl.'/index.php'.$this->viewRoute.'/'.$this->owner->id;
128: }
129: return $url;
130: }
131:
132: 133: 134: 135:
136: private static $_mobileLinkableRecordTypes;
137: public static function getMobileLinkableRecordTypes () {
138: if (!isset (self::$_mobileLinkableRecordTypes)) {
139: self::$_mobileLinkableRecordTypes = array_flip (array_merge (array (
140: 'Contacts',
141: 'Accounts',
142: 'X2Leads',
143: 'Opportunity',
144: 'User',
145: 'Product',
146: 'Quote',
147: 'BugReports',
148: ), Yii::app()->db->createCommand ("
149: select name
150: from x2_modules
151: where custom and name
152: ")->queryColumn ()));
153: }
154: return self::$_mobileLinkableRecordTypes;
155: }
156:
157: public static function isMobileLinkableRecordType ($type) {
158: $mobileLinkableRecordTypes = self::getMobileLinkableRecordTypes ();
159: return isset ($mobileLinkableRecordTypes[$type]);
160: }
161:
162: 163: 164: 165: 166:
167: public function getUrlLink($htmlOptions=array ()) {
168: if (Yii::app()->params->isMobileApp &&
169: !self::isMobileLinkableRecordType (get_class ($this->owner))) {
170:
171: return $this->owner->renderAttribute ('name');
172: }
173:
174: $name = ($this->owner->hasAttribute('name') || $this->owner->canGetProperty('name') ||
175: property_exists($this->owner, 'name')) ? $this->owner->name : '';
176: if(trim($name) == '') {
177: if ($this->owner->hasAttribute('fileName')) {
178: $name = $this->owner->fileName;
179: }
180: if(trim($name) == '') {
181: $name = $this->owner->hasAttribute('id') ? '#'.$this->owner->id : '';
182: }
183: }
184:
185: $url = $this->url;
186: if($this->owner instanceof Contacts){
187: return CHtml::link(
188: '<span>'.X2Html::encode($name).'</span>',
189: $url,
190: array_merge (array(
191: 'class'=>'contact-name'
192: ), $htmlOptions)
193: );
194: }else{
195: return CHtml::link(
196: '<span>'.X2Html::encode($name).'</span>',
197: $url,
198: $htmlOptions
199: );
200: }
201: }
202:
203: 204: 205: 206: 207:
208: public function getLink($htmlOptions=array ()) {
209: return $this->getUrlLink ($htmlOptions);
210: }
211:
212: 213: 214:
215: public function createLink() {
216: if(isset($this->owner->id))
217: return $this->getLink();
218: else
219: return $this->owner->name;
220: }
221:
222: 223: 224: 225: 226:
227: public function getAutoCompleteSource() {
228: return $this->autoCompleteSource;
229: }
230:
231: 232: 233: 234:
235: public static function getItems($term, $valueAttr='name', $nameAttr='id', $modelClass=null) {
236: if (!$modelClass)
237: $modelClass = Yii::app()->controller->modelClass;
238: $model = X2Model::model($modelClass);
239:
240: if (isset($model)) {
241: $modelClass::checkThrowAttrError (array ($valueAttr, $nameAttr));
242: $tableName = $model->tableName();
243: $qterm = $term . '%';
244: $params = array (
245: ':qterm' => $qterm,
246: );
247: $sql = "
248: SELECT $nameAttr as id, $valueAttr as value
249: FROM " . $tableName . " as t
250: WHERE $valueAttr LIKE :qterm";
251: if ($model->asa ('permissions')) {
252: list ($accessCond, $permissionsParams) = $model->getAccessSQLCondition ();
253: $sql .= ' AND '.$accessCond;
254: $params = array_merge ($params, $permissionsParams);
255: }
256:
257: $sql .= "ORDER BY $valueAttr ASC";
258: $command = Yii::app()->db->createCommand($sql);
259: $result = $command->queryAll(true, $params);
260: echo CJSON::encode($result);
261: }
262: Yii::app()->end();
263: }
264:
265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276:
277: public function getItems2 (
278: $prefix='', $page=0, $limit=20, $valueAttr='name', $nameAttr='name') {
279:
280: $modelClass = get_class ($this->owner);
281: $model = CActiveRecord::model ($modelClass);
282: $table = $model->tableName ();
283: $offset = intval ($page) * intval ($limit);
284:
285: AuxLib::coerceToArray ($valueAttr);
286: $modelClass::checkThrowAttrError (array_merge ($valueAttr, array ($nameAttr)));
287: $params = array ();
288: if ($prefix !== '') {
289: $params[':prefix'] = $prefix . '%';
290: }
291: if ($limit !== -1) {
292: $offset = abs ((int) $offset);
293: $limit = abs ((int) $limit);
294: $limitClause = "LIMIT $offset, $limit";
295: }
296:
297: if ($model->asa ('permissions')) {
298: list ($accessCond, $permissionsParams) = $model->getAccessSQLCondition ();
299: $params = array_merge ($params, $permissionsParams);
300: }
301:
302: $command = Yii::app()->db->createCommand ("
303: SELECT " . implode (',', $valueAttr) . ", $nameAttr as __name
304: FROM $table as t
305: WHERE " . ($prefix === '' ?
306: '1=1' : ($nameAttr . ' LIKE :prefix')
307: ) . (isset ($accessCond) ? " AND $accessCond" : '') . "
308: ORDER BY __name
309: ". (isset ($limitClause) ? $limitClause : '') ."
310: ");
311: $rows = $command->queryAll (true, $params);
312:
313: $items = array ();
314: foreach ($rows as $row) {
315: $name = $row['__name'];
316: unset ($row['__name']);
317: $items[] = array ($name, $row);
318: }
319:
320: return $items;
321: }
322:
323: 324: 325: 326:
327: public function setModule($value) {
328: $this->_module = $value;
329: }
330:
331: }
332: