PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/app/controllers/Member/AdvertsController.php

https://gitlab.com/dwalker109/carbon-and-classic
PHP | 368 lines | 193 code | 57 blank | 118 comment | 18 complexity | 4778fd314d97fca5a6d0f5196b9df881 MD5 | raw file
  1. <?php
  2. namespace Member;
  3. // Domain
  4. use Advert;
  5. use Category;
  6. use Manufacturer;
  7. use Country;
  8. use PostageOption;
  9. use Confide;
  10. use Offer;
  11. // Core
  12. use Input;
  13. use Redirect;
  14. use View;
  15. use Session;
  16. use URL;
  17. use Request;
  18. use Response;
  19. use Carbon\Carbon;
  20. // Third Party
  21. use Notification;
  22. class AdvertsController extends \BaseController
  23. {
  24. /**
  25. * Register filters
  26. */
  27. public function __construct()
  28. {
  29. $this->beforeFilter('extended_profile_holder', ['except' => 'index', 'delete']);
  30. }
  31. /**
  32. * Display a listing of the resource.
  33. *
  34. * @return Response
  35. */
  36. public function index()
  37. {
  38. $adverts = \Collection::make([
  39. 'not_completed' => Advert::ownedBySelf()->notCompleted()->get(),
  40. 'completed' => Advert::ownedBySelf()->completed()->get(),
  41. ]);
  42. return View::make('member.listings.index', compact('adverts'));
  43. }
  44. /**
  45. * Show the form for creating a new resource.
  46. *
  47. * @return Response
  48. */
  49. public function create()
  50. {
  51. $advert = new Advert();
  52. $rules = $advert->getRules();
  53. $categories = Category::getFullHierachyNestedList();
  54. $manufacturers = Manufacturer::lists('name', 'id');
  55. return View::make(
  56. 'member.listings.create',
  57. compact('advert', 'rules', 'categories', 'manufacturers')
  58. );
  59. }
  60. /**
  61. * Store a newly created resource in storage.
  62. *
  63. * @return Response
  64. */
  65. public function store()
  66. {
  67. return $this->handleSave(new Advert);
  68. }
  69. /**
  70. * Show the form for editing the specified resource.
  71. *
  72. * @param Advert $advert
  73. * @return Response
  74. */
  75. public function edit(Advert $advert)
  76. {
  77. $rules = $advert->getRules();
  78. $categories = Category::getFullHierachyNestedList();
  79. $manufacturers = Manufacturer::lists('name', 'id');
  80. return View::make(
  81. 'member.listings.edit',
  82. compact('advert', 'rules', 'categories', 'manufacturers')
  83. );
  84. }
  85. /**
  86. * Update the specified resource in storage.
  87. *
  88. * @param Advert $advert
  89. * @return Response
  90. */
  91. public function update(Advert $advert)
  92. {
  93. return $this->handleSave($advert);
  94. }
  95. /**
  96. * Remove the specified resource from storage.
  97. *
  98. * @param Advert $advert
  99. * @return Response
  100. */
  101. public function destroy(Advert $advert)
  102. {
  103. $advert->delete();
  104. return Redirect::route('member.listings.index');
  105. }
  106. /**
  107. * Called from the store and update methods to validate/save/redirect
  108. * @param Advert $advert
  109. * @return Response
  110. */
  111. private function handleSave($advert)
  112. {
  113. // Fill the $advert (excluding non member editable fields),
  114. $advert->fill(
  115. Input::except(
  116. 'user_id',
  117. 'approval_status',
  118. 'purchased_at',
  119. 'reserved_at',
  120. 'shipped_at'
  121. )
  122. );
  123. // Set the user manually
  124. $advert->user_id = Confide::user()->id;
  125. if ($advert->save()) {
  126. Notification::success('Advert saved');
  127. return Redirect::route('member.listings.interstitial', $advert->id);
  128. } else {
  129. return Redirect::back()->withErrors($advert->getErrors())->withInput();
  130. }
  131. }
  132. ///////////////////
  133. // Extra methods //
  134. ///////////////////
  135. /**
  136. * Display a cut down preview version of the frontend page
  137. *
  138. * @param Advert $advert
  139. * @return Response
  140. */
  141. public function preview(Advert $advert)
  142. {
  143. return View::make('public.advert.preview', compact('advert'));
  144. }
  145. /**
  146. * Display full summary details of the resource
  147. *
  148. * @param Advert $advert
  149. * @return Response
  150. */
  151. public function summary(Advert $advert)
  152. {
  153. return View::make('member.listings.summary', compact('advert'));
  154. }
  155. /**
  156. * Present some "what next" options to the member, normally advert is draft at this point
  157. *
  158. * @param Advert $advert
  159. * @return Response
  160. */
  161. public function interstitial(Advert $advert)
  162. {
  163. return View::make('member.listings.interstitial', compact('advert'));
  164. }
  165. /**
  166. * Send completed listing to admins to be approved
  167. * @param Advert $advert
  168. * @return Response
  169. */
  170. public function submitForApproval(Advert $advert)
  171. {
  172. $advert->makeDraft();
  173. $advert->submitForApproval();
  174. return View::make('member.listings.finished', compact('advert'));
  175. }
  176. /**
  177. * Show all active selling items (or a subset for Ajax requests)
  178. *
  179. * @return Response
  180. */
  181. public function allActiveSelling()
  182. {
  183. if (Request::ajax()) {
  184. $adverts = Advert::ownedBySelf()->notCompleted()->forAjax()->get();
  185. $view = $adverts->isEmpty() ? 'member.dashboard.no-data' : 'member.listings._table';
  186. return View::make($view, compact('adverts'));
  187. } else {
  188. $adverts = Advert::ownedBySelf()->notCompleted()->get();
  189. return View::make('member.listings.active', compact('adverts'));
  190. }
  191. }
  192. /**
  193. * Show all recently sold items (or a subset for Ajax requests)
  194. *
  195. * @return Response
  196. */
  197. public function recentlySold()
  198. {
  199. if (Request::ajax()) {
  200. $adverts = Advert::ownedBySelf()->completed()->recentlyModified()->forAjax()->get();
  201. $view = $adverts->isEmpty() ? 'member.dashboard.no-data' : 'member.listings._table';
  202. return View::make($view, compact('adverts'));
  203. } else {
  204. $adverts = Advert::ownedBySelf()->completed()->recentlyModified()->get();
  205. return View::make('member.listings.recently-sold', compact('adverts'));
  206. }
  207. }
  208. /**
  209. * Show an edit form for managing postage options
  210. *
  211. * @param Advert $advert
  212. * @return Response
  213. */
  214. public function editPostage(Advert $advert)
  215. {
  216. // Set up Redirect::intended() for use during a cancel click
  217. if (URL::current() != URL::previous()) {
  218. Session::put('url.intended', URL::previous());
  219. }
  220. $regions = new \Collection;
  221. // Get all regions and add a placeholder PostageOption model
  222. foreach (Country::getRegions() as $region_name) {
  223. $placeholder = new PostageOption();
  224. $regions->put($region_name, $placeholder);
  225. }
  226. // Iterate existing PostageOptions and replace the placeholders
  227. foreach ($advert->postage as $postage_option) {
  228. $postage_option->enabled = true;
  229. $regions->put($postage_option->region_name, $postage_option);
  230. }
  231. return View::make('member.listings.postage', compact('advert', 'regions'));
  232. }
  233. /**
  234. * Save postage updates
  235. *
  236. * @param Advert $advert
  237. * @return Response
  238. */
  239. public function updatePostage(Advert $advert)
  240. {
  241. // Build a new model for each delivery option from the form
  242. $postage_options = new \Collection;
  243. foreach (Input::get('postage_options') as $form_input) {
  244. if (isset($form_input['enabled'])) {
  245. $model = new PostageOption;
  246. // Fill the remaining date and roundtrip the checkbox
  247. $model->fill($form_input + ['enabled' => true]);
  248. // Collect in person is a special case - ensure it has not been tempered with
  249. if ($form_input['region_name'] == PostageOption::COLLECT_IN_PERSON) {
  250. $model->fill(PostageOption::$collect_in_person_prototype);
  251. }
  252. // Add to the options collection
  253. $postage_options->push($model);
  254. }
  255. }
  256. // Ensure at least one option was selected
  257. if ($postage_options->isEmpty()) {
  258. Notification::error('Please complete at least one postage option');
  259. return Redirect::back()->withInput();
  260. }
  261. // Check each is valid, collect up errors if any fail
  262. $postage_errors = new \Collection;
  263. foreach ($postage_options as $postage_option) {
  264. if ($postage_option->isInvalid()) {
  265. $postage_errors->put($postage_option->region_name, $postage_option->getErrors());
  266. }
  267. }
  268. // Flash errors and return to form if any errors occurred
  269. if (! $postage_errors->isEmpty()) {
  270. \Session::flash('postage_errors', $postage_errors);
  271. return Redirect::back()->withInput();
  272. }
  273. // No errors, clear out old shipping and save new
  274. $advert->postage()->delete();
  275. $advert->postage()->saveMany($postage_options->all());
  276. // Return
  277. Notification::success('Postage options have been updated');
  278. return Redirect::route('member.listings.interstitial', ['id' => $advert->id]);
  279. }
  280. /**
  281. * Set shipped status
  282. *
  283. * @param Advert $advert
  284. * @return Response
  285. */
  286. public function shipped(Advert $advert)
  287. {
  288. if ($advert->markShipped()) {
  289. Notification::success('Shipping status has been updated');
  290. } else {
  291. Notification::error('Shipping status cannot be updated');
  292. }
  293. return Redirect::back();
  294. }
  295. /**
  296. * Cancel a non-paid for sale and return to listings
  297. *
  298. * @param Advert $advert
  299. * @return Response
  300. */
  301. public function cancel(Advert $advert)
  302. {
  303. if ($advert->cancelPendingSale()) {
  304. Notification::success('Reservation has been cancelled and the item returned to sale');
  305. return Redirect::back();
  306. } else {
  307. Notification::error('This item cannot be returned to sale');
  308. return Redirect::back();
  309. }
  310. }
  311. }