PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/_/app/Helpers/Core/_Old/DynamicItem.php

https://gitlab.com/billyprice1/MFAdmin
PHP | 450 lines | 349 code | 97 blank | 4 comment | 35 complexity | 181ec72fee92595935e183aeff6ba3f7 MD5 | raw file
  1. <?php namespace App\Helpers\Core;
  2. use App\Http\Controllers\CoreController;
  3. use App\Models\Image_Generation_Queue;
  4. use Illuminate\Database\Eloquent\ModelNotFoundException;
  5. trait DynamicItem
  6. {
  7. protected $dynamic_item_options;
  8. private function setDefaultDynamicItemOptions()
  9. {
  10. $this->dynamic_item_options =
  11. [
  12. 'model' => NULL,
  13. 'identifier' =>
  14. [
  15. 'singular' => NULL,
  16. 'plural' => NULL
  17. ],
  18. 'title_column' => NULL,
  19. 'save' =>
  20. [
  21. 'add' =>
  22. [
  23. 'success' =>
  24. [
  25. 'message' => NULL,
  26. 'redirect' => NULL
  27. ]
  28. ],
  29. 'edit' =>
  30. [
  31. 'success' =>
  32. [
  33. 'message' => NULL,
  34. 'redirect' => NULL
  35. ]
  36. ]
  37. ],
  38. 'item_not_found' =>
  39. [
  40. 'message' => NULL,
  41. 'redirect' => NULL
  42. ],
  43. 'breadcrumb_items' =>
  44. [
  45. 'pre' => NULL
  46. ],
  47. 'tabs' => NULL
  48. ];
  49. }
  50. protected function initDynamicItem(array $options = [])
  51. {
  52. $this->setDefaultDynamicItemOptions();
  53. if ( is_array($options) && count($options) > 0 )
  54. {
  55. $this->dynamic_item_options = array_merge($this->dynamic_item_options, $options);
  56. }
  57. if ( !isset($this->dynamic_item_options['title_column']) )
  58. {
  59. throw new \Exception('Missing required dynamic item option "title_column".');
  60. }
  61. if ( !isset($this->dynamic_item_options['save']) )
  62. {
  63. throw new \Exception('Missing required dynamic item option "save".');
  64. }
  65. $model = $this->dynamic_item_options['model'];
  66. if ( !method_exists($model, 'getDynamicItemColumns') )
  67. {
  68. throw new \Exception($model . ' is missing required dynamic model function getDynamicItemColumns.');
  69. }
  70. foreach ( $model::getDynamicItemColumns() as $column_id => $column )
  71. {
  72. if ( !isset($column['title']) )
  73. {
  74. throw new \Exception('Column "' . $column_id . '" in ' . $model . ' is missing required title.');
  75. }
  76. if ( !isset($column['form']['type']) )
  77. {
  78. throw new \Exception('Column "' . $column_id . '" in ' . $model . ' is missing required form type.');
  79. }
  80. }
  81. }
  82. public function dynamicItem($id = NULL)
  83. {
  84. $called_class = get_called_class();
  85. $model = $this->dynamic_item_options['model'];
  86. $model::init();
  87. if ( $id !== NULL )
  88. {
  89. try
  90. {
  91. $title_column = $this->dynamic_item_options['title_column'];
  92. $item_to_edit = $model::where('id', $id)
  93. ->firstOrFail();
  94. }
  95. catch ( ModelNotFoundException $e )
  96. {
  97. $this->ui->showWarning(sprintf($this->dynamic_item_options['item_not_found']['message'], $id));
  98. return \Redirect::to($this->dynamic_item_options['item_not_found']['redirect']);
  99. }
  100. }
  101. else
  102. {
  103. $item_to_edit = NULL;
  104. }
  105. $this->assign('item_to_edit', $item_to_edit);
  106. $this->assign('item_id_to_edit', ($item_to_edit !== NULL ? $item_to_edit->id : NULL), CoreController::SECTION_JS);
  107. $this->assign('title_column', $this->dynamic_item_options['title_column']);
  108. $this->assign('identifier', $this->dynamic_item_options['identifier']);
  109. $form_html_view_filename = $this->current_controller['underscore'] . '/' . $this->dynamic_item_options['identifier']['singular'];
  110. $form_html_view_filepath = base_path('resources/templates/' . $form_html_view_filename);
  111. $form_html = NULL;
  112. if ( file_exists($form_html_view_filepath . '.php') )
  113. {
  114. $form_html_view = view($form_html_view_filename);
  115. $form_html_view->dynamic_item = $this;
  116. $form_html_view->item_to_edit = $item_to_edit;
  117. if ( method_exists($called_class, 'getDynamicItemFormHTML') )
  118. {
  119. $form_html_view = $this->getDynamicItemFormHTML($form_html_view, $item_to_edit);
  120. }
  121. $form_html = $form_html_view->render();
  122. }
  123. $this->assign('dynamic_item_columns', $model::getDynamicItemColumns(), 'js');
  124. $this->assign('form_html', $form_html);
  125. if ( is_array($this->dynamic_item_options['breadcrumb_items']['pre']) )
  126. {
  127. foreach ( $this->dynamic_item_options['breadcrumb_items']['pre'] as $breadcrumb_item )
  128. {
  129. $this->addBreadcrumbItem($breadcrumb_item['text'], $breadcrumb_item['url']);
  130. }
  131. }
  132. $this->addBreadcrumbItem($item_to_edit !== NULL ? e($item_to_edit->$title_column) : 'Add');
  133. // Tabs
  134. if ( is_array($this->dynamic_item_options['tabs']) )
  135. {
  136. $tabs = $this->dynamic_item_options['tabs'];
  137. foreach ( $tabs as $tab_id => $tab_data )
  138. {
  139. $tab_view_filename = $this->current_controller['underscore'] . '/' . $this->dynamic_item_options['identifier']['singular'] . '/tabs/' . $tab_id;
  140. $tab_view_filepath = base_path('resources/templates/' . $tab_view_filename);
  141. if ( !file_exists($tab_view_filepath . '.php') )
  142. {
  143. throw new \Exception($called_class . ' is missing tab file "' . $tab_view_filename . '.php".');
  144. }
  145. $tab_view = view($tab_view_filename);
  146. $tab_view->dynamic_item = $this;
  147. $tab_view->item_to_edit = $item_to_edit;
  148. if ( method_exists($called_class, 'getDynamicItemTabHTML') )
  149. {
  150. $tab_view = $this->getDynamicItemTabHTML($tab_view, $item_to_edit, $tab_id);
  151. }
  152. $tabs[$tab_id] =
  153. [
  154. 'text' => $tab_data['text'],
  155. 'only_edit' => (isset($tab_data['only_edit']) && $tab_data['only_edit'] === TRUE),
  156. 'active_toggle' => (isset($tab_data['active_toggle']) && $tab_data['active_toggle'] === TRUE),
  157. 'save_button' => (isset($tab_data['save_button']) && $tab_data['save_button'] === TRUE),
  158. 'html' => $tab_view->render()
  159. ];
  160. }
  161. $this->assign('tabs', $tabs);
  162. }
  163. $this->assign('dynamic_item', $this);
  164. // Active toggle
  165. $active_toggle = $model::getOption('active_toggle');
  166. if ( $active_toggle !== NULL )
  167. {
  168. $active_toggle_column = $active_toggle['column'];
  169. $active_toggle_view = view ('dashboard/partials/dynamic_item/form_fields/toggle');
  170. $active_toggle_view->name = $active_toggle_column;
  171. $active_toggle_view->title = $active_toggle['title'];
  172. $active_toggle_view->checked = ($item_to_edit !== NULL && $item_to_edit->$active_toggle_column === 'yes');
  173. $this->assign('active_toggle', $active_toggle_view->render());
  174. }
  175. $this->setPage($this->dynamic_item_options['identifier']['singular']);
  176. $this->assign('dynamic_item_options', $this->dynamic_item_options, 'js');
  177. $this->assign('ALWAYS_REQUIRED', ALWAYS_REQUIRED, self::SECTION_JS);
  178. $this->assign('REQUIRED_ON_ADD', REQUIRED_ON_ADD, self::SECTION_JS);
  179. $this->assign('NOT_REQUIRED', NOT_REQUIRED, self::SECTION_JS);
  180. return $this->display([($item_to_edit !== NULL ? e($item_to_edit->$title_column) : 'Add'), ucfirst($this->dynamic_item_options['identifier']['plural']), 'Work'], TRUE, 'dashboard/partials/dynamic_item/dynamic_item');
  181. }
  182. public function getDynamicItemColumn($column_id)
  183. {
  184. $model = $this->dynamic_item_options['model'];
  185. $columns = $model::getDynamicItemColumns();
  186. if ( !isset($columns[$column_id]) )
  187. {
  188. throw new \Exception('Could not find column "' . $column_id . '" in ' . $model . '.');
  189. }
  190. return $columns[$column_id];
  191. }
  192. public function getFormFieldHTML($column_id, $item_to_edit, $params = NULL)
  193. {
  194. $column_data = self::getDynamicItemColumn($column_id);
  195. $form_field_view = view('dashboard/partials/dynamic_item/form_fields/' . $column_data['form']['type']);
  196. $form_field_view->column_id = $column_id;
  197. $form_field_view->column_data = $column_data;
  198. $form_field_view->item_to_edit = $item_to_edit;
  199. $form_field_view->params = $params;
  200. if ( isset($params['options']) )
  201. {
  202. $form_field_view->num_options = count($params['options']);
  203. $form_field_view->options = $params['options'];
  204. if ( $column_data['form']['type'] === 'select' && isset($params['selected_option']) )
  205. {
  206. $form_field_view->selected_option = $params['selected_option'];
  207. }
  208. elseif ( $column_data['form']['type'] === 'checkbox' && isset($params['selected_options']) )
  209. {
  210. $form_field_view->selected_options = $params['selected_options'];
  211. }
  212. }
  213. return $form_field_view->render();
  214. }
  215. public function saveDynamicItem($id = NULL)
  216. {
  217. $called_class = get_called_class();
  218. $input = \Input::all();
  219. $have_custom_saving_columns = ($input['have_custom_saving_columns'] === 'yes');
  220. $items_to_delete = (isset($input['items_to_delete']) ? $input['items_to_delete'] : []);
  221. $model = $this->dynamic_item_options['model'];
  222. $dynamic_item_title_column = $this->dynamic_item_options['title_column'];
  223. if ( $id !== NULL )
  224. {
  225. try
  226. {
  227. $item_to_edit = $model::where('id', $id)->firstOrFail();
  228. if ( method_exists($called_class, 'dynamicItemPreEdit') )
  229. {
  230. $this->dynamicItemPreEdit($item_to_edit);
  231. }
  232. // Delete
  233. foreach ( $items_to_delete as $item_to_delete_column_id )
  234. {
  235. $this->deleteDynamicItemColumn($item_to_edit, $item_to_delete_column_id);
  236. }
  237. // Edit
  238. $item_to_edit->edit($input);
  239. if ( method_exists($called_class, 'dynamicItemPostEdit') )
  240. {
  241. $this->dynamicItemPostEdit($item_to_edit);
  242. }
  243. $success_message = sprintf($this->dynamic_item_options['save']['edit']['success']['message'], $item_to_edit->$dynamic_item_title_column);
  244. if ( $have_custom_saving_columns === TRUE )
  245. {
  246. $this->ui->showSuccess($success_message);
  247. }
  248. else
  249. {
  250. $this->ajax->showSuccess($success_message);
  251. return $this->ajax->redirect((isset($this->dynamic_item_options['save']['edit']['success']['redirect']) ? $this->dynamic_item_options['save']['edit']['success']['redirect'] : ''), 750);
  252. }
  253. }
  254. catch ( \Illuminate\Database\Eloquent\ModelNotFoundException $e )
  255. {
  256. $this->ajax->showWarning(sprintf($this->dynamic_item_options['item_not_found']['message'], $id));
  257. return $this->ajax->output();
  258. }
  259. }
  260. else
  261. {
  262. $added_item = $model::add($input);
  263. if ( method_exists($called_class, 'dynamicItemPostAdd') )
  264. {
  265. $this->dynamicItemPostAdd($added_item);
  266. }
  267. $this->ajax->addData('added_item_id', $added_item->id);
  268. $success_message = sprintf($this->dynamic_item_options['save']['add']['success']['message'], $added_item->$dynamic_item_title_column);
  269. if ( $have_custom_saving_columns === TRUE )
  270. {
  271. $this->ui->showSuccess($success_message);
  272. }
  273. else
  274. {
  275. $this->ajax->showSuccess($success_message);
  276. return $this->ajax->redirect($this->dynamic_item_options['save']['add']['success']['redirect'], 750);
  277. }
  278. }
  279. return $this->ajax->output();
  280. }
  281. public function deleteDynamicItemColumn($item_to_edit, $column_id)
  282. {
  283. $model = $this->dynamic_item_options['model'];
  284. $columns = $model::getDynamicItemColumns();
  285. $column_to_delete = $columns[$column_id];
  286. if ( $column_to_delete['form']['type'] === 'image' )
  287. {
  288. $image_directory = $model::getDynamicImageDirectory($item_to_edit->id, $column_id);
  289. \App\Helpers\Core\Directory::delete($image_directory, TRUE);
  290. $item_to_edit->$column_id = null;
  291. $item_to_edit->save();
  292. }
  293. }
  294. public function deleteDynamicItem($id)
  295. {
  296. $model = $this->dynamic_item_options['model'];
  297. $id = \Input::get('id');
  298. try
  299. {
  300. $item_to_delete = $model::where('id', $id)->firstOrFail();
  301. $item_to_delete->delete();
  302. }
  303. catch ( ModelNotFoundException $e )
  304. {
  305. $this->ui->showWarning(sprintf($this->dynamic_item_options['item_not_found']['message'], $id));
  306. return \Redirect::to($this->dynamic_item_options['item_not_found']['redirect']);
  307. }
  308. return $this->ajax->output();
  309. }
  310. public function uploadDynamicItemImage()
  311. {
  312. $id = $_SERVER['HTTP_ID'];
  313. $mime = $_SERVER['HTTP_MIME'];
  314. $column_id = $_SERVER['HTTP_COLUMN_ID'];
  315. $model = $this->dynamic_item_options['model'];
  316. if ( !method_exists($model, 'getDynamicImageDirectory') )
  317. {
  318. throw new \Exception($model . ' is missing required function "getDynamicImageDirectory".');
  319. }
  320. if ( !method_exists($model, 'setDynamicImage') )
  321. {
  322. throw new \Exception($model . ' is missing required function "setDynamicImage".');
  323. }
  324. $called_class = get_called_class();
  325. $column_options = $called_class::getDynamicItemColumn($column_id);
  326. try
  327. {
  328. $item = $model::where('id', $id)->firstOrFail();
  329. $upload_dir = $model::getDynamicImageDirectory($item->id, $column_id);
  330. if ( !file_exists($upload_dir) )
  331. {
  332. mkdir($upload_dir, 0775, TRUE);
  333. }
  334. $file_extension = \App\Helpers\Core\File::getExtensionFromMime($mime);
  335. $upload_filename = 'original.' . $file_extension;
  336. $upload_filepath = $upload_dir . $upload_filename;
  337. $input_fp = fopen('php://input', 'rb');
  338. $output_fp = fopen($upload_filepath, 'wb');
  339. while ( $chunk = fread($input_fp, 8192) )
  340. {
  341. fwrite($output_fp, $chunk);
  342. }
  343. fclose($input_fp);
  344. fclose($output_fp);
  345. Image_Generation_Queue::add($column_options['image_generation_queue'], $item->id);
  346. $item->setDynamicImage($column_id, $file_extension, $mime);
  347. }
  348. catch ( ModelNotFoundException $e )
  349. {
  350. }
  351. return \Response::json();
  352. }
  353. }