PageRenderTime 68ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/appzcoder/crud-generator/src/Commands/CrudViewCommand.php

https://gitlab.com/m12_team/m12
PHP | 475 lines | 255 code | 55 blank | 165 comment | 24 complexity | 8bf8923e494c1b607bd316c1ccf90fa1 MD5 | raw file
  1. <?php
  2. namespace Appzcoder\CrudGenerator\Commands;
  3. use File;
  4. use Illuminate\Console\Command;
  5. class CrudViewCommand extends Command
  6. {
  7. /**
  8. * The name and signature of the console command.
  9. *
  10. * @var string
  11. */
  12. protected $signature = 'crud:view
  13. {name : The name of the Crud.}
  14. {--fields= : The fields name for the form.}
  15. {--view-path= : The name of the view path.}
  16. {--route-group= : Prefix of the route group.}
  17. {--pk=id : The name of the primary key.}
  18. {--localize=yes : Localize the view? yes|no.}';
  19. /**
  20. * The console command description.
  21. *
  22. * @var string
  23. */
  24. protected $description = 'Create views for the Crud.';
  25. /**
  26. * View Directory Path.
  27. *
  28. * @var string
  29. */
  30. protected $viewDirectoryPath;
  31. /**
  32. * Form field types collection.
  33. *
  34. * @var array
  35. */
  36. protected $typeLookup = [
  37. 'string' => 'text',
  38. 'char' => 'text',
  39. 'varchar' => 'text',
  40. 'text' => 'textarea',
  41. 'mediumtext' => 'textarea',
  42. 'longtext' => 'textarea',
  43. 'json' => 'textarea',
  44. 'jsonb' => 'textarea',
  45. 'binary' => 'textarea',
  46. 'password' => 'password',
  47. 'email' => 'email',
  48. 'number' => 'number',
  49. 'integer' => 'number',
  50. 'bigint' => 'number',
  51. 'mediumint' => 'number',
  52. 'tinyint' => 'number',
  53. 'smallint' => 'number',
  54. 'decimal' => 'number',
  55. 'double' => 'number',
  56. 'float' => 'number',
  57. 'date' => 'date',
  58. 'datetime' => 'datetime-local',
  59. 'timestamp' => 'datetime-local',
  60. 'time' => 'time',
  61. 'boolean' => 'radio',
  62. ];
  63. /**
  64. * Form's fields.
  65. *
  66. * @var array
  67. */
  68. protected $formFields = [];
  69. /**
  70. * Html of Form's fields.
  71. *
  72. * @var string
  73. */
  74. protected $formFieldsHtml = '';
  75. /**
  76. * Number of columns to show from the table. Others are hidden.
  77. *
  78. * @var integer
  79. */
  80. protected $defaultColumsToShow = 3;
  81. /**
  82. * Name of the Crud.
  83. *
  84. * @var string
  85. */
  86. protected $crudName = '';
  87. /**
  88. * Crud Name in capital form.
  89. *
  90. * @var string
  91. */
  92. protected $crudNameCap = '';
  93. /**
  94. * Crud Name in singular form.
  95. *
  96. * @var string
  97. */
  98. protected $crudNameSingular = '';
  99. /**
  100. * Primary key of the model.
  101. *
  102. * @var string
  103. */
  104. protected $primaryKey = 'id';
  105. /**
  106. * Name of the Model.
  107. *
  108. * @var string
  109. */
  110. protected $modelName = '';
  111. /**
  112. * Name or prefix of the Route Group.
  113. *
  114. * @var string
  115. */
  116. protected $routeGroup = '';
  117. /**
  118. * Html of the form heading.
  119. *
  120. * @var string
  121. */
  122. protected $formHeadingHtml = '';
  123. /**
  124. * Html of the form body.
  125. *
  126. * @var string
  127. */
  128. protected $formBodyHtml = '';
  129. /**
  130. * Html of view to show.
  131. *
  132. * @var string
  133. */
  134. protected $formBodyHtmlForShowView = '';
  135. /**
  136. * Create a new command instance.
  137. *
  138. * @return void
  139. */
  140. public function __construct()
  141. {
  142. parent::__construct();
  143. $this->viewDirectoryPath = config('crudgenerator.custom_template')
  144. ? config('crudgenerator.path')
  145. : __DIR__ . '/../stubs/';
  146. if (config('crudgenerator.view_columns_number')) {
  147. $this->defaultColumsToShow = config('crudgenerator.view_columns_number');
  148. }
  149. }
  150. /**
  151. * Execute the console command.
  152. *
  153. * @return void
  154. */
  155. public function handle()
  156. {
  157. $this->crudName = $this->argument('name');
  158. $this->crudNameCap = ucwords($this->crudName);
  159. $this->crudNameSingular = str_singular($this->crudName);
  160. $this->modelName = ucwords($this->crudNameSingular);
  161. $this->primaryKey = $this->option('pk');
  162. $this->routeGroup = ($this->option('route-group')) ? $this->option('route-group') . '/' : $this->option('route-group');
  163. $viewDirectory = config('view.paths')[0] . '/';
  164. if ($this->option('view-path')) {
  165. $userPath = $this->option('view-path');
  166. $path = $viewDirectory . $userPath . '/' . $this->crudName . '/';
  167. } else {
  168. $path = $viewDirectory . $this->crudName . '/';
  169. }
  170. if (!File::isDirectory($path)) {
  171. File::makeDirectory($path, 0755, true);
  172. }
  173. $fields = $this->option('fields');
  174. $fieldsArray = explode(',', $fields);
  175. $this->formFields = array();
  176. if ($fields) {
  177. $x = 0;
  178. foreach ($fieldsArray as $item) {
  179. $itemArray = explode('#', $item);
  180. $this->formFields[$x]['name'] = trim($itemArray[0]);
  181. $this->formFields[$x]['type'] = trim($itemArray[1]);
  182. $this->formFields[$x]['required'] = (isset($itemArray[2]) && (trim($itemArray[2]) == 'req' || trim($itemArray[2]) == 'required')) ? true : false;
  183. $x++;
  184. }
  185. }
  186. foreach ($this->formFields as $item) {
  187. $this->formFieldsHtml .= $this->createField($item);
  188. }
  189. $i = 0;
  190. foreach ($this->formFields as $key => $value) {
  191. if ($i == $this->defaultColumsToShow) {
  192. break;
  193. }
  194. $field = $value['name'];
  195. $label = ucwords(str_replace('_', ' ', $field));
  196. if ($this->option('localize') == 'yes') {
  197. $label = '{{ trans(\'' . $this->crudName . '.' . $field . '\') }}';
  198. }
  199. $this->formHeadingHtml .= '<th> ' . $label . ' </th>';
  200. $this->formBodyHtml .= '<td>{{ $item->' . $field . ' }}</td>';
  201. $this->formBodyHtmlForShowView .= '<tr><th> ' . $label . ' </th><td> {{ $%%crudNameSingular%%->' . $field . ' }} </td></tr>';
  202. $i++;
  203. }
  204. // For index.blade.php file
  205. $indexFile = $this->viewDirectoryPath . 'index.blade.stub';
  206. $newIndexFile = $path . 'index.blade.php';
  207. if (!File::copy($indexFile, $newIndexFile)) {
  208. echo "failed to copy $indexFile...\n";
  209. } else {
  210. $this->templateIndexVars($newIndexFile);
  211. }
  212. // For create.blade.php file
  213. $createFile = $this->viewDirectoryPath . 'create.blade.stub';
  214. $newCreateFile = $path . 'create.blade.php';
  215. if (!File::copy($createFile, $newCreateFile)) {
  216. echo "failed to copy $createFile...\n";
  217. } else {
  218. $this->templateCreateVars($newCreateFile);
  219. }
  220. // For edit.blade.php file
  221. $editFile = $this->viewDirectoryPath . 'edit.blade.stub';
  222. $newEditFile = $path . 'edit.blade.php';
  223. if (!File::copy($editFile, $newEditFile)) {
  224. echo "failed to copy $editFile...\n";
  225. } else {
  226. $this->templateEditVars($newEditFile);
  227. }
  228. // For show.blade.php file
  229. $showFile = $this->viewDirectoryPath . 'show.blade.stub';
  230. $newShowFile = $path . 'show.blade.php';
  231. if (!File::copy($showFile, $newShowFile)) {
  232. echo "failed to copy $showFile...\n";
  233. } else {
  234. $this->templateShowVars($newShowFile);
  235. }
  236. $this->info('View created successfully.');
  237. }
  238. /**
  239. * Update values between %% with real values in index view.
  240. *
  241. * @param string $newIndexFile
  242. *
  243. * @return void
  244. */
  245. public function templateIndexVars($newIndexFile)
  246. {
  247. File::put($newIndexFile, str_replace('%%formHeadingHtml%%', $this->formHeadingHtml, File::get($newIndexFile)));
  248. File::put($newIndexFile, str_replace('%%formBodyHtml%%', $this->formBodyHtml, File::get($newIndexFile)));
  249. File::put($newIndexFile, str_replace('%%crudName%%', $this->crudName, File::get($newIndexFile)));
  250. File::put($newIndexFile, str_replace('%%crudNameCap%%', $this->crudNameCap, File::get($newIndexFile)));
  251. File::put($newIndexFile, str_replace('%%modelName%%', $this->modelName, File::get($newIndexFile)));
  252. File::put($newIndexFile, str_replace('%%routeGroup%%', $this->routeGroup, File::get($newIndexFile)));
  253. File::put($newIndexFile, str_replace('%%primaryKey%%', $this->primaryKey, File::get($newIndexFile)));
  254. }
  255. /**
  256. * Update values between %% with real values in create view.
  257. *
  258. * @param string $newCreateFile
  259. *
  260. * @return void
  261. */
  262. public function templateCreateVars($newCreateFile)
  263. {
  264. File::put($newCreateFile, str_replace('%%crudName%%', $this->crudName, File::get($newCreateFile)));
  265. File::put($newCreateFile, str_replace('%%crudNameCap%%', $this->crudNameCap, File::get($newCreateFile)));
  266. File::put($newCreateFile, str_replace('%%modelName%%', $this->modelName, File::get($newCreateFile)));
  267. File::put($newCreateFile, str_replace('%%routeGroup%%', $this->routeGroup, File::get($newCreateFile)));
  268. File::put($newCreateFile, str_replace('%%formFieldsHtml%%', $this->formFieldsHtml, File::get($newCreateFile)));
  269. }
  270. /**
  271. * Update values between %% with real values in edit view.
  272. *
  273. * @param string $newEditFile
  274. *
  275. * @return void
  276. */
  277. public function templateEditVars($newEditFile)
  278. {
  279. File::put($newEditFile, str_replace('%%crudName%%', $this->crudName, File::get($newEditFile)));
  280. File::put($newEditFile, str_replace('%%crudNameSingular%%', $this->crudNameSingular, File::get($newEditFile)));
  281. File::put($newEditFile, str_replace('%%crudNameCap%%', $this->crudNameCap, File::get($newEditFile)));
  282. File::put($newEditFile, str_replace('%%modelName%%', $this->modelName, File::get($newEditFile)));
  283. File::put($newEditFile, str_replace('%%routeGroup%%', $this->routeGroup, File::get($newEditFile)));
  284. File::put($newEditFile, str_replace('%%formFieldsHtml%%', $this->formFieldsHtml, File::get($newEditFile)));
  285. File::put($newEditFile, str_replace('%%primaryKey%%', $this->primaryKey, File::get($newEditFile)));
  286. }
  287. /**
  288. * Update values between %% with real values in show view.
  289. *
  290. * @param string $newShowFile
  291. *
  292. * @return void
  293. */
  294. public function templateShowVars($newShowFile)
  295. {
  296. File::put($newShowFile, str_replace('%%formHeadingHtml%%', $this->formHeadingHtml, File::get($newShowFile)));
  297. File::put($newShowFile, str_replace('%%formBodyHtmlForShowView%%', $this->formBodyHtmlForShowView, File::get($newShowFile)));
  298. File::put($newShowFile, str_replace('%%crudNameSingular%%', $this->crudNameSingular, File::get($newShowFile)));
  299. File::put($newShowFile, str_replace('%%crudNameCap%%', $this->crudNameCap, File::get($newShowFile)));
  300. File::put($newShowFile, str_replace('%%modelName%%', $this->modelName, File::get($newShowFile)));
  301. File::put($newShowFile, str_replace('%%primaryKey%%', $this->primaryKey, File::get($newShowFile)));
  302. File::put($newShowFile, str_replace('%%crudName%%', $this->crudName, File::get($newShowFile)));
  303. File::put($newShowFile, str_replace('%%routeGroup%%', $this->routeGroup, File::get($newShowFile)));
  304. }
  305. /**
  306. * Form field wrapper.
  307. *
  308. * @param string $item
  309. * @param string $field
  310. *
  311. * @return void
  312. */
  313. protected function wrapField($item, $field)
  314. {
  315. $formGroup =
  316. <<<EOD
  317. <div class="form-group {{ \$errors->has('%1\$s') ? 'has-error' : ''}}">
  318. {!! Form::label('%1\$s', %2\$s, ['class' => 'col-sm-3 control-label']) !!}
  319. <div class="col-sm-6">
  320. %3\$s
  321. {!! \$errors->first('%1\$s', '<p class="help-block">:message</p>') !!}
  322. </div>
  323. </div>\n
  324. EOD;
  325. $labelText = "'" . ucwords(strtolower(str_replace('_', ' ', $item['name']))) . "'";
  326. if ($this->option('localize') == 'yes') {
  327. $labelText = 'trans(\'' . $this->crudName . '.' . $item['name'] . '\')';
  328. }
  329. return sprintf($formGroup, $item['name'], $labelText, $field);
  330. }
  331. /**
  332. * Form field generator.
  333. *
  334. * @param string $item
  335. *
  336. * @return string
  337. */
  338. protected function createField($item)
  339. {
  340. switch ($this->typeLookup[$item['type']]) {
  341. case 'password':
  342. return $this->createPasswordField($item);
  343. break;
  344. case 'datetime-local':
  345. case 'time':
  346. return $this->createInputField($item);
  347. break;
  348. case 'radio':
  349. return $this->createRadioField($item);
  350. break;
  351. default: // text
  352. return $this->createFormField($item);
  353. }
  354. }
  355. /**
  356. * Create a specific field using the form helper.
  357. *
  358. * @param string $item
  359. *
  360. * @return string
  361. */
  362. protected function createFormField($item)
  363. {
  364. $required = ($item['required'] === true) ? ", 'required' => 'required'" : "";
  365. return $this->wrapField(
  366. $item,
  367. "{!! Form::" . $this->typeLookup[$item['type']] . "('" . $item['name'] . "', null, ['class' => 'form-control'$required]) !!}"
  368. );
  369. }
  370. /**
  371. * Create a password field using the form helper.
  372. *
  373. * @param string $item
  374. *
  375. * @return string
  376. */
  377. protected function createPasswordField($item)
  378. {
  379. $required = ($item['required'] === true) ? ", 'required' => 'required'" : "";
  380. return $this->wrapField(
  381. $item,
  382. "{!! Form::password('" . $item['name'] . "', ['class' => 'form-control'$required]) !!}"
  383. );
  384. }
  385. /**
  386. * Create a generic input field using the form helper.
  387. *
  388. * @param string $item
  389. *
  390. * @return string
  391. */
  392. protected function createInputField($item)
  393. {
  394. $required = ($item['required'] === true) ? ", 'required' => 'required'" : "";
  395. return $this->wrapField(
  396. $item,
  397. "{!! Form::input('" . $this->typeLookup[$item['type']] . "', '" . $item['name'] . "', null, ['class' => 'form-control'$required]) !!}"
  398. );
  399. }
  400. /**
  401. * Create a yes/no radio button group using the form helper.
  402. *
  403. * @param string $item
  404. *
  405. * @return string
  406. */
  407. protected function createRadioField($item)
  408. {
  409. $field =
  410. <<<EOD
  411. <div class="checkbox">
  412. <label>{!! Form::radio('%1\$s', '1') !!} Yes</label>
  413. </div>
  414. <div class="checkbox">
  415. <label>{!! Form::radio('%1\$s', '0', true) !!} No</label>
  416. </div>
  417. EOD;
  418. return $this->wrapField($item, sprintf($field, $item['name']));
  419. }
  420. }