PageRenderTime 23ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/protected/extensions/gii-template-collection-develop/fullCrud/FullCrudCode.php

https://github.com/mithereal/yii-runends-template
PHP | 297 lines | 176 code | 29 blank | 92 comment | 23 complexity | 9e9aa9c1d5b0a90c56532efb9a660dab MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. Yii::import('system.gii.generators.crud.CrudCode');
  3. Yii::setPathOfAlias("gtc", dirname(__FILE__) . DIRECTORY_SEPARATOR . '..');
  4. Yii::import('gtc.components.*');
  5. Yii::import('gtc.fullCrud.FullCrudHelper');
  6. Yii::import('gtc.fullCrud.providers.*');
  7. class FullCrudCode extends CrudCode
  8. {
  9. // validation method; 0 = none, 1 = ajax, 2 = client-side, 3 = both
  10. public $validation = 3;
  11. public $baseControllerClass = 'Controller';
  12. public $messageCatalogStandard = "crud";
  13. public $messageCatalog = "model";
  14. public $template = "slim";
  15. // Slim template
  16. public $authTemplateSlim = "yii_extended_user_management_access_control";
  17. public $formEnctype = null;
  18. public $formLayout = 'two-columns';
  19. // Hybrid template
  20. public $authTemplateHybrid = "yii_user_management_access_control";
  21. public $formOrientation = "horizontal";
  22. public $textEditor = "textarea";
  23. public $internalModels = array();
  24. public $backendViewPathAlias = "application.themes.backend2.views";
  25. public $frontendViewPathAlias = "application.themes.frontend.views";
  26. // Legacy template
  27. public $authTemplate = "auth_filter_default";
  28. /*
  29. * for usage as provider
  30. */
  31. public $codeModel;
  32. /*
  33. * custom providers, topmost has highest priority, include GtcPartialViewProvider as first if needed
  34. */
  35. public $providers = array();
  36. //for editable and simple crud icon
  37. public $icon = "";
  38. private $_defaultProviders = array(
  39. "gtc.fullCrud.providers.EnumProvider",
  40. "gtc.fullCrud.providers.GtcIdentifierProvider",
  41. "gtc.fullCrud.providers.GtcPartialViewProvider", // highest customization level
  42. "gtc.fullCrud.providers.GtcOptionsProvider",
  43. "gtc.fullCrud.providers.TbEditableProvider",
  44. "gtc.fullCrud.providers.YiiBoosterActiveRowProvider",
  45. "gtc.fullCrud.providers.GtcRelationProvider",
  46. "gtc.fullCrud.providers.GtcActiveFieldProvider",
  47. "gtc.fullCrud.providers.GtcAttributeProvider",
  48. "gtc.fullCrud.providers.GtcColumnProvider",
  49. "gtc.fullCrud.FullCrudCode",
  50. );
  51. /**
  52. * Returns validation rules
  53. * @return array
  54. */
  55. public function rules()
  56. {
  57. return array_merge(
  58. parent::rules(),
  59. array(
  60. array('validation', 'required'),
  61. array('authTemplateSlim, authTemplateHybrid, providers, authTemplate, formEnctype, formLayout, formOrientation, textEditor, internalModels, backendViewPathAlias, frontendViewPathAlias, messageCatalog,messageCatalogStandard,icon', 'safe'),
  62. )
  63. );
  64. }
  65. /**
  66. * Returns form attribute labels
  67. * @return array
  68. */
  69. public function attributeLabels()
  70. {
  71. return array_merge(
  72. parent::attributeLabels(),
  73. array(
  74. 'validation' => 'Validation method',
  75. )
  76. );
  77. }
  78. /**
  79. * Tries to find a provider for the called method, continues until a provider returns not NULL
  80. *
  81. * @param string $name
  82. * @param array $args
  83. *
  84. * @return mixed
  85. */
  86. public function provider()
  87. {
  88. $provider = new GtcCodeProviderQueue();
  89. $provider->providers = CMap::mergeArray($this->providers,$this->_defaultProviders);
  90. $provider->codeModel = $this;
  91. Yii::log("Provider queue:".CJSON::encode($this->providers));
  92. return $provider;
  93. }
  94. /**
  95. * This method is overridden to be able to copy certain templates to backend and frontend themes' view directories
  96. * More specifically, views placed in _frontend will be copied to the destination as specified by $this->frontendViewPathAlias
  97. * and views placed in _backend will be copied to the destination as specified by $this->backendViewPathAlias
  98. */
  99. public function prepare()
  100. {
  101. parent::prepare();
  102. // Add backend theme views
  103. $templatePath = $this->templatePath. DIRECTORY_SEPARATOR . "_backend";
  104. if (is_dir($templatePath)) {
  105. $files=scandir($templatePath);
  106. foreach($files as $file)
  107. {
  108. if(is_file($templatePath.'/'.$file) && CFileHelper::getExtension($file)==='php')
  109. {
  110. $this->files[]=new CCodeFile(
  111. Yii::getPathOfAlias($this->backendViewPathAlias).DIRECTORY_SEPARATOR.$this->getControllerID().DIRECTORY_SEPARATOR.$file,
  112. $this->render($templatePath.'/'.$file)
  113. );
  114. }
  115. }
  116. }
  117. // Add frontend theme views
  118. $templatePath = $this->templatePath. DIRECTORY_SEPARATOR . "_frontend";
  119. if (is_dir($templatePath)) {
  120. $files=scandir($templatePath);
  121. foreach($files as $file)
  122. {
  123. if(is_file($templatePath.'/'.$file) && CFileHelper::getExtension($file)==='php')
  124. {
  125. $this->files[]=new CCodeFile(
  126. Yii::getPathOfAlias($this->frontendViewPathAlias).DIRECTORY_SEPARATOR.$this->getControllerID().DIRECTORY_SEPARATOR.$file,
  127. $this->render($templatePath.'/'.$file)
  128. );
  129. }
  130. }
  131. }
  132. }
  133. /**
  134. * prepend echo
  135. *
  136. * @param $modelClass
  137. * @param $column
  138. *
  139. * @return string
  140. */
  141. public function generateActiveLabel($modelClass, $column)
  142. {
  143. return "echo " . $this->generateActiveLabelGtcCodeStyle($modelClass, $column);
  144. }
  145. /**
  146. * prepend echo
  147. *
  148. * @param $modelClass
  149. * @param $column
  150. *
  151. * @return string
  152. */
  153. public function generateActiveField($modelClass, $column)
  154. {
  155. return "echo " . $this->generateActiveFieldGtcCodeStyle($modelClass, $column);
  156. }
  157. /**
  158. * Overridden to ensure that output follows GTC preferred code style
  159. */
  160. public function generateActiveLabelGtcCodeStyle($modelClass,$column)
  161. {
  162. return "\$form->labelEx(\$model, '{$column->name}')";
  163. }
  164. /**
  165. * Overridden to ensure that output follows GTC preferred code style
  166. */
  167. public function generateActiveFieldGtcCodeStyle($modelClass,$column)
  168. {
  169. if($column->type==='boolean')
  170. return "\$form->checkBox(\$model, '{$column->name}')";
  171. elseif(stripos($column->dbType,'text')!==false)
  172. return "\$form->textArea(\$model, '{$column->name}', array('rows' => 6, 'cols' => 50))";
  173. else
  174. {
  175. if(preg_match('/^(password|pass|passwd|passcode)$/i',$column->name))
  176. $inputField='passwordField';
  177. else
  178. $inputField='textField';
  179. if($column->type!=='string' || $column->size===null)
  180. return "\$form->{$inputField}(\$model, '{$column->name}')";
  181. else
  182. {
  183. if(($size=$maxLength=$column->size)>60)
  184. $size=60;
  185. return "\$form->{$inputField}(\$model, '{$column->name}', array('size' => $size, 'maxlength' => $maxLength))";
  186. }
  187. }
  188. }
  189. /**
  190. * Shorthand
  191. * @return string
  192. */
  193. public function getEnableAjaxValidation()
  194. {
  195. return ($this->validation == 1 || $this->validation == 3) ? 'true' : 'false';
  196. }
  197. /**
  198. * Shorthand
  199. * @return string
  200. */
  201. public function getEnableClientValidation()
  202. {
  203. return ($this->validation == 2 || $this->validation == 3) ? 'true' : 'false';
  204. }
  205. /**
  206. * Returns relations of current active record model
  207. * @return array
  208. */
  209. public function getRelations()
  210. {
  211. return CActiveRecord::model($this->modelClass)->relations();
  212. }
  213. /**
  214. * Returns the prefix for auth assignments, eg. `Module.Controller.Action`
  215. * @return mixed
  216. */
  217. public function getRightsPrefix(){
  218. if ($this->getModule() instanceof GiicApplication) {
  219. $module = '';
  220. } else {
  221. $module = $this->getModule()->id;
  222. }
  223. #var_dump($module, $this->getModule());exit;
  224. return str_replace(" ",".",ucwords(trim(str_replace("/"," ",$module.'/'.$this->getControllerID()))));
  225. }
  226. /**
  227. * @param null $model
  228. *
  229. * @return mixed
  230. * @TODO ?
  231. */
  232. public function getItemLabel($model = null)
  233. {
  234. if ($model === null) {
  235. $model = $this->model;
  236. }
  237. return $this->suggestIdentifier($model); // TODO ??? see provider
  238. }
  239. /**
  240. * @param CCodeFile $file whether the code file should be saved
  241. *
  242. * @todo Don't use a constant
  243. */
  244. public function confirmed($file)
  245. {
  246. if (defined('GIIC_ALL_CONFIRMED') && GIIC_ALL_CONFIRMED === true) {
  247. return true;
  248. } else {
  249. return parent::confirmed($file);
  250. }
  251. }
  252. /**
  253. * Returns a controller route for the specified relation.
  254. * Note: Controllers and models have to be named the same way, eg. model (Foo) -> controller (FooController)
  255. *
  256. * @param $relation
  257. *
  258. * @return string
  259. */
  260. public function resolveController($relation)
  261. {
  262. $relatedController = strtolower(substr($relation[1], 0, 1)) . substr($relation[1], 1);
  263. $controllerName = (strrchr($this->controller, "/")) ? strrchr($this->controller, "/") : $this->controller;
  264. $return = "/" . str_replace($controllerName, '/' . $relatedController, $this->controller);
  265. return $return;
  266. }
  267. }
  268. ?>