1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17:
18: class Modules extends CActiveRecord {
19:
20: 21: 22:
23: private static $_displayNames;
24:
25: 26: 27: 28: 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: 63: 64: 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: 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: 139:
140: public function rules() {
141:
142:
143: return array(
144: array('visible, menuPosition, searchable, editable, adminOnly, custom, toggleable', 'numerical', 'integerOnly'=>true),
145: array('name, title', 'length', 'max'=>250),
146:
147:
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: 161:
162: public function relations() {
163:
164:
165: return array(
166: );
167: }
168:
169: 170: 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: 189: 190:
191: protected function afterDelete() {
192: parent::afterDelete();
193: if (!$this->custom)
194: return;
195:
196:
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: 210: 211:
212: public function search() {
213:
214:
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: 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: 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: 289: 290: 291: 292: 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:
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:
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:
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: 352: 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:
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:
385: $module->itemName = $itemName;
386: $module->save();
387: }
388: }
389: }
390: }
391:
392: return $itemName;
393: }
394:
395: 396: 397: 398: 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: 409: 410: 411:
412: public function retitle ($newTitle) {
413: $oldTitle = $this->title;
414: $this->title = $newTitle;
415: if($this->save()){
416:
417: if ($this->name === 'document') {
418: $doc = Docs::model ()->findByAttributes (array ('name' => $oldTitle));
419: $doc->name = $this->title;
420: $doc->save ();
421: }
422:
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: