PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/application/protected/extensions/gtc/fullCrud/FullCrudCode.php

https://bitbucket.org/dinhtrung/yiicorecms/
PHP | 266 lines | 218 code | 28 blank | 20 comment | 68 complexity | 7df98f74b3bff46c4c9f600a09746e8a MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, CC0-1.0, BSD-2-Clause, GPL-2.0, LGPL-2.1, LGPL-3.0
  1. <?php
  2. Yii::import('system.gii.generators.crud.CrudCode');
  3. Yii::import('ext.gtc.components.*');
  4. class FullCrudCode extends CrudCode {
  5. public $authtype;
  6. public $persistent_sessions = true;
  7. public $enable_ajax_validation = true;
  8. public $baseControllerClass='GController';
  9. public function prepare() {
  10. parent::prepare();
  11. if(!isset($this->baseControllerClass))
  12. $this->baseControllerClass;
  13. }
  14. public function rules()
  15. {
  16. return array_merge(parent::rules(), array(
  17. array('authtype, persistent_sessions, enable_ajax_validation', 'required'),
  18. ));
  19. }
  20. public function attributeLabels()
  21. {
  22. return array_merge(parent::attributeLabels(), array(
  23. 'authtype'=>'Authentication type',
  24. 'persistent_sessions'=>'Persistent Sessions',
  25. 'enable_ajax_validateion'=>'Enable ajax Validation',
  26. ));
  27. }
  28. public function init() {
  29. // just check if the classes can be found
  30. if (!@class_exists("GController")) {
  31. throw new CException("Fatal Error: Class 'GController' could not be found in your application! Add 'ext.gtc.components.*' to your import paths.");
  32. }
  33. if (!@class_exists("Relation")) {
  34. throw new CException("Fatal Error: Class 'Relation' could not be found in your application! Add 'ext.gtc.components.*' to your import paths.");
  35. }
  36. parent::init();
  37. }
  38. // suggest which database column is best suited for being display in
  39. // a foreign Relation
  40. public function suggestName($columns) {
  41. $j = 0;
  42. foreach ($columns as $column) {
  43. if (!$column->isForeignKey
  44. && !$column->isPrimaryKey
  45. && $column->type != 'INT'
  46. && $column->type != 'INTEGER'
  47. && $column->type != 'BOOLEAN') {
  48. $num = $j;
  49. break;
  50. }
  51. $j++;
  52. }
  53. for ($i = 0; $i < $j; $i++)
  54. next($columns);
  55. if (is_object(current($columns)))
  56. return current($columns);
  57. else {
  58. $column = reset($columns);
  59. return $column;
  60. }
  61. }
  62. public function getRelations() {
  63. return CActiveRecord::model($this->modelClass)->relations();
  64. }
  65. public function generateRelation($model, $relationname, $relation) {
  66. // Use the second attribute of the model, since the first is the id in
  67. // most cases
  68. if ($columns = CActiveRecord::model($relation[1])->tableSchema->columns) {
  69. $j = 0;
  70. foreach ($columns as $column) {
  71. if (!$column->isForeignKey && !$column->isPrimaryKey) {
  72. $num = $j;
  73. break;
  74. }
  75. $j++;
  76. }
  77. for ($i = 0; $i < $j; $i++)
  78. next($columns);
  79. $field = current($columns);
  80. $style = $relation[0] == 'CManyManyRelation' ? 'checkbox' : 'dropdownlist';
  81. if (is_object($field)) {
  82. if ($relation[0] == 'CManyManyRelation')
  83. $allowEmpty = 'false';
  84. elseif ($relation[0] == 'CHasOneRelation') {
  85. $allowEmpty = (CActiveRecord::model($relation[1])->tableSchema->columns[$relation[2]]->allowNull ? 'true' : 'false');
  86. return "if (\$model->{$relationname} !== null) echo \$model->{$relationname}->title;";
  87. }
  88. else
  89. $allowEmpty= (CActiveRecord::model($model)->tableSchema->columns[$relation[2]]->allowNull?'true':'false');
  90. return("\$this->widget(
  91. 'Relation',
  92. array(
  93. 'model' => \$model,
  94. 'relation' => '{$relationname}',
  95. 'fields' => '{$field->name}',
  96. 'allowEmpty' => {$allowEmpty},
  97. 'style' => '{$style}',
  98. 'htmlOptions' => array(
  99. 'checkAll' => Yii::t('app', 'Choose all'),
  100. ),)
  101. )");
  102. }
  103. }
  104. }
  105. /**
  106. * @param CActiveRecord $modelClass
  107. * @param CDbColumnSchema $column
  108. */
  109. public function generateActiveField($model, $column) {
  110. if (!is_object($model))
  111. $model = CActiveRecord::model($model);
  112. $providerPaths = Yii::app()->controller->module->params['gtc.fullCrud.providers'];
  113. $providerPaths[] = 'ext.gtc.fullCrud.providers.FullCrudFieldProvider';
  114. $field = null;
  115. foreach($providerPaths AS $provider) {
  116. $providerClass = Yii::createComponent($provider);
  117. if (($field = $providerClass::generateActiveField($model, $column)) !== null)
  118. break;
  119. }
  120. if ($field !== null) {
  121. return $field;
  122. } else {
  123. return('echo ' . parent::generateActiveField($model, $column));
  124. }
  125. }
  126. /**
  127. * @param CActiveRecord $modelClass
  128. * @param CDbColumnSchema $column
  129. */
  130. public function generateValueField($modelClass, $column, $view = false) {
  131. if ($column->isForeignKey) {
  132. $model = CActiveRecord::model($modelClass);
  133. $table = $model->getTableSchema();
  134. $fk = $table->foreignKeys[$column->name];
  135. // We have to look into relations to find the correct model class (i.e. if models are generated with table prefix)
  136. // TODO: do not repeat yourself (foreach) - this is a hotfix
  137. foreach ($model->relations() as $key => $value) {
  138. if (strcasecmp($value[2], $column->name) == 0)
  139. $relation = $value;
  140. }
  141. $fmodel = CActiveRecord::model($relation[1]);
  142. $fmodelName = $relation[1];
  143. $modelTable = ucfirst($fmodel->tableName());
  144. $fcolumns = $fmodel->attributeNames();
  145. if (method_exists($fmodel,'getRecordTitle')) {
  146. $fcolumns[1] = "recordTitle";
  147. }
  148. //$rel = $model->getActiveRelation($column->name);
  149. $relname = strtolower($fk[0]);
  150. foreach ($model->relations() as $key => $value) {
  151. if (strcasecmp($value[2], $column->name) == 0)
  152. $relname = $key;
  153. }
  154. //return("\$model->{$relname}->{$fcolumns[1]}");
  155. //return("CHtml::value(\$model,\"{$relname}.{$fcolumns[1]}\")");
  156. //return("{$relname}.{$fcolumns[1]}");
  157. if ($view === true) {
  158. return "array(
  159. 'name'=>'{$column->name}',
  160. 'value'=>CHtml::value(\$model,'{$relname}.{$fcolumns[1]}'),
  161. )";
  162. } elseif ($view == 'search')
  163. return "\$form->dropDownList(\$model,'{$column->name}',CHtml::listData({$fmodelName}::model()->findAll(), '{$fmodel->getTableSchema()->primaryKey}', '{$fcolumns[1]}'),array('prompt'=>Yii::t('app', 'All')))";
  164. else
  165. return "array(
  166. 'name'=>'{$column->name}',
  167. 'value'=>'CHtml::value(\$data,\\'{$relname}.{$fcolumns[1]}\\')',
  168. 'filter'=>CHtml::listData({$fmodelName}::model()->findAll(), '{$fcolumns[0]}', '{$fcolumns[1]}'),
  169. )";
  170. //{$relname}.{$fcolumns[1]}
  171. } else if (strtoupper($column->dbType) == 'BOOLEAN'
  172. or strtoupper($column->dbType) == 'TINYINT(1)' or
  173. strtoupper($column->dbType) == 'BIT') {
  174. if ($view) {
  175. return "array(
  176. 'name'=>'{$column->name}',
  177. 'value'=>\$model->{$column->name}?Yii::t('app', 'Yes'):Yii::t('app', 'No'),
  178. )";
  179. } else
  180. return "array(
  181. 'name'=>'{$column->name}',
  182. 'value'=>'\$data->{$column->name}?Yii::t(\\'app\\',\\'Yes\\'):Yii::t(\\'app\\', \\'No\\')',
  183. 'filter'=>array('0'=>Yii::t('app','No'),'1'=>Yii::t('app','Yes')),
  184. )";
  185. } else if($column->name == 'createtime'
  186. or $column->name == 'updatetime'
  187. or $column->name == 'timestamp')
  188. {
  189. return "'{$column->name}:datetime'";
  190. } else {
  191. return("'" . $column->name . "'");
  192. }
  193. }
  194. public function generateCFormField($model, $column) {
  195. if (strtoupper($column->dbType) == 'TINYINT(1)'
  196. || strtoupper($column->dbType) == 'BIT'
  197. || strtoupper($column->dbType) == 'BOOL'
  198. || strtoupper($column->dbType) == 'BOOLEAN') {
  199. return array(
  200. 'type' => 'check',
  201. 'value' => 1,
  202. );
  203. } else if (strtoupper($column->dbType) == 'DATE') {
  204. return array(
  205. 'class' => 'zii.widgets.jui.CJuiDatePicker',
  206. 'options' => array(
  207. 'dateFormat'=>Yii::t('app', 'jui.datepicker.dateFormat'),
  208. ),
  209. );
  210. } else if (strtoupper($column->dbType) == 'DATETIME') {
  211. return array(
  212. "class" => 'ext.widgets.datetimepicker.CJuiDateTimePicker',
  213. 'options'=>array(
  214. 'dateFormat'=>"Yii::t('app', 'jui.datepicker.dateFormat')",
  215. ),
  216. );
  217. } else if (strtoupper($column->dbType) == 'TIME') {
  218. return array(
  219. "class" => 'ext.widgets.datetimepicker.CJuiDateTimePicker',
  220. 'options'=>array(
  221. 'timeOnly' => true,
  222. ),
  223. );
  224. } else if (substr(strtoupper($column->dbType), 0, 4) == 'ENUM') {
  225. $enum_values = explode(',', substr($column->dbType, 4, strlen($column->dbType) - 1));
  226. return array(
  227. 'type' => 'select',
  228. 'options' => $enum_values
  229. );
  230. } else {
  231. return array(
  232. 'type' => 'text',
  233. 'size' => 20,
  234. );
  235. }
  236. }
  237. }
  238. ?>