Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
Module.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo 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 ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo 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 Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified 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 Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
40  abstract class Module extends CWebModule
41  {
42  private $isEnabled = true;
43 
51  public static function getModuleObjects()
52  {
53  $cacheKey = 'application.allModules';
54  try
55  {
56  // not using default value to save cpu cycles on requests that follow the first exception.
57  $modules = GeneralCache::getEntry($cacheKey);
58  }
59  catch (NotFoundException $e)
60  {
61  $moduleConfig = Yii::app()->getModules();
62  $modules = array();
63  foreach ($moduleConfig as $moduleName => $info)
64  {
65  $module = Yii::app()->findModule($moduleName);
66  if (isset($info['modules']) && is_array($info['modules']))
67  {
68  foreach ($info['modules'] as $nestedModuleName => $nestedInfo)
69  {
70  $modules[$nestedModuleName] = $module->getModule($nestedModuleName);
71  }
72  }
73  $modules[$moduleName] = $module;
74  }
75  GeneralCache::cacheEntry($cacheKey, $modules);
76  }
77  return $modules;
78  }
79 
84  public static function getDependenciesForModule(Module $module, $dependencies = array())
85  {
86  assert('$module !== null');
87  $dependencies = array_merge(array($module->getName()), $dependencies);
88  $dependencyNames = $module->getDependencies();
89  foreach ($dependencyNames as $dependencyName)
90  {
91  if (!in_array($dependencyName, $dependencies))
92  {
93  $dependentModule = Yii::app()->findModule($dependencyName);
94  assert('$dependentModule instanceof Module');
95  $dependencies = array_merge(self::getDependenciesForModule($dependentModule, $dependencies),
96  $dependencies);
97  }
98  }
99  return array_values(array_unique($dependencies));
100  }
101 
106  public function getName()
107  {
108  $calledClassName = get_called_class();
109  return $calledClassName::getDirectoryName();
110  }
111 
118  public static function getDirectoryName()
119  {
120  $name = get_called_class();
121  $name = substr($name, 0, strlen($name) - strlen('Module'));
122  $name = lcfirst($name);
123  return $name;
124  }
125 
131  public static function getSingularCamelCasedName()
132  {
133  $name = get_called_class();
134  $name = substr($name, 0, strlen($name) - strlen('Module') - 1);
135  return $name;
136  }
137 
142  public static function getPluralCamelCasedName()
143  {
144  $name = get_called_class();
145  $name = substr($name, 0, strlen($name) - strlen('Module'));
146  return $name;
147  }
148 
155  protected static function getSingularModuleLabel($language)
156  {
157  $name = static::getPluralModuleLabel($language);
158  $name = substr($name, 0, strlen($name) - 1);
159  return Zurmo::t('Core', $name, array(), null, $language);;
160  }
161 
168  protected static function getPluralModuleLabel($language)
169  {
170  $calledClassName = get_called_class();
171  $name = $calledClassName::getDirectoryName();
172  $name = preg_replace('/([A-Z])/', ' \1', $name);
173  return Zurmo::t('Core', ucfirst($name), array(), null, $language);
174  }
175 
176  public static function getModuleLabelByTypeAndLanguage($type, $language = null)
177  {
178  assert('in_array($type, array("Singular", "SingularLowerCase", "Plural", "PluralLowerCase"))');
179  assert('$language == null || is_string($language)');
180  if ($language == null)
181  {
182  $language = Yii::app()->language;
183  }
184  $label = self::getCustomModuleLabelByTypeAndLanguage($type, $language);
185  if ($label!= null)
186  {
187  return $label;
188  }
189  switch ($type)
190  {
191  case 'Singular':
192  return static::getSingularModuleLabel($language);
193  case 'SingularLowerCase':
194  $string = static::getSingularModuleLabel($language);
196  case 'Plural':
197  return static::getPluralModuleLabel($language);
198  case 'PluralLowerCase':
199  $string = static::getPluralModuleLabel($language);
201  }
202  }
203 
204  protected static function getCustomModuleLabelByTypeAndLanguage($type, $language)
205  {
206  assert('in_array($type, array("Singular", "SingularLowerCase", "Plural", "PluralLowerCase"))');
207  assert('$language != null');
208  $metadata = static::getMetadata();
209  switch ($type)
210  {
211  case 'Singular':
212  if (isset($metadata['global']['singularModuleLabels']) &&
213  isset($metadata['global']['singularModuleLabels'][$language]))
214  {
215  $label = $metadata['global']['singularModuleLabels'][$language];
216  return StringUtil::makeWordsUpperCase($label);
217  }
218  case 'SingularLowerCase':
219  if ( isset($metadata['global']['singularModuleLabels']) &&
220  isset($metadata['global']['singularModuleLabels'][$language]))
221  {
222  return $metadata['global']['singularModuleLabels'][$language];
223  }
224  case 'Plural':
225  if ( isset($metadata['global']['pluralModuleLabels']) &&
226  isset($metadata['global']['pluralModuleLabels'][$language]))
227  {
228  $label = $metadata['global']['pluralModuleLabels'][$language];
229  return StringUtil::makeWordsUpperCase($label);
230  }
231  case 'PluralLowerCase':
232  if ( isset($metadata['global']['pluralModuleLabels']) &&
233  isset($metadata['global']['pluralModuleLabels'][$language]))
234  {
235  return $metadata['global']['pluralModuleLabels'][$language];
236  }
237  }
238  }
239 
243  public function isEnabled()
244  {
245  return $this->isEnabled;
246  }
247 
252  public function canDisable()
253  {
254  return true;
255  }
256 
262  public function setIsEnabled($isEnabled)
263  {
264  assert('is_bool($isEnabled)');
265  if (!$isEnabled && !$this->canDisable())
266  {
267  throw new NotSupportedException();
268  }
269  if ($isEnabled)
270  {
271  $modules = Module::GetModuleObjects();
272  $dependencies = $this->getDependencies();
273  foreach ($dependencies as $dependency)
274  {
275  assert('array_key_exists($dependency, $modules)');
276  $modules[$dependency]->setIsEnabled($isEnabled);
277  }
278  }
279  $this->isEnabled = $isEnabled;
280  assert('!$this->isEnabled || $this->isEnabled && $this->dependenciesAreEnabled()');
281  }
282 
289  public abstract function getDependencies();
290 
294  public function getEnabledDependencies($temp = null)
295  {
296  if ($temp === null) // TODO - remove this $temp junk when the modules metadata is being saved.
297  {
298  $temp = self::getModuleObjects();
299  }
300  return $this->getEnabledDependenciesInternal($temp);
301  }
302 
303  // The public version gets the modules once, then
304  // the private version can use it recursively.
305  private function getEnabledDependenciesInternal($modules)
306  {
307  $unsatisfiedDependencies = array();
308  $dependencies = $this->getDependencies();
309  foreach ($dependencies as $dependency)
310  {
311  assert('array_key_exists($dependency, $modules)');
312  if ($modules[$dependency]->isEnabled())
313  {
314  $unsatisfiedDependencies[] = $dependency;
315  }
316  array_merge($unsatisfiedDependencies, $modules[$dependency]->getEnabledDependencies($modules));
317  }
318  return $unsatisfiedDependencies;
319  }
320 
321  public function getRootModelNamesIncludingDependencies()
322  {
323  $rootModels = $this->getRootModelNames();
324  foreach ($this->getEnabledDependencies() as $dependencyName)
325  {
326  $module = Yii::app()->getModule($dependencyName);
327  $dependencyModulesRootModels = $module->getRootModelNamesIncludingDependencies();
328  $rootModels = array_merge($rootModels, array_diff($dependencyModulesRootModels, $rootModels));
329  }
330  return $rootModels;
331  }
332 
340  public function getRootModelNames()
341  {
342  return array();
343  }
344 
345  /*
346  * Returns the stronger of the two policy values, being the more
347  * restrictive given the nature of that specific policy.
348  */
349  public static function getStrongerPolicy($policyName, array $values)
350  {
351  throw new NotSupportedException();
352  }
353 
357  public static function getPolicyRulesTypes()
358  {
359  return array();
360  }
361 
365  public function getConfigurationView()
366  {
367  return new ConfigurationView();
368  }
369 
373  public static function getTabMenuItems($user = null)
374  {
375  assert('$user == null || $user instanceof User');
376  $metadata = self::getMetadata();
377  if (!empty($metadata['global']['tabMenuItems']))
378  {
379  return $metadata['global']['tabMenuItems'];
380  }
381  return array();
382  }
383 
387  public static function getAdminTabMenuItems($user = null)
388  {
389  assert('$user == null || $user instanceof User');
390  $metadata = self::getMetadata();
391  if (!empty($metadata['global']['adminTabMenuItems']))
392  {
393  return $metadata['global']['adminTabMenuItems'];
394  }
395  return array();
396  }
397 
401  public static function getConfigureMenuItems()
402  {
403  $metadata = self::getMetadata();
404  if (!empty($metadata['global']['configureMenuItems']))
405  {
406  return $metadata['global']['configureMenuItems'];
407  }
408  return array();
409  }
410 
411  public static function getConfigureSubMenuItems()
412  {
413  $metadata = self::getMetadata();
414  if (!empty($metadata['global']['configureSubMenuItems']))
415  {
416  return $metadata['global']['configureSubMenuItems'];
417  }
418  return array();
419  }
420 
421  public static function getShortCutsCreateMenuItems()
422  {
423  $calledClass = get_called_class();
424  $metadata = $calledClass::getMetadata();
425  if (!empty($metadata['global']['shortcutsCreateMenuItems']))
426  {
427  return $metadata['global']['shortcutsCreateMenuItems'];
428  }
429  return array();
430  }
431 
432  public function getDesignerMenuItems()
433  {
434  $metadata = $this->getMetadata();
435  if (!empty($metadata['global']['designerMenuItems']))
436  {
437  return $metadata['global']['designerMenuItems'];
438  }
439  return array();
440  }
441 
448  public static function getPrimaryModelName()
449  {
450  throw new NotSupportedException();
451  }
452 
457  public static function getGlobalSearchFormClassName()
458  {
459  return null;
460  }
461 
466  public static function modelsAreNeverGloballySearched()
467  {
468  return false;
469  }
470 
475  public static function hasAtLeastOneGlobalSearchAttributeName()
476  {
477  $metadata = static::getMetadata();
478  if ( isset($metadata['global']['globalSearchAttributeNames']) &&
479  is_array($metadata['global']['globalSearchAttributeNames']) &&
480  count($metadata['global']['globalSearchAttributeNames']) > 0)
481  {
482  return true;
483  }
484  return false;
485  }
486 
491  public static function getStateMetadataAdapterClassName()
492  {
493  return null;
494  }
495 
502  public static function getMetadata(User $user = null)
503  {
504  $className = get_called_class();
505  if ($user == null)
506  {
507  try
508  {
509  // not using default value to save cpu cycles on requests that follow the first exception.
510  return GeneralCache::getEntry($className . 'Metadata');
511  }
512  catch (NotFoundException $e)
513  {
514  }
515  }
516  $metadata = MetadataUtil::getMetadata($className, $user);
517  if (YII_DEBUG)
518  {
519  $className::assertMetadataIsValid($metadata);
520  }
521  if ($user == null)
522  {
523  GeneralCache::cacheEntry($className . 'Metadata', $metadata);
524  }
525  return $metadata;
526  }
527 
533  public static function setMetadata(array $metadata, User $user = null)
534  {
535  $className = get_called_class();
536  if (YII_DEBUG)
537  {
538  self::assertMetadataIsValid($metadata);
539  }
540  MetadataUtil::setMetadata($className, $metadata, $user);
541  if ($user == null)
542  {
543  GeneralCache::forgetEntry($className . 'Metadata');
544  }
545  }
546 
552  public static function getDefaultMetadata()
553  {
554  return array();
555  }
556 
557  public static function getViewClassNames()
558  {
559  return static::getAllClassNamesByPathFolder('views');
560  }
561 
565  public static function getModelClassNames()
566  {
567  return static::getAllClassNamesByPathFolder('models');
568  }
569 
570  public static function getAllClassNamesByPathFolder($folder)
571  {
572  assert('is_string($folder)');
573  $classNames = array();
574  $className = get_called_class();
575  $alias = 'application.modules.' . $className::getDirectoryName() . '.' . $folder;
576  $classNames = PathUtil::getAllClassNamesByPathAlias($alias);
577  return $classNames;
578  }
579 
580  private static function assertMetadataIsValid(array $metadata)
581  {
582  //add checks as needed.
583  }
584 
588  public static function getDefaultDataMakerClassName()
589  {
590  }
591 
595  public static function getDemoDataMakerClassNames()
596  {
597  }
598 
602  public static function isReportable()
603  {
604  return false;
605  }
606 
610  public static function canHaveWorkflow()
611  {
612  return false;
613  }
614 
618  public static function canHaveContentTemplates()
619  {
620  return false;
621  }
622 
626  public static function canShowOnCalendar()
627  {
628  return false;
629  }
630  }
631 ?>
static getMetadata(User $user=null)
Definition: Module.php:502
getConfigurationView()
Definition: Module.php:365
getRootModelNames()
Definition: Module.php:340
static getTabMenuItems($user=null)
Definition: Module.php:373
isEnabled()
Definition: Module.php:243
static canShowOnCalendar()
Definition: Module.php:626
Definition: User.php:37
static getSingularCamelCasedName()
Definition: Module.php:131
static getStateMetadataAdapterClassName()
Definition: Module.php:491
static getAdminTabMenuItems($user=null)
Definition: Module.php:387
static canHaveContentTemplates()
Definition: Module.php:618
static getDefaultMetadata()
Definition: Module.php:552
static getModuleObjects()
Definition: Module.php:51
static makeWordsUpperCase($string)
Definition: StringUtil.php:261
static canHaveWorkflow()
Definition: Module.php:610
static getGlobalSearchFormClassName()
Definition: Module.php:457
canDisable()
Definition: Module.php:252
static getDemoDataMakerClassNames()
Definition: Module.php:595
static getPrimaryModelName()
Definition: Module.php:448
static setMetadata($className, array $metadata, User $user=null)
static getDirectoryName()
Definition: Module.php:118
static getModelClassNames()
Definition: Module.php:565
static getPolicyRulesTypes()
Definition: Module.php:357
getEnabledDependencies($temp=null)
Definition: Module.php:294
static getDefaultDataMakerClassName()
Definition: Module.php:588
static getSingularModuleLabel($language)
Definition: Module.php:155
getDependencies()
setIsEnabled($isEnabled)
Definition: Module.php:262
static strToLowerWithDefaultEncoding($string)
Definition: TextUtil.php:47
static getConfigureMenuItems()
Definition: Module.php:401
static setMetadata(array $metadata, User $user=null)
Definition: Module.php:533
static isReportable()
Definition: Module.php:602
static getMetadata($className, User $user=null)
static hasAtLeastOneGlobalSearchAttributeName()
Definition: Module.php:475
static getPluralModuleLabel($language)
Definition: Module.php:168
static getPluralCamelCasedName()
Definition: Module.php:142
getName()
Definition: Module.php:106
static modelsAreNeverGloballySearched()
Definition: Module.php:466
static getDependenciesForModule(Module $module, $dependencies=array())
Definition: Module.php:84
Generated on Mon Jul 13 2020 07:10:29
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.