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

/www/app/Modules/Gallery/AlbumBrowser/AlbumBrowser.php

https://github.com/bazo/Mokuji
PHP | 217 lines | 180 code | 22 blank | 15 comment | 3 complexity | 274c315ed8db662f4afc5abc6232fba4 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  1. <?php
  2. /**
  3. * AlbumBrowser
  4. *
  5. * @author Martin
  6. */
  7. class Gallery extends Control{
  8. //put your code here
  9. private $edit = null, $model;
  10. public $photoFolder;
  11. public $width =150, $height = 100;
  12. /**
  13. * @persistent
  14. */
  15. public $albumId = null;
  16. public function __construct(IComponentContainer $parent = NULL, $name = NULL) {
  17. parent::__construct($parent, $name);
  18. $this->model = new GalleryModuleModel();
  19. $this->photoFolder = APP_DIR.'/Data/Photos/';
  20. }
  21. protected function createTemplate()
  22. {
  23. $template = parent::createTemplate();
  24. $template->setFile(dirname(__FILE__) . '/albumbrowser.phtml');
  25. return $template;
  26. }
  27. public function handleBrowsePhotos($albumId)
  28. {
  29. $this->redirect('this', array('albumId' => $albumId));
  30. }
  31. public function handleAddPhoto()
  32. {
  33. $this->edit = true;
  34. $this->template->browser = 'photos';
  35. $this->invalidateControl('popup');
  36. }
  37. public function handleNewAlbum()
  38. {
  39. $this->edit = true;
  40. $this->template->browser = 'albums';
  41. $this->invalidateControl();
  42. }
  43. public function createComponentAlbumForm($name)
  44. {
  45. $form = new LiveForm($this, $name);
  46. $form->addText('name', 'Name')->addRule(Form::FILLED, 'Please enter a album name');
  47. $form->addTextArea('description', 'Description');
  48. $form->addButton('btnClose', 'Close');
  49. $form->addSubmit('btnSubmit', 'OK')->onClick[] = callback($this, 'AlbumFormSubmitted');
  50. return $form;
  51. }
  52. public function AlbumFormSubmitted($button)
  53. {
  54. $values = $button->getForm()->getValues();
  55. unset($values['btnClose']);
  56. try{
  57. $this->model->createAlbum($values);
  58. $this->presenter->flash('Album '.$values['name'].' created');
  59. $this->invalidateControl('albums');
  60. }
  61. catch (DibiDriverException $e)
  62. {
  63. $this->presenter->flash($e->getMessage());
  64. }
  65. }
  66. public function createComponentPhotoForm($name)
  67. {
  68. $form = new LiveForm($this, $name);
  69. $form->getElementPrototype()->class = 'ajax_uploader';
  70. $form->addFile('photo', 'Photo')->getControlPrototype()->class('multi');
  71. $form->addText('name', 'Name')->addRule(Form::FILLED, 'Please enter a album name');
  72. $form->addTextArea('description', 'Description');
  73. $form->addButton('btnClose', 'Close');
  74. $form->addSubmit('btnSubmit', 'OK')->onClick[] = callback($this, 'PhotoFormSubmitted');
  75. $form->addHidden('album_id')->setValue($this->getParam('albumId'));
  76. return $form;
  77. }
  78. public function PhotoFormSubmitted($button)
  79. {
  80. $values = $button->getForm()->getValues();
  81. unset($values['btnClose']);
  82. try{
  83. $file = $values['photo'];
  84. if($file->isOK())
  85. {
  86. $file->move($this->photoFolder.$file->getName());
  87. unset($values['photo']);
  88. $values['file'] = $file->getName();
  89. $this->model->savePhotos($values);
  90. }
  91. $this->presenter->flash('Photo '.$values['name'].'uploaded');
  92. $this->invalidateControl();
  93. $this->redirect('this');
  94. }
  95. catch(InvalidStateException $e)
  96. {
  97. $this->presenter->flash($e->getMessage());
  98. }
  99. catch (DibiDriverException $e)
  100. {
  101. $this->presenter->flash($e->getMessage());
  102. }
  103. }
  104. public function createComponentCss($name)
  105. {
  106. $css = parent::createComponentCss($name);
  107. $css->sourcePath = dirname(__FILE__).'/../css' ;
  108. return $css;
  109. }
  110. public function createComponentJs($name)
  111. {
  112. $js = parent::createComponentJs($name);
  113. $js->sourcePath = dirname(__FILE__).'/../js' ;
  114. return $js;
  115. }
  116. public function handleOutputPhoto($file)
  117. {
  118. $image = Image::fromFile($this->photoFolder.$file);
  119. $image->resize($this->width, $this->height);
  120. $image->sharpen();
  121. $image->send();
  122. }
  123. public function handleOutputCoverPhoto($file)
  124. {
  125. $image = Image::fromFile($this->photoFolder.$file);
  126. $image->resize(75, 50);
  127. $image->sharpen();
  128. $image->send();
  129. exit;
  130. }
  131. /*
  132. * used for frontend album photos rendering
  133. */
  134. public function renderPhotos($album_name)
  135. {
  136. $this->template->photos = $this->model->getPhotosByAlbumName($album_name);
  137. $this->template->setFile(dirname(__FILE__) . '/frontphotos.phtml');
  138. return $this->template->render();
  139. }
  140. /*
  141. * used for frontend albums rendering
  142. */
  143. public function renderAlbums()
  144. {
  145. $params = $this->getParam();
  146. if(isset($params['albumId']))
  147. {
  148. $album_name = $this->model->getAlbumName($params['albumId']);
  149. return $this->renderPhotos($album_name['name']);
  150. }
  151. $albums = $this->model->getAlbums();
  152. foreach($albums as $index => $album)
  153. {
  154. $albums[$index]['cover_photos'] = $this->model->getCoverPhotos($album->id, 4);
  155. }
  156. $this->template->albums = $albums;
  157. $this->template->setFile(dirname(__FILE__) . '/frontalbums.phtml');
  158. return $this->template->render();
  159. }
  160. public function render()
  161. {
  162. $params = $this->getParam();
  163. $this->template->edit = $this->edit;
  164. if(isset($params['albumId']))
  165. {
  166. $this->template->browser = 'photos';
  167. $album_id = $params['albumId'];
  168. $this->template->link = $this->createAddPhotoLink();
  169. $this->template->photos = $this->model->getPhotos($album_id);
  170. }
  171. else
  172. {
  173. $this->template->browser = 'albums';
  174. $this->template->link = $this->createAddAlbumLink();
  175. $albums = $this->model->getAlbums();
  176. foreach($albums as $index => $album)
  177. {
  178. $albums[$index]['cover_photos'] = $this->model->getCoverPhotos($album->id, 4);
  179. }
  180. $this->template->albums = $albums;
  181. }
  182. return $this->template->render();
  183. }
  184. public function createAddAlbumLink()
  185. {
  186. $link = Html::el('a')->href($this->link('newAlbum!'))->class('ajax')->add(Html::el('div')->id('create-new-link')->add(Html::el('div')->id('icon'))->add(Html::el('span')->add('Create new album')));
  187. return $link;
  188. }
  189. public function createAddPhotoLink()
  190. {
  191. $link = Html::el('a')->href($this->link('addPhoto!'))->class('ajax')->add(Html::el('div')->id('create-new-link')->add(Html::el('div')->id('icon'))->add(Html::el('span')->add('Add photo')));
  192. return $link;
  193. }
  194. }
  195. ?>