Generator.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace common\components\gii\crud;
  3. use Yii;
  4. /**
  5. * Class Generator
  6. * @package backend\components\gii\crud
  7. * @author jianyan74 <751393839@qq.com>
  8. */
  9. class Generator extends \yii\gii\generators\crud\Generator
  10. {
  11. public $listFields;
  12. public $formFields;
  13. public $inputType;
  14. /**
  15. * @return array
  16. */
  17. public function fieldTypes()
  18. {
  19. return [
  20. 'text' => "文本框",
  21. 'textarea' => "文本域",
  22. 'time' => "时间",
  23. 'date' => "日期",
  24. 'datetime' => "日期时间",
  25. 'dropDownList' => "下拉文本框",
  26. 'multipleInput' => "Input组",
  27. 'radioList' => "单选按钮",
  28. 'checkboxList' => "复选框",
  29. 'baiduUEditor' => "百度编辑器",
  30. 'image' => "图片上传",
  31. 'images' => "多图上传",
  32. 'file' => "文件上传",
  33. 'files' => "多文件上传",
  34. 'cropper' => "图片裁剪上传",
  35. 'latLngSelection' => "经纬度选择",
  36. ];
  37. }
  38. /**
  39. * @inheritdoc
  40. */
  41. public function rules()
  42. {
  43. return array_merge(parent::rules(), [
  44. [['listFields', 'formFields', 'inputType'], 'safe'],
  45. ]);
  46. }
  47. /**
  48. * Generates code for active field
  49. * @param string $attribute
  50. * @return string
  51. */
  52. public function generateActiveField($attribute)
  53. {
  54. $tableSchema = $this->getTableSchema();
  55. $type = $this->inputType[$attribute] ?? '';
  56. switch ($type) {
  57. case 'text':
  58. return parent::generateActiveField($attribute);
  59. break;
  60. case 'textarea':
  61. return "\$form->field(\$model, '$attribute')->textarea()";
  62. break;
  63. case 'dropDownList':
  64. return "\$form->field(\$model, '$attribute')->dropDownList([])";
  65. break;
  66. case 'radioList':
  67. return "\$form->field(\$model, '$attribute')->radioList(\common\\enums\StatusEnum::getMap())";
  68. break;
  69. case 'checkboxList':
  70. return "\$form->field(\$model, '$attribute')->checkboxList(\common\\enums\StatusEnum::getMap())";
  71. break;
  72. case 'baiduUEditor':
  73. return "\$form->field(\$model, '$attribute')->widget(\common\widgets\ueditor\UEditor::class, [])";
  74. break;
  75. case 'color':
  76. return "\$form->field(\$model, '$attribute')->widget(\kartik\color\ColorInput::class, [
  77. 'options' => ['placeholder' => '请选择颜色'],
  78. ]);";
  79. break;
  80. case 'time':
  81. return "\$form->field(\$model, '$attribute')->widget(kartik\\time\TimePicker::class, [
  82. 'language' => 'zh-CN',
  83. 'pluginOptions' => [
  84. 'showSeconds' => true
  85. ]
  86. ])";
  87. break;
  88. case 'date':
  89. return "\$form->field(\$model, '$attribute')->widget(kartik\date\DatePicker::class, [
  90. 'language' => 'zh-CN',
  91. 'layout'=>'{picker}{input}',
  92. 'pluginOptions' => [
  93. 'format' => 'yyyy-mm-dd',
  94. 'todayHighlight' => true, // 今日高亮
  95. 'autoclose' => true, // 选择后自动关闭
  96. 'todayBtn' => true, // 今日按钮显示
  97. ],
  98. 'options'=>[
  99. 'class' => 'form-control no_bor',
  100. ]
  101. ])";
  102. break;
  103. case 'datetime':
  104. return "\$form->field(\$model, '$attribute')->widget(kartik\datetime\DateTimePicker::class, [
  105. 'language' => 'zh-CN',
  106. 'options' => [
  107. 'value' => \$model->isNewRecord ? date('Y-m-d H:i:s') : date('Y-m-d H:i:s',\$model->$attribute),
  108. ],
  109. 'pluginOptions' => [
  110. 'format' => 'yyyy-mm-dd hh:ii',
  111. 'todayHighlight' => true, // 今日高亮
  112. 'autoclose' => true, // 选择后自动关闭
  113. 'todayBtn' => true, // 今日按钮显示
  114. ]
  115. ])";
  116. break;
  117. case 'multipleInput':
  118. return "\$form->field(\$model, '$attribute')->widget(unclead\multipleinput\MultipleInput::class, [
  119. 'max' => 4,
  120. 'columns' => [
  121. [
  122. 'name' => 'user_id',
  123. 'type' => 'dropDownList',
  124. 'title' => 'User',
  125. 'defaultValue' => 1,
  126. 'items' => [
  127. 1 => 'User 1',
  128. 2 => 'User 2'
  129. ]
  130. ],
  131. [
  132. 'name' => 'day',
  133. 'type' => \kartik\date\DatePicker::class,
  134. 'title' => 'Day',
  135. 'value' => function(\$data) {
  136. return \$data['day'];
  137. },
  138. 'items' => [
  139. '0' => 'Saturday',
  140. '1' => 'Monday'
  141. ],
  142. 'options' => [
  143. 'pluginOptions' => [
  144. 'format' => 'dd.mm.yyyy',
  145. 'todayHighlight' => true
  146. ]
  147. ]
  148. ],
  149. [
  150. 'name' => 'priority',
  151. 'title' => 'Priority',
  152. 'enableError' => true,
  153. 'options' => [
  154. 'class' => 'input-priority'
  155. ]
  156. ]
  157. ]
  158. ])";
  159. break;
  160. case 'cropper':
  161. return "\$form->field(\$model, '$attribute')->widget(\common\widgets\cropper\Cropper::class, [
  162. 'config' => [
  163. // 可设置自己的上传地址, 不设置则默认地址
  164. // 'server' => '',
  165. ],
  166. 'formData' => [
  167. // 'drive' => 'local',// 默认本地 支持 qiniu/oss/cos 上传
  168. ],
  169. ]);";
  170. break;
  171. case 'latLngSelection':
  172. return "\$form->field(\$model, '$attribute')->widget(\common\widgets\map\Map::class, [
  173. 'type' => 'amap', // amap高德;tencent:腾讯;baidu:百度
  174. ])->hint('点击地图某处才会获取到经纬度,否则默认北京')";
  175. break;
  176. case 'image':
  177. return "\$form->field(\$model, '$attribute')->widget(\common\widgets\webuploader\Files::class, [
  178. 'type' => 'images',
  179. 'theme' => 'default',
  180. 'themeConfig' => [],
  181. 'config' => [
  182. // 可设置自己的上传地址, 不设置则默认地址
  183. // 'server' => '',
  184. 'pick' => [
  185. 'multiple' => false,
  186. ],
  187. ]
  188. ]);";
  189. break;
  190. case 'images':
  191. return "\$form->field(\$model, '$attribute')->widget(\common\widgets\webuploader\Files::class, [
  192. 'type' => 'images',
  193. 'theme' => 'default',
  194. 'themeConfig' => [],
  195. 'config' => [
  196. // 可设置自己的上传地址, 不设置则默认地址
  197. // 'server' => '',
  198. 'pick' => [
  199. 'multiple' => true,
  200. ],
  201. ]
  202. ]);";
  203. break;
  204. case 'file':
  205. return "\$form->field(\$model, '$attribute')->widget(\common\widgets\webuploader\Files::class, [
  206. 'type' => 'files',
  207. 'theme' => 'default',
  208. 'themeConfig' => [],
  209. 'config' => [
  210. // 可设置自己的上传地址, 不设置则默认地址
  211. // 'server' => '',
  212. 'pick' => [
  213. 'multiple' => false,
  214. ],
  215. ]
  216. ]);";
  217. break;
  218. case 'files':
  219. return "\$form->field(\$model, '$attribute')->widget(\common\widgets\webuploader\Files::class, [
  220. 'type' => 'files',
  221. 'theme' => 'default',
  222. 'themeConfig' => [],
  223. 'config' => [
  224. // 可设置自己的上传地址, 不设置则默认地址
  225. // 'server' => '',
  226. 'pick' => [
  227. 'multiple' => true,
  228. ],
  229. ]
  230. ]);";
  231. break;
  232. }
  233. }
  234. }
粤ICP备19079148号