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

/controllers/PurchasesController.php

http://github.com/masom/Chowly
PHP | 202 lines | 150 code | 37 blank | 15 comment | 11 complexity | 16bd64ad1308b9c84e404cbc8099b8c3 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0
  1. <?php
  2. /**
  3. * Chowly Pick. Eat. Save!
  4. *
  5. * @copyright Copyright 2011, Martin Samson <pyrolian@gmail.com>
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace chowly\controllers;
  9. use chowly\models\Purchases;
  10. use chowly\models\Offers;
  11. use chowly\models\Venues;
  12. use chowly\extensions\Utils;
  13. use li3_flash_message\extensions\storage\FlashMessage;
  14. class PurchasesController extends \chowly\extensions\action\Controller{
  15. public function admin_index(){
  16. $limit = 20;
  17. $page = ($this->request->page) ?: 1;
  18. $order = array('created' => 'DESC');
  19. $total = Purchases::count();
  20. $purchases = Purchases::all(compact('order','limit','page'));
  21. return compact('purchases', 'total', 'page', 'limit');
  22. }
  23. public function admin_view(){
  24. $conditions = array('_id' => $this->request->id);
  25. $purchase = Purchases::first(compact('conditions'));
  26. if (!$purchase){
  27. FlashMessage::set("The specified purchase could not be found.");
  28. return $this->redirect($this->request->referer());
  29. }
  30. $conditions = array('_id'=>array());
  31. foreach ($purchase->offers as $offer){
  32. $conditions['_id'][] = $offer->_id;
  33. }
  34. $offers = Offers::all(compact('conditions'));
  35. $conditions = array('_id'=>array());
  36. foreach ($offers as $offer){
  37. $conditions['_id'][] = $offer->venue_id;
  38. }
  39. $venues = Venues::all(compact('conditions'));
  40. return compact('purchase','offers','venues');
  41. }
  42. public function admin_search(){
  43. switch ($this->request->data['search']['type']){
  44. case "email":
  45. $conditions = array('email' => $this->request->data['search']['value']);
  46. break;
  47. case "name":
  48. $conditions = array('name' => $this->request->data['search']['value']);
  49. break;
  50. default:
  51. FlashMessage::set("Invalid search parameters");
  52. return $this->redirect(array('Purchases::index','admin'=>true));
  53. }
  54. $order = array('created' => 'DESC');
  55. $purchases = Purchases::all(compact('order','conditions'));
  56. $this->_render['template'] = 'admin_index';
  57. return compact('purchases');
  58. }
  59. public function admin_download(){
  60. if (!$this->request->id){
  61. FlashMessage::set("Missing download details.");
  62. return $this->redirect(array('Purchases::index'));
  63. }
  64. $conditions = array('_id' => $this->request->id);
  65. $purchase = Purchases::first(compact('conditions'));
  66. if (!$purchase){
  67. FlashMessage::set("The purchase could not be found");
  68. return $this->redirect(array('Purchases::index'));
  69. }
  70. $offers = $purchase->offers;
  71. $conditions = array('_id'=>array());
  72. foreach ($offers as $offer){
  73. $conditions['_id'][] = $offer->venue_id;
  74. }
  75. $venues = Venues::all(compact('conditions'));
  76. $filename = $purchase->_id . '.pdf';
  77. $this->_view['renderer'] = 'Pdf';
  78. $this->_render['template'] = 'purchase';
  79. return compact('purchase','venues', 'offers','filename');
  80. }
  81. /**
  82. * Resend a purchase
  83. */
  84. public function admin_resend(){
  85. $conditions = array('_id' => $this->request->id);
  86. $purchase = Purchases::first(compact('conditions'));
  87. if (!$purchase){
  88. FlashMessage::set('The specified purchase could not be found in the database.');
  89. return $this->redirect(array('Purchases::index'));
  90. }
  91. $offers = $purchase->offers;
  92. $conditions = array('_id'=>array());
  93. foreach ($offers as $offer){
  94. $conditions['_id'][] = $offer->venue_id;
  95. }
  96. $venues = Venues::all(compact('conditions'));
  97. $pdfPath = null;
  98. try{
  99. $pdfPath = Utils::getPdf($purchase, $offers, $venues);
  100. }catch(\Exception $e){
  101. FlashMessage::set("An error occured while generating the PDF: {$e->getMessage()}");
  102. return $this->redirect(array('Purchases::view', 'id'=>$purchase->_id));
  103. }
  104. if (!$pdfPath){
  105. FlashMessage::set("Unable to retreive PDF");
  106. return $this->redirect(array('Purchases::view', 'id'=>$purchase->_id));
  107. }
  108. $to = $purchase->email;
  109. $transport = Swift_MailTransport::newInstance();
  110. $mailer = Swift_Mailer::newInstance($transport);
  111. $message = Swift_Message::newInstance();
  112. $message->setSubject("Chowly Purchase Confirmation");
  113. $message->setFrom(array('purchases@chowly.com' => 'Chowly'));
  114. $message->setTo($to);
  115. $message->setBody(parent::_getEmail(compact('purchase'), 'purchase', 'purchases'));
  116. $message->attach(Swift_Attachment::fromPath($pdfPath));
  117. if ($mailer->send($message)){
  118. FlashMessage::set("Email re-sent");
  119. }else{
  120. FlashMessage::set("The email could not be re-sent.");
  121. }
  122. return $this->redirect(array('Purchases::view', 'id'=>$purchase->_id));
  123. }
  124. public function download(){
  125. if (!$this->request->id){
  126. FlashMessage::set("Missing download details.");
  127. return $this->redirect(array('Offers::index'));
  128. }
  129. $conditions = array('_id' => $this->request->id);
  130. $purchase = Purchases::first(compact('conditions'));
  131. if (!$purchase){
  132. FlashMessage::set("The purchase could not be found");
  133. return $this->redirect($this->request->referer());
  134. }
  135. /*if ($purchase->downloaded){
  136. $message = "The purchase has already been downloaded.";
  137. $message .= " Contact Chowly support to re-download.";
  138. FlashMessage::set($message);
  139. return $this->redirect(array('Offers::index'));
  140. }*/
  141. $options = array('multiple' => false, 'safe' => false, 'upsert'=>false);
  142. $data = array('$set'=>array('downloaded'=>true));
  143. $conditions = array('_id' => $purchase->_id);
  144. Purchases::update($data, $conditions, $options);
  145. $path = Purchases::pdfPath() . DIRECTORY_SEPARATOR . $purchase->_id . '.pdf';
  146. if (file_exists($path)){
  147. $this->_render['auto'] = false;
  148. $this->response->headers('Content-Type', 'application/pdf');
  149. $this->response->headers('download', $purchase->_id . '.pdf');
  150. $this->response->body = file_get_contents($path);
  151. return;
  152. }
  153. $offers = $purchase->offers;
  154. $conditions = array('_id'=>array());
  155. foreach ($offers as $offer){
  156. $conditions['_id'][] = $offer->venue_id;
  157. }
  158. $venues = Venues::all(compact('conditions'));
  159. $filename = $purchase->_id . '.pdf';
  160. $this->_view['renderer'] = 'Pdf';
  161. $this->_render['template'] = 'purchase';
  162. return compact('purchase','venues', 'offers','filename');
  163. }
  164. }
  165. ?>