Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
WebCustModule.php
1 <?php
2 /**
3  * Created by JetBrains PhpStorm.
4  * User: Andrey
5  * Date: 4/19/13
6  * Time: 4:04 PM
7  * To change this template use File | Settings | File Templates.
8  */
9 class WebCustModule extends CWebModule{
10 
11  public $enableCustomize = true;
12 
13  private $_assetsUrl;
14  /**
15  * @var int the number of seconds in which the cached value will expire. 0 means never expire.
16  */
17  public $cacheTime = 60;
18  /**
19  * @var string the name of the main template for a mail
20  */
21  public $mainMailTemplate = null;
22 
23  public function init(){
24 
25  parent::init();
26 
27  $this->setLanguageFromSession();
28  $this->importFiles();
29  }
30 
31  private function importFiles(){
32 
33  $importList = array();
34  $moduleId = $this->getId();
35  $importList = array();
36  if($this->enableCustomize){
37  $importList = $this->getCustomizedModelsImportList();
38  }else{
39  $importList[] = "$moduleId.models.*";
40  }
41  $importList[] = "$moduleId.components.*";
42  $this->setImport($importList);
43  }
44 
45  private function getCustomizedModelsImportList(){
46 
47  $moduleId = $this->getId();
48 
49  $pathModule = $this->getBasePath();
50  $pathCommonModels = $pathModule.DIRECTORY_SEPARATOR."models".DIRECTORY_SEPARATOR;
51  $pathCustomModels = $pathModule.DIRECTORY_SEPARATOR."..".DIRECTORY_SEPARATOR."..".DIRECTORY_SEPARATOR.".."
52  .DIRECTORY_SEPARATOR."custom".DIRECTORY_SEPARATOR."modules".DIRECTORY_SEPARATOR
53  .$moduleId.DIRECTORY_SEPARATOR."models".DIRECTORY_SEPARATOR;
54  $pathCustomModelsBasic = $pathCustomModels."basic".DIRECTORY_SEPARATOR;
55 
56  $modelAlias = array();
57  $cacheId = 'custom_models_';
58  if(file_exists($pathCustomModels) && is_dir($pathCustomModels)){
59 
60  $commonModels = scandir($pathCommonModels);
61  $customModels = scandir($pathCustomModels);
62  //Build cache id ( CACHING )
63  foreach($customModels as $file){
64 
65  if(is_file($pathCustomModels.$file) && $touchTime = filemtime($pathCustomModels.$file)){
66  $cacheId .= $touchTime;
67  }
68  }
69  $cacheId = md5($cacheId);
70 
71  if(!$modelAlias = Yii::app()->cache->get($cacheId)){
72  function leftFilesOnly(&$dirContent){
73  foreach($dirContent as $i => &$file){
74  if(substr($file,-4) != '.php'){
75  unset($dirContent[$i]);
76  }
77  }
78  }
79 
80  leftFilesOnly($commonModels);
81  leftFilesOnly($customModels);
82 
83  foreach($commonModels as $model){
84  $location = 'common';
85  $modelName = explode('.',$model);
86  if(in_array($model, $customModels)){
87  $location = 'custom';
88  if(file_exists($pathCustomModelsBasic) && is_dir($pathCustomModelsBasic)){
89  if(substr(decoct(fileperms($pathCustomModelsBasic)),2) != 777){
90  @chmod($pathCustomModelsBasic, 0777);
91  }
92  }else{
93  mkdir($pathCustomModelsBasic);
94  @chmod($pathCustomModelsBasic, 0777);
95  }
96  $basicModelPath = $pathCustomModelsBasic.$modelName[0]."Basic.".$modelName[1];
97  if(!file_exists($basicModelPath)){
98  //Copying basic module class file to custom area
99  copy($pathCommonModels.$model, $basicModelPath);
100  //Rename the basic model class
101  $basic_model_code=file( $basicModelPath );
102  foreach($basic_model_code as $line=>$content){
103 
104  if(preg_match("/^class ([a-zA-Z0-9_]+) extends CActiveRecord/",$content) !== 0 && !strpos ($content,"Basic")){
105  $basic_model_code[$line] = preg_replace(
106  "/^class ([a-zA-Z0-9_]+) extends CActiveRecord/",
107  "class \${1}Basic extends CActiveRecord",
108  $content);
109  break;
110  }
111  }
112  file_put_contents( $basicModelPath , $basic_model_code );
113  }
114  //Adding the declaration of a basic class to import array
115  $modelAlias[] = "site.$location.modules.shoppingcart.models.basic.$modelName[0]Basic";
116  }
117  $modelAlias[] = "site.$location.modules.shoppingcart.models.$modelName[0]";
118  }
119  Yii::app()->cache->set($cacheId, $modelAlias);
120  }
121  }
122  if(!count($modelAlias)){
123  $modelAlias[] = "shoppingcart.models.*";
124  }
125  return $modelAlias;
126  }
127 
128  private function setLanguageFromSession(){
129 
130  if(Yii::app()->user->getState('lang') != null){
131  Yii::app()->language = Yii::app()->user->getState('lang');
132  }
133  }
134 
135  /**
136  * Translates a message to the specified language.
137  *
138  * @param string $str message
139  * @param array $params params
140  * @param string $dic dictionary
141  *
142  * @return string
143  */
144  public static function t($str = '', $params = array(), $dic = 'core')
145  {
146  return Yii::t(get_called_class() . "." . $dic, $str, $params);
147  }
148 
149  /**
150  * Returns path to assets for current module
151  *
152  * @return string
153  */
154  public function getAssetsUrl()
155  {
156  $moduleId = $this->getId();
157 
158  if ($this->_assetsUrl === null) {
159  $assetsPath = Yii::getPathOfAlias("$moduleId.assets");
160  $this->_assetsUrl = Yii::app()->getAssetManager()->publish($assetsPath, true, -1, YII_DEBUG);
161  }
162 
163  return $this->_assetsUrl;
164  }
165 }