Overview

Packages

  • application
    • commands
    • components
      • actions
      • filters
      • leftWidget
      • permissions
      • sortableWidget
      • util
      • webupdater
      • x2flow
        • actions
        • triggers
      • X2GridView
      • X2Settings
    • controllers
    • models
      • embedded
    • modules
      • accounts
        • controllers
        • models
      • actions
        • controllers
        • models
      • calendar
        • controllers
        • models
      • charts
        • models
      • contacts
        • controllers
        • models
      • docs
        • components
        • controllers
        • models
      • groups
        • controllers
        • models
      • marketing
        • components
        • controllers
        • models
      • media
        • controllers
        • models
      • mobile
        • components
      • opportunities
        • controllers
        • models
      • products
        • controllers
        • models
      • quotes
        • controllers
        • models
      • services
        • controllers
        • models
      • template
        • models
      • users
        • controllers
        • models
      • workflow
        • controllers
        • models
      • x2Leads
        • controllers
        • models
  • None
  • system
    • base
    • caching
    • console
    • db
      • ar
      • schema
    • validators
    • web
      • actions
      • auth
      • helpers
      • widgets
        • captcha
        • pagers
  • zii
    • widgets
      • grid

Classes

  • ActionMetaData
  • ActionText
  • Admin
  • AmorphousModel
  • ApiHook
  • APIModel
  • ChartSetting
  • ContactForm
  • ContactList
  • Credentials
  • Criteria
  • Dropdowns
  • Events
  • EventsData
  • Fields
  • FormLayout
  • Imports
  • InlineEmail
  • LeadRouting
  • Locations
  • LoginForm
  • Maps
  • Modules
  • Notes
  • Notification
  • PhoneNumber
  • Profile
  • Record
  • Relationships
  • Roles
  • RoleToPermission
  • RoleToUser
  • RoleToWorkflow
  • Rules
  • Session
  • SessionLog
  • Social
  • Tags
  • TempFile
  • Tips
  • Tours
  • TrackEmail
  • TriggerLog
  • URL
  • ViewLog
  • Widgets
  • X2List
  • X2ListCriterion
  • X2ListItem
  • X2Model
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * This is the model class for table "x2_modules".
  5:  *
  6:  * @package application.models
  7:  * @property integer $id
  8:  * @property string $name
  9:  * @property string $title
 10:  * @property integer $visible
 11:  * @property integer $menuPosition
 12:  * @property integer $searchable
 13:  * @property integer $editable
 14:  * @property integer $adminOnly
 15:  * @property integer $custom
 16:  * @property integer $toggleable
 17:  */
 18: class Modules extends CActiveRecord {
 19: 
 20:     /**
 21:      * Cached Module titles
 22:      */
 23:     private static $_displayNames;
 24: 
 25:     /**
 26:      * Update visibility and order of top bar module links 
 27:      * @param array $idsOfVisibleModules 
 28:      * @param array $idsOfHiddenModules
 29:      */
 30:     public static function updateTopBarLinks (
 31:         array $idsOfVisibleModules, array $idsOfHiddenModules) {
 32: 
 33:         $transaction = Yii::app()->db->beginTransaction ();
 34:         try {
 35:             $count = count ($idsOfVisibleModules);
 36:             for ($i = 0; $i < $count; $i++) {
 37:                 $id = $idsOfVisibleModules[$i];
 38:                 Yii::app()->db->createCommand ("
 39:                     update x2_modules
 40:                     set visible=1, menuPosition=$i
 41:                     where id=:id
 42:                 ")->execute (array (':id' => $id));
 43:             }
 44:             $count = count ($idsOfHiddenModules);
 45:             for ($i = 0; $i < $count; $i++) {
 46:                 $id = $idsOfHiddenModules[$i];
 47:                 Yii::app()->db->createCommand ("
 48:                     update x2_modules
 49:                     set visible=0, menuPosition=-1
 50:                     where id=:id
 51:                 ")->execute (array (':id' => $id));
 52:             }
 53:             $transaction->commit ();
 54:         } catch (Exception $e) {
 55:             $transaction->rollback ();
 56:             return false;
 57:         }
 58:         return true;
 59:     }
 60: 
 61:     /**
 62:      * Returns the static model of the specified AR class.
 63:      * @param string $className active record class name.
 64:      * @return Modules the static model class
 65:      */
 66:     public static function model($className=__CLASS__) {
 67:         return parent::model($className);
 68:     }
 69: 
 70:     public static function getModuleNames ($custom=null) {
 71:         static $cache;
 72:         if (!isset ($cache)) {
 73:             $cache = Yii::app()->db->createCommand ()
 74:                 ->select ('name, custom')
 75:                 ->from ('x2_modules')
 76:                 ->where('moduleType = "module"')
 77:                 ->queryAll ();
 78:         }
 79:         if ($custom) {
 80:             $records = array_filter ($cache, function ($record) {
 81:                 return (int) $record['custom'] === 1;
 82:             });
 83:         } else {
 84:             $records = $cache;
 85:         }
 86:         return array_map (function ($record) {
 87:             return $record['name'];
 88:         }, $records);
 89:     }
 90: 
 91:     public static function getDropdownOptions ($keyType='name', $filter=null) {
 92:         $filter = $filter === null ? function () { return true; } : $filter;
 93:         if ($keyType === 'name') {
 94:             $moduleNames = array_filter (self::getModuleNames (), $filter);
 95:             $options = array ();
 96:             foreach ($moduleNames as $name) {
 97:                 $options[$name] = self::displayName (true, $name);
 98:             }
 99:         } elseif ($keyType === 'id') {
100:             $modules = array_filter (Yii::app()->db->createCommand ()
101:                 ->select ('id, name')
102:                 ->from ('x2_modules')
103:                 ->where('moduleType = "module"')
104:                 ->queryAll (), $filter);
105:             $options = array ();
106:             foreach ($modules as $record) {
107:                 $options[$record['id']] = self::displayName (true, $record['name']);
108:             }
109:         } else {
110:             throw new CException ('invalid key type');
111:         }
112:         asort ($options);
113:         return $options;
114:     }
115: 
116: 
117:     public static function dropDownList ($name, $selected='', $htmlOptions=array ()) {
118:         return CHtml::dropDownList (
119:             $name, $selected, self::getDropdownOptions (), $htmlOptions);
120:     }
121: 
122:     /**
123:      * @return string the associated database table name
124:      */
125:     public function tableName() {
126:         return 'x2_modules';
127:     }
128: 
129:     public function scopes () {
130:         return array (
131:             'titleSorted' => array (
132:                 'order' => 'title ASC'
133:             ),
134:         );
135:     }
136: 
137:     /**
138:      * @return array validation rules for model attributes.
139:      */
140:     public function rules() {
141:         // NOTE: you should only define rules for those attributes that
142:         // will receive user inputs.
143:         return array(
144:             array('visible, menuPosition, searchable, editable, adminOnly, custom, toggleable', 'numerical', 'integerOnly'=>true),
145:             array('name, title', 'length', 'max'=>250),
146:             // The following rule is used by search().
147:             // Please remove those attributes that should not be searched.
148:             array('id, name, title, visible, menuPosition, searchable, editable, adminOnly, custom, toggleable', 'safe', 'on'=>'search'),
149:             array(
150:                 'moduleType', 'in', 
151:                 'range' => array ('module', 'link', 'recordLink', 'pseudoModule')
152:             ),
153:             array(
154:                 'linkHref,linkRecordType,linkRecordId', 'safe', 
155:             ),
156:         );
157:     }
158: 
159:     /**
160:      * @return array relational rules.
161:      */
162:     public function relations() {
163:         // NOTE: you may need to adjust the relation name and the related
164:         // class name for the relations automatically generated below.
165:         return array(
166:         );
167:     }
168: 
169:     /**
170:      * @return array customized attribute labels (name=>label)
171:      */
172:     public function attributeLabels() {
173:         return array(
174:             'id' => 'ID',
175:             'name' => 'Name',
176:             'title' => 'Title',
177:             'visible' => 'Visible',
178:             'menuPosition' => 'Menu Position',
179:             'searchable' => 'Searchable',
180:             'editable' => 'Editable',
181:             'adminOnly' => 'Admin Only',
182:             'custom' => 'Custom',
183:             'toggleable' => 'Toggleable',
184:         );
185:     }
186: 
187:     /**
188:      * Clean up after custom modules when they are deleted. Note, this shouldn't be applicable to default modules,
189:      * they cannot be deleted, only disabled
190:      */
191:     protected function afterDelete() {
192:         parent::afterDelete();
193:         if (!$this->custom)
194:             return;
195: 
196:         // remove associated Events, Actions, changelog entries, linked records, and Relationships
197:         $events = X2Model::model('Events')->findAllByAttributes(array(
198:             'associationType' => $this->name,
199:         ));
200:         $actions = X2Model::model('Actions')->findAllByAttributes(array(
201:             'associationType' => $this->name,
202:         ));
203:         $models = array_merge ($events, $actions);
204:         foreach ($models as $model)
205:             $model->delete();
206:     }
207: 
208:     /**
209:      * Retrieves a list of models based on the current search/filter conditions.
210:      * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
211:      */
212:     public function search() {
213:         // Warning: Please modify the following code to remove attributes that
214:         // should not be searched.
215: 
216:         $criteria=new CDbCriteria;
217: 
218:         $criteria->compare('id',$this->id);
219:         $criteria->compare('name',$this->name,true);
220:         $criteria->compare('title',$this->title,true);
221:         $criteria->compare('visible',$this->visible);
222:         $criteria->compare('menuPosition',$this->menuPosition);
223:         $criteria->compare('searchable',$this->searchable);
224:         $criteria->compare('editable',$this->editable);
225:         $criteria->compare('adminOnly',$this->adminOnly);
226:         $criteria->compare('custom',$this->custom);
227:         $criteria->compare('toggleable',$this->toggleable);
228: 
229:         return new CActiveDataProvider($this, array(
230:             'criteria'=>$criteria,
231:         ));
232:     }
233:     
234:     /**
235:      * Populate a list of available modules to import/export
236:      */
237:     public static function getExportableModules() {
238:         $modules = Modules::model()->findAll();
239:         $moduleList = array();
240:         $skipModules = array(
241:             'Calendar', 'Charts', 'Groups', 'Reports', 'Media', 'Users', 'Workflow');
242:         
243:         foreach($modules as $module){
244:             $name = ucfirst($module->name);
245:             if (in_array($name, $skipModules)) {
246:                 continue;
247:             }
248:             if($name != 'Document'){
249:                 $controllerName = $name.'Controller';
250:                 if(file_exists(
251:                     'protected/modules/'.$module->name.'/controllers/'.$controllerName.'.php')){
252: 
253:                     Yii::import("application.modules.$module->name.controllers.$controllerName");
254:                     $controller = new $controllerName($controllerName);
255:                     $model = $controller->modelClass;
256:                     if(class_exists($model)){
257:                         $moduleList[$model] = Yii::t('app', $module->title);
258:                     }
259:                 }
260:             }
261:         }
262:         return $moduleList;
263:     }
264: 
265:     /**
266:      * @return array names of models associated with each module
267:      */
268:     public static function getNamesOfModelsOfModules () {
269:         $moduleNames = array_map (function ($record) {
270:             return $record->name; 
271:         }, Modules::model ()->findAll (array ('select' => 'name')));
272: 
273:         $models = array ();
274:         foreach ($moduleNames as $name) {
275:             $modelName = X2Model::getModelName ($name);
276:             if ($modelName && is_subclass_of ($modelName, 'X2Model')) {
277:                 $models[] = $modelName;
278:             }
279:         }
280:         return $models;
281:     }
282: 
283:     public function getDisplayName ($plural = true) {
284:         return self::displayName ($plural, $this->name);
285:     }
286: 
287:     /**
288:      * Retrieves the title of a given module or the current module
289:      * if no module is specified
290:      * @param bool Retrieve the plural form of the module name
291:      * @param string Module to retrieve name for
292:      * @return string|false Current module title, false if none could be found
293:      */
294:     public static function displayName($plural = true, $module = null, $refresh = false) {
295:         $moduleTitle = null;
296:         if (is_null($module) && isset(Yii::app()->controller->module))
297:             $module = Yii::app()->controller->module->name;
298: 
299:         // return a cached value
300:         if (!$refresh && !is_null($module) && isset(self::$_displayNames[$module][$plural]))
301:             return self::$_displayNames[$module][$plural];
302: 
303:         $moduleTitle = Yii::app()->db->createCommand()
304:             ->select('title')
305:             ->from('x2_modules')
306:             ->where("name = :name")
307:             ->bindValue(':name', $module)
308:             ->limit(1)
309:             ->queryScalar();
310: 
311:         if (!$moduleTitle) return false;
312: 
313:         if (Yii::app()->locale->id === 'en') {
314:             // Handle silly English pluralization
315:             if ($plural === false) {
316:                 if (preg_match('/ies$/', $moduleTitle)) {
317:                     $moduleTitle = preg_replace('/ies$/', 'y', $moduleTitle);
318:                 } else if (preg_match('/ses$/', $moduleTitle)) {
319:                     $moduleTitle = preg_replace('/es$/', '', $moduleTitle);
320:                 } else if ($moduleTitle !== 'Process') {
321:                     // Otherwise chop the trailing s
322:                     $moduleTitle = trim($moduleTitle, 's');
323:                 }
324:             } elseif ($plural === 'optional') {
325:                 if (preg_match('/y$/', $moduleTitle)) {
326:                     $moduleTitle = preg_replace('/y$/', '(ies)', $moduleTitle);
327:                 } else if (preg_match('/ss$/', $moduleTitle)) {
328:                     $moduleTitle .= '(es)';
329:                 } else if (in_array ($moduleTitle, array ('Service'))) {
330:                     $moduleTitle .= '(s)';
331:                 } elseif (preg_match ('/s$/', $moduleTitle)) {
332:                     $moduleTitle = preg_replace('/s$/', '(s)', $moduleTitle);
333:                 }
334:             } else {
335:                 if (preg_match('/y$/', $moduleTitle)) {
336:                     $moduleTitle = preg_replace('/y$/', 'ies', $moduleTitle);
337:                 } else if (preg_match('/ss$/', $moduleTitle)) {
338:                     $moduleTitle .= 'es';
339:                 } else if (in_array ($moduleTitle, array ('Service'))) {
340:                     $moduleTitle .= 's';
341:                 }
342:             }
343:         }else{
344:             $moduleTitle = Yii::t('app', $moduleTitle);
345:         }
346:         self::$_displayNames[$module][$plural] = $moduleTitle;
347:         return $moduleTitle;
348:     }
349: 
350:     /**
351:      * Retrieves the item name for the specified Module
352:      * @param string $module Module to retrieve item name for, or the current module if null
353:      */
354:     public static function itemDisplayName($moduleName = null) {
355:         if (is_null($moduleName) && isset(Yii::app()->controller->module))
356:             $moduleName = Yii::app()->controller->module->name;
357:         $module = X2Model::model('Modules')->findByAttributes(array('name' => $moduleName));
358:         if(!$module){
359:             return $moduleName;
360:         }
361:         $itemName = $moduleName;
362:         if (!empty($module->itemName)) {
363:             $itemName = $module->itemName;
364:         } else {
365:             // Attempt to load item name from legacy module options file
366:             $moduleDir = implode(DIRECTORY_SEPARATOR, array(
367:                 'protected',
368:                 'modules',
369:                 $moduleName
370:             ));
371:             $configFile = implode(DIRECTORY_SEPARATOR, array(
372:                 $moduleDir,
373:                 lcfirst($moduleName)."Config.php"
374:             ));
375: 
376:             if (is_dir($moduleDir) && file_exists($configFile)) {
377:                 $file = Yii::app()->file->set($configFile);
378:                 $contents = $file->getContents();
379:                 if (preg_match("/.*'recordName'.*/", $contents, $matches)) {
380:                     $itemNameLine = $matches[0];
381:                     $itemNameRegex = "/.*'recordName'=>'([\w\s]*?)'.*/";
382:                     $itemName = preg_replace($itemNameRegex, "$1", $itemNameLine);
383:                     if (!empty($itemName)) {
384:                         // Save this name in the database for the future
385:                         $module->itemName = $itemName;
386:                         $module->save();
387:                     }
388:                 }
389:             }
390:         }
391: 
392:         return $itemName;
393:     }
394: 
395:     /**
396:      * Returns array of custom modules
397:      * @param bool $visible
398:      * @return <array of Modules> 
399:      */
400:     public function getCustomModules ($visible=false) { 
401:         $attributes = array ('custom' => 1);
402:         if ($visible) $attributes['visible'] = 1;
403:         return $this->findAllByAttributes ($attributes);
404:     }
405: 
406: 
407:     /**
408:      * Renames module
409:      * @param string $newTitle 
410:      * @return bool true for success, false for failure
411:      */
412:     public function retitle ($newTitle) {
413:         $oldTitle = $this->title;
414:         $this->title = $newTitle;
415:         if($this->save()){
416:             // if it's a static page, rename the doc too
417:             if ($this->name === 'document') { 
418:                 $doc = Docs::model ()->findByAttributes (array ('name' => $oldTitle));
419:                 $doc->name = $this->title;
420:                 $doc->save ();
421:             }
422:             // Clear cached display names
423:             self::$_displayNames = array();
424:             return true;
425:         } else {
426:             return false;
427:         }
428:     }
429: 
430:     public function getLinkedRecord () {
431:         if ($this->moduleType !== 'recordLink') {
432:             throw new CException ('invalid module type');
433:         }
434:         $model = X2Model::model2 ($this->linkRecordType);
435:         if ($model && ($record = $model->findByPk ($this->linkRecordId))) {
436:             return $record;
437:         }
438:     }
439: 
440:     public function getTitle () {
441:         switch ($this->moduleType) {
442:             case 'module':
443:             case 'pseudoModule':
444:             case 'link':
445:                 return $this->title;
446:             case 'recordLink':
447:                 $linkedRecord = $this->getLinkedRecord ();
448:                 if ($linkedRecord && isset ($linkedRecord->name)) {
449:                     return $linkedRecord->name;
450:                 }
451:                 break;
452:             default:
453:                 break;
454:         }
455:     }
456: 
457: 
458: }
459: 
X2CRM Documentation API documentation generated by ApiGen 2.8.0