PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Http/Controllers/backend/ContentsController.php

https://gitlab.com/techniconline/kmc
PHP | 294 lines | 216 code | 35 blank | 43 comment | 18 complexity | 815c62a313313452a3e947aa5f5163d5 MD5 | raw file
  1. <?php namespace App\Http\Controllers\backend;
  2. use App\BaseModel;
  3. use App\Category;
  4. use App\Content;
  5. use App\ContentLang;
  6. use App\Language;
  7. use App\Media;
  8. use App\Providers\Helpers\Category\CategoryHelper;
  9. use Illuminate\Support\Facades\Auth;
  10. use Illuminate\Support\Facades\Config;
  11. use Illuminate\Support\Facades\Input;
  12. use Illuminate\Support\Facades\Redirect;
  13. use Laracasts\Flash\Flash;
  14. use Intervention\Image\Facades\Image;
  15. use Illuminate\Support\Facades\File;
  16. use Illuminate\Support\Facades\Session;
  17. use Illuminate\Support\Facades\Lang;
  18. use Illuminate\Support\Facades\Request;
  19. class ContentsController extends BackendController
  20. {
  21. public $content;
  22. public $category;
  23. public $locale;
  24. public $langActive;
  25. public $language;
  26. public $listLang;
  27. public function __construct(Category $category, Content $content, Language $language)
  28. {
  29. $this->content = $content;
  30. $this->category = $category;
  31. $this->listLang = Config::get('app.locales');
  32. $this->langActive = app('getLocale');
  33. $this->language = $language;
  34. $this->locale = app('locale') == '' ? '' : app('locale') . '.';
  35. }
  36. /**
  37. * Display a listing of the resource.
  38. *
  39. * @return Response
  40. */
  41. public function index()
  42. {
  43. $contents = $this->content
  44. ->join('contents_lang', 'contents_id', '=', 'contents.id')
  45. ->join('employee', 'contents.author_id', '=', 'employee.id')
  46. ->where('contents_lang.lang_id', '=', app('activeLangDetails')['lang_id'])
  47. ->whereNotIn('type', ['news', 'weblog'])
  48. ->where('contents.status', 1)
  49. ->orderBy('contents.id', 'DESC')
  50. ->get();
  51. return view('backend.content.index', [
  52. 'contents' => $contents,
  53. 'locale' => $this->locale,
  54. 'type' => 'content'
  55. ]);
  56. }
  57. /**
  58. * Show the form for creating a new resource.
  59. *
  60. * @return Response
  61. */
  62. public function create()
  63. {
  64. $results = $this->category->getDataCategory(2);
  65. $roots = $results['roots'];
  66. $translatedName = $results['translatedLang'];
  67. return view('backend.content.form', [
  68. 'locale' => $this->locale,
  69. 'listLang' => $this->listLang,
  70. 'type' => 'content'
  71. ])->with('roots', $roots)
  72. ->with('translatedName', $translatedName);
  73. }
  74. /**
  75. * Store a newly created resource in storage.
  76. *
  77. * @return Response
  78. */
  79. public function store()
  80. {
  81. $input = Input::all();
  82. $input_content['url'] = $input['url'];
  83. $input['author_id'] = $input_content['employee_id'] = $input_content['author_id'] = Auth::employee()->get()->id ? Auth::employee()->get()->id : 0;
  84. $input_content['status'] = $input['status'];
  85. $input_content['type'] = $input['type'];
  86. $input_content['sort'] = isset($input['sort']) && $input['sort'] ? $input['sort'] : 1;
  87. $URL = isset($input['url']) && $input['url'] ? str_replace(" ", "-", $input['url']) : CategoryHelper::createAlias($input['link_title']);
  88. $input['url'] = $input_content['url'] = $URL;
  89. $input['category_id'] = isset($input['categories']) ? $input['categories'][0] : null;
  90. $fill = $this->content->fill($input_content);
  91. $valid = $fill->validationData($input, $this->content->rules);
  92. if (!$valid) {
  93. return redirect()->back()->withInput()->withErrors($this->content->errors);
  94. }
  95. $this->content->save();
  96. if ($id = $this->content->id) {
  97. $resCat = $this->content->saveContentCategories($id, $input['categories']);
  98. if (Request::hasFile('image_content')) {
  99. $extension = Input::file('image_content')->getClientOriginalExtension();
  100. $createFolder = base_path() . env('PUBLIC_PATH_URL', 'public') . '/uploads/images/image_content/' . $id . '/';
  101. if (!file_exists($createFolder)) {
  102. mkdir($createFolder, 0755);
  103. }
  104. if (File::exists(base_path() . env('PUBLIC_PATH_URL', 'public') . '/uploads/images/image_content/' . $id . '/' . $id . '-60x60.' . $extension)) {
  105. File::delete($id . '-60x60.' . $extension);
  106. }
  107. if (File::exists(base_path() . env('PUBLIC_PATH_URL', 'public') . '/uploads/images/image_content/' . $id . '/' . $id . '-160x80.' . $extension)) {
  108. File::delete($id . '-160x80.' . $extension);
  109. }
  110. if (File::exists(base_path() . env('PUBLIC_PATH_URL', 'public') . '/uploads/images/image_content/' . $id . '/' . $id . '-600x400.' . $extension)) {
  111. File::delete($id . '-600x400.' . $extension);
  112. }
  113. Image::make(Input::file('image_content'))->fit(60, 60)->save('uploads/images/image_content/' . $id . '/' . $id . '-60x60.' . $extension);
  114. Image::make(Input::file('image_content'))->fit(160, 80)->save('uploads/images/image_content/' . $id . '/' . $id . '-160x80.' . $extension);
  115. Image::make(Input::file('image_content'))->fit(600, 400)->save('uploads/images/image_content/' . $id . '/' . $id . '-600x400.' . $extension);
  116. $updateContent = $this->content->find($id);
  117. $updateContent->image_content = '/uploads/images/image_content/' . $id . '/';
  118. $updateContent->extension = $extension;
  119. $updateContent->save();
  120. $messages[] = ['level' => BaseModel::level_success, 'message' => Lang::get('backend/Content.messages.uploadFileSuccess')];
  121. }
  122. $input['contents_id'] = $id;
  123. foreach ($this->listLang as $key => $value) {
  124. $content_lang = new ContentLang();
  125. $lang = $this->language->getLanguage($key);
  126. $input['lang_id'] = $lang['lang_id'];
  127. $input['title_tags'] = CategoryHelper::createTags($input['link_title']);
  128. $input['tags_upd'] = array_unique(explode(",", $input['title_tags'] . ',' . $input['tags']));
  129. $input['tags'] = implode(",", $input['tags_upd']);
  130. if (!$content_lang->fill($input)->isValid()) {
  131. return redirect()->back()->withInput()->withErrors($content_lang->errors);
  132. }
  133. $content_lang->save();
  134. }
  135. $messages[] = ['level' => BaseModel::level_success, 'message' => Lang::get('backend/Content.messages.successInsert')];
  136. Session::flash('messages', $messages);
  137. return Redirect::route($this->locale . 'backend.content.index')->with('locale', $this->locale);
  138. }
  139. Flash::error(Lang::get('backend/Content.messages.errCreate'));
  140. return Redirect::back()->withErrors(Lang::get('backend/Content.messages.errCreate'));
  141. }
  142. /**
  143. * Display the specified resource.
  144. *
  145. * @param int $id
  146. * @return Response
  147. */
  148. public function show($id)
  149. {
  150. //
  151. }
  152. /**
  153. * Show the form for editing the specified resource.
  154. *
  155. * @param int $id
  156. * @return Response
  157. */
  158. public function edit($id)
  159. {
  160. $results = $this->category->getDataCategory(2);
  161. $roots = $results['roots'];
  162. $translatedName = $results['translatedLang'];
  163. $categoriesContent = $this->content->getContentCategories($id, true);
  164. $content = $this->content->where('id', $id)
  165. ->join('contents_lang', 'id', '=', 'contents_id')
  166. ->where('contents_lang.lang_id', '=', app('activeLangDetails')['lang_id'])
  167. ->first();
  168. $params=[];
  169. $params['ctrl']='content';
  170. $params['itemId']=$id;
  171. $params['type']='image';
  172. $images = $this->content->getMediaUpload($params);
  173. return view('backend.content.form', [
  174. 'images' => $images,
  175. 'content' => $content,
  176. 'categoriesContent' => $categoriesContent,
  177. 'locale' => $this->locale,
  178. 'listLang' => $this->listLang,
  179. 'type' => 'content'
  180. ])->with('roots', $roots)
  181. ->with('translatedName', $translatedName);
  182. }
  183. /**
  184. * Update the specified resource in storage.
  185. *
  186. * @param int $id
  187. * @return Response
  188. */
  189. public function update($id)
  190. {
  191. $input = Input::all();
  192. $lang = $this->language->getLanguage($input['lang']);
  193. $input['lang_id'] = $lang['lang_id'];
  194. $updateContent = $this->content->find($id);
  195. $URL = isset($input['url']) && $input['url'] ? str_replace(" ", "-", $input['url']) : CategoryHelper::createAlias($input['link_title']);
  196. $input['url'] = $URL;
  197. $input['title_tags'] = CategoryHelper::createTags($input['link_title']);
  198. $updateContent->url = $input['url'];
  199. $updateContent->type = $input['type'];
  200. $updateContent->sort = isset($input['sort']) && $input['sort'] ? $input['sort'] : 1;
  201. $input['tags_upd'] = array_unique(explode(",", $input['title_tags'] . ',' . $input['tags']));
  202. $paramsForTranslate = [
  203. 'contents_id' => $id,
  204. 'lang_id' => $input['lang_id'],
  205. 'link_title' => $input['link_title'],
  206. 'browser_title' => $input['browser_title'],
  207. 'body_title' => $input['body_title'],
  208. 'tags' => implode(",", $input['tags_upd']),
  209. 'short_content' => $input['short_content'],
  210. 'body_content' => $input['body_content']
  211. ];
  212. ContentLang::updateLang($paramsForTranslate);
  213. $resCat = $this->content->saveContentCategories($id, $input['categories']);
  214. if (Request::hasFile('image_content')) {
  215. $extension = Input::file('image_content')->getClientOriginalExtension();
  216. $createFolder = base_path() . env('PUBLIC_PATH_URL', 'public') . '/uploads/images/image_content/' . $id . '/';
  217. if (!file_exists($createFolder)) {
  218. mkdir($createFolder, 0755);
  219. }
  220. if (File::exists(base_path() . env('PUBLIC_PATH_URL', 'public') . '/uploads/images/image_content/' . $id . '/' . $id . '-60x60.' . $extension)) {
  221. File::delete($id . '-60x60.' . $extension);
  222. }
  223. if (File::exists(base_path() . env('PUBLIC_PATH_URL', 'public') . '/uploads/images/image_content/' . $id . '/' . $id . '-160x80.' . $extension)) {
  224. File::delete($id . '-160x80.' . $extension);
  225. }
  226. if (File::exists(base_path() . env('PUBLIC_PATH_URL', 'public') . '/uploads/images/image_content/' . $id . '/' . $id . '-600x400.' . $extension)) {
  227. File::delete($id . '-600x400.' . $extension);
  228. }
  229. Image::make(Input::file('image_content'))->fit(60, 60)->save('uploads/images/image_content/' . $id . '/' . $id . '-60x60.' . $extension);
  230. Image::make(Input::file('image_content'))->fit(160, 80)->save('uploads/images/image_content/' . $id . '/' . $id . '-160x80.' . $extension);
  231. Image::make(Input::file('image_content'))->fit(600, 400)->save('uploads/images/image_content/' . $id . '/' . $id . '-600x400.' . $extension);
  232. // $updateContent = $this->content->find($id);
  233. $updateContent->image_content = '/uploads/images/image_content/' . $id . '/';
  234. $updateContent->extension = $extension;
  235. $messages[] = ['level' => BaseModel::level_success, 'message' => Lang::get('backend/Content.messages.uploadFileSuccess')];
  236. }
  237. $updateContent->save();
  238. $messages[] = ['level' => BaseModel::level_success, 'message' => Lang::get('backend/Content.messages.successUpdate')];
  239. Session::flash('messages', $messages);
  240. return Redirect::back();
  241. }
  242. /**
  243. * Remove the specified resource from storage.
  244. *
  245. * @param int $id
  246. * @return Response
  247. */
  248. public function destroy($id)
  249. {
  250. $content = $this->content->find($id);
  251. // $content->status = 0;
  252. // $content->save();
  253. $content->delete();
  254. DB::table('contents_lang')->where('contents_id', $id)->delete();
  255. DB::table('content_categories')->where('content_id', $id)->delete();
  256. Flash::success(trans('backend/Content.messages.successDelete'));
  257. return Redirect::back();
  258. }
  259. }