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

/application/modules/Album/controllers/AlbumController.php

https://github.com/grandison/budo16
PHP | 454 lines | 398 code | 29 blank | 27 comment | 27 complexity | 8149ca484ab92a1236b5b71f522a056b MD5 | raw file
  1. <?php
  2. /**
  3. * SocialEngine
  4. *
  5. * @category Application_Extensions
  6. * @package Album
  7. * @copyright Copyright 2006-2010 Webligo Developments
  8. * @license http://www.socialengine.net/license/
  9. * @version $Id: AlbumController.php 8381 2011-02-02 05:03:02Z john $
  10. * @author John Boehr <j@webligo.com>
  11. */
  12. /**
  13. * @category Application_Extensions
  14. * @package Album
  15. * @copyright Copyright 2006-2010 Webligo Developments
  16. * @license http://www.socialengine.net/license/
  17. */
  18. class Album_AlbumController extends Core_Controller_Action_Standard
  19. {
  20. public function init()
  21. {
  22. if( !$this->_helper->requireAuth()->setAuthParams('album', null, 'view')->isValid() ) return;
  23. if( 0 !== ($photo_id = (int) $this->_getParam('photo_id')) &&
  24. null !== ($photo = Engine_Api::_()->getItem('album_photo', $photo_id)) )
  25. {
  26. Engine_Api::_()->core()->setSubject($photo);
  27. }
  28. else if( 0 !== ($album_id = (int) $this->_getParam('album_id')) &&
  29. null !== ($album = Engine_Api::_()->getItem('album', $album_id)) )
  30. {
  31. Engine_Api::_()->core()->setSubject($album);
  32. }
  33. }
  34. public function editAction()
  35. {
  36. if( !$this->_helper->requireUser()->isValid() ) return;
  37. if( !$this->_helper->requireSubject('album')->isValid() ) return;
  38. if( !$this->_helper->requireAuth()->setAuthParams(null, null, 'edit')->isValid() ) return;
  39. // Get navigation
  40. $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
  41. ->getNavigation('album_main');
  42. // Hack navigation
  43. foreach( $navigation->getPages() as $page )
  44. {
  45. if( $page->route != 'album_general' || $page->action != 'manage' ) continue;
  46. $page->active = true;
  47. }
  48. // Prepare data
  49. $this->view->album = $album = Engine_Api::_()->core()->getSubject();
  50. // Make form
  51. $this->view->form = $form = new Album_Form_Album_Edit();
  52. if( !$this->getRequest()->isPost() )
  53. {
  54. $form->populate($album->toArray());
  55. $auth = Engine_Api::_()->authorization()->context;
  56. $roles = array('owner', 'owner_member', 'owner_member_member', 'owner_network', 'registered', 'everyone');
  57. foreach( $roles as $role ) {
  58. if( 1 === $auth->isAllowed($album, $role, 'view') ) {
  59. $form->auth_view->setValue($role);
  60. }
  61. if( 1 === $auth->isAllowed($album, $role, 'comment') ) {
  62. $form->auth_comment->setValue($role);
  63. }
  64. if( 1 === $auth->isAllowed($album, $role, 'tag') ) {
  65. $form->auth_tag->setValue($role);
  66. }
  67. }
  68. $this->view->status = false;
  69. $this->view->error = Zend_Registry::get('Zend_Translate')->_('Invalid request method');
  70. return;
  71. }
  72. if( !$form->isValid($this->getRequest()->getPost()) )
  73. {
  74. $this->view->status = false;
  75. $this->view->error = Zend_Registry::get('Zend_Translate')->_('Invalid data');
  76. return;
  77. }
  78. // Process
  79. $db = $album->getTable()->getAdapter();
  80. $db->beginTransaction();
  81. try
  82. {
  83. $values = $form->getValues();
  84. $album->setFromArray($values);
  85. $album->save();
  86. // CREATE AUTH STUFF HERE
  87. $auth = Engine_Api::_()->authorization()->context;
  88. $roles = array('owner', 'owner_member', 'owner_member_member', 'owner_network', 'registered', 'everyone');
  89. if( empty($values['auth_view']) ) {
  90. $values['auth_view'] = key($form->auth_view->options);
  91. if( empty($values['auth_view']) ) {
  92. $values['auth_view'] = 'everyone';
  93. }
  94. }
  95. if( empty($values['auth_comment']) ) {
  96. $values['auth_comment'] = key($form->auth_comment->options);
  97. if( empty($values['auth_comment']) ) {
  98. $values['auth_comment'] = 'owner_member';
  99. }
  100. }
  101. if( empty($values['auth_tag']) ) {
  102. $values['auth_tag'] = key($form->auth_tag->options);
  103. if( empty($values['auth_tag']) ) {
  104. $values['auth_tag'] = 'owner_member';
  105. }
  106. }
  107. $viewMax = array_search($values['auth_view'], $roles);
  108. $commentMax = array_search($values['auth_comment'], $roles);
  109. $tagMax = array_search($values['auth_tag'], $roles);
  110. foreach( $roles as $i => $role ) {
  111. $auth->setAllowed($album, $role, 'view', ($i <= $viewMax));
  112. $auth->setAllowed($album, $role, 'comment', ($i <= $commentMax));
  113. $auth->setAllowed($album, $role, 'tag', ($i <= $tagMax));
  114. }
  115. $db->commit();
  116. }
  117. catch( Exception $e )
  118. {
  119. $db->rollBack();
  120. throw $e;
  121. }
  122. $db->beginTransaction();
  123. try {
  124. // Rebuild privacy
  125. $actionTable = Engine_Api::_()->getDbtable('actions', 'activity');
  126. foreach( $actionTable->getActionsByObject($album) as $action ) {
  127. $actionTable->resetActivityBindings($action);
  128. }
  129. $db->commit();
  130. } catch( Exception $e ) {
  131. $db->rollBack();
  132. throw $e;
  133. }
  134. return $this->_helper->redirector->gotoRoute(array('action' => 'manage'), 'album_general', true);
  135. }
  136. public function viewAction()
  137. {
  138. $settings = Engine_Api::_()->getApi('settings', 'core');
  139. if( !$this->_helper->requireSubject('album')->isValid() ) return;
  140. $this->view->album = $album = Engine_Api::_()->core()->getSubject();
  141. if( !$this->_helper->requireAuth()->setAuthParams($album, null, 'view')->isValid() ) return;
  142. // Prepare params
  143. $this->view->page = $page = $this->_getParam('page');
  144. // Prepare data
  145. $this->view->paginator = $paginator = $album->getCollectiblesPaginator();
  146. $paginator->setItemCountPerPage($settings->getSetting('album_page', 25));
  147. $paginator->setCurrentPageNumber($page);
  148. // Do other stuff
  149. $this->view->mine = true;
  150. $this->view->canEdit = $this->_helper->requireAuth()->setAuthParams($album, null, 'edit')->checkRequire();
  151. if( !$album->getOwner()->isSelf(Engine_Api::_()->user()->getViewer()) )
  152. {
  153. $album->view_count++;
  154. $album->save();
  155. $this->view->mine = false;
  156. }
  157. }
  158. public function deleteAction()
  159. {
  160. $viewer = Engine_Api::_()->user()->getViewer();
  161. $album = Engine_Api::_()->getItem('album', $this->getRequest()->getParam('album_id'));
  162. if( !$this->_helper->requireAuth()->setAuthParams($album, null, 'delete')->isValid()) return;
  163. // In smoothbox
  164. $this->_helper->layout->setLayout('default-simple');
  165. $this->view->form = $form = new Album_Form_Album_Delete();
  166. if( !$album )
  167. {
  168. $this->view->status = false;
  169. $this->view->error = Zend_Registry::get('Zend_Translate')->_("Album doesn't exists or not authorized to delete");
  170. return;
  171. }
  172. if( !$this->getRequest()->isPost() )
  173. {
  174. $this->view->status = false;
  175. $this->view->error = Zend_Registry::get('Zend_Translate')->_('Invalid request method');
  176. return;
  177. }
  178. $db = $album->getTable()->getAdapter();
  179. $db->beginTransaction();
  180. try
  181. {
  182. $album->delete();
  183. $db->commit();
  184. }
  185. catch( Exception $e )
  186. {
  187. $db->rollBack();
  188. throw $e;
  189. }
  190. $this->view->status = true;
  191. $this->view->message = Zend_Registry::get('Zend_Translate')->_('Album has been deleted.');
  192. return $this->_forward('success' ,'utility', 'core', array(
  193. 'parentRedirect' => Zend_Controller_Front::getInstance()->getRouter()->assemble(array('action' => 'manage'), 'album_general', true),
  194. 'messages' => Array($this->view->message)
  195. ));
  196. }
  197. public function editphotosAction()
  198. {
  199. if( !$this->_helper->requireUser()->isValid() ) return;
  200. if( !$this->_helper->requireSubject('album')->isValid() ) return;
  201. if( !$this->_helper->requireAuth()->setAuthParams(null, null, 'edit')->isValid() ) return;
  202. // Get navigation
  203. $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
  204. ->getNavigation('album_main');
  205. // Hack navigation
  206. foreach( $navigation->getPages() as $page )
  207. {
  208. if( $page->route != 'album_general' || $page->action != 'manage' ) continue;
  209. $page->active = true;
  210. }
  211. // Prepare data
  212. $this->view->album = $album = Engine_Api::_()->core()->getSubject();
  213. $this->view->paginator = $paginator = $album->getCollectiblesPaginator();
  214. $paginator->setCurrentPageNumber($this->_getParam('page'));
  215. $paginator->setItemCountPerPage(10);
  216. // Make form
  217. $this->view->form = $form = new Album_Form_Album_Photos();
  218. foreach( $paginator as $photo ) {
  219. $subform = new Album_Form_Album_EditPhoto(array('elementsBelongTo' => $photo->getGuid()));
  220. $subform->populate($photo->toArray());
  221. $form->addSubForm($subform, $photo->getGuid());
  222. $form->cover->addMultiOption($photo->getIdentity(), $photo->getIdentity());
  223. }
  224. if( !$this->getRequest()->isPost() ) {
  225. return;
  226. }
  227. if( !$form->isValid($this->getRequest()->getPost()) ) {
  228. return;
  229. }
  230. $table = $album->getTable();
  231. $db = $table->getAdapter();
  232. $db->beginTransaction();
  233. try
  234. {
  235. $values = $form->getValues();
  236. if( !empty($values['cover']) ) {
  237. $album->photo_id = $values['cover'];
  238. $album->save();
  239. }
  240. // Process
  241. foreach( $paginator as $photo )
  242. {
  243. $subform = $form->getSubForm($photo->getGuid());
  244. $values = $subform->getValues();
  245. $values = $values[$photo->getGuid()];
  246. unset($values['photo_id']);
  247. if( isset($values['delete']) && $values['delete'] == '1' )
  248. {
  249. $photo->delete();
  250. }
  251. else
  252. {
  253. $photo->setFromArray($values);
  254. $photo->save();
  255. }
  256. }
  257. $db->commit();
  258. }
  259. catch( Exception $e )
  260. {
  261. $db->rollBack();
  262. throw $e;
  263. }
  264. return $this->_helper->redirector->gotoRoute(array('action' => 'view', 'album_id' => $album->album_id), 'album_specific', true);
  265. }
  266. public function orderAction()
  267. {
  268. if( !$this->_helper->requireUser()->isValid() ) return;
  269. if( !$this->_helper->requireSubject('album')->isValid() ) return;
  270. if( !$this->_helper->requireAuth()->setAuthParams(null, null, 'edit')->isValid() ) return;
  271. $album = Engine_Api::_()->core()->getSubject();
  272. $order = $this->_getParam('order');
  273. if( !$order ) {
  274. $this->view->status = false;
  275. return;
  276. }
  277. // Get a list of all photos in this album, by order
  278. $photoTable = Engine_Api::_()->getItemTable('album_photo');
  279. $currentOrder = $photoTable->select()
  280. ->from($photoTable, 'photo_id')
  281. ->where('collection_id = ?', $album->getIdentity())
  282. ->order('order ASC')
  283. ->query()
  284. ->fetchAll(Zend_Db::FETCH_COLUMN)
  285. ;
  286. // Find the starting point?
  287. $start = null;
  288. $end = null;
  289. for( $i = 0, $l = count($currentOrder); $i < $l; $i++ ) {
  290. if( in_array($currentOrder[$i], $order) ) {
  291. $start = $i;
  292. $end = $i + count($order);
  293. break;
  294. }
  295. }
  296. if( null === $start || null === $end ) {
  297. $this->view->status = false;
  298. return;
  299. }
  300. for( $i = 0, $l = count($currentOrder); $i < $l; $i++ ) {
  301. if( $i >= $start && $i <= $end ) {
  302. $photo_id = $order[$i - $start];
  303. } else {
  304. $photo_id = $currentOrder[$i];
  305. }
  306. $photoTable->update(array(
  307. 'order' => $i,
  308. ), array(
  309. 'photo_id = ?' => $photo_id,
  310. ));
  311. }
  312. $this->view->status = true;
  313. }
  314. public function composeUploadAction()
  315. {
  316. if( !Engine_Api::_()->user()->getViewer()->getIdentity() )
  317. {
  318. $this->_redirect('login');
  319. return;
  320. }
  321. if( !$this->getRequest()->isPost() )
  322. {
  323. $this->view->status = false;
  324. $this->view->error = Zend_Registry::get('Zend_Translate')->_('Invalid method');
  325. return;
  326. }
  327. if( empty($_FILES['Filedata']) )
  328. {
  329. $this->view->status = false;
  330. $this->view->error = Zend_Registry::get('Zend_Translate')->_('Invalid data');
  331. return;
  332. }
  333. // Get album
  334. $viewer = Engine_Api::_()->user()->getViewer();
  335. $table = Engine_Api::_()->getDbtable('albums', 'album');
  336. $db = $table->getAdapter();
  337. $db->beginTransaction();
  338. try
  339. {
  340. $type = $this->_getParam('type', 'wall');
  341. if (empty($type)) $type = 'wall';
  342. $album = $table->getSpecialAlbum($viewer, $type);
  343. $photoTable = Engine_Api::_()->getDbtable('photos', 'album');
  344. $photo = $photoTable->createRow();
  345. $photo->setFromArray(array(
  346. 'owner_type' => 'user',
  347. 'owner_id' => Engine_Api::_()->user()->getViewer()->getIdentity()
  348. ));
  349. $photo->save();
  350. $photo->setPhoto($_FILES['Filedata']);
  351. if( $type == 'message' ) {
  352. $photo->title = Zend_Registry::get('Zend_Translate')->_('Attached Image');
  353. }
  354. $photo->collection_id = $album->album_id;
  355. $photo->save();
  356. if( !$album->photo_id )
  357. {
  358. $album->photo_id = $photo->getIdentity();
  359. $album->save();
  360. }
  361. if( $type != 'message' ) {
  362. // Authorizations
  363. $auth = Engine_Api::_()->authorization()->context;
  364. $auth->setAllowed($photo, 'everyone', 'view', true);
  365. $auth->setAllowed($photo, 'everyone', 'comment', true);
  366. }
  367. $db->commit();
  368. $this->view->status = true;
  369. $this->view->photo_id = $photo->photo_id;
  370. $this->view->album_id = $album->album_id;
  371. $this->view->src = $photo->getPhotoUrl();
  372. $this->view->message = Zend_Registry::get('Zend_Translate')->_('Photo saved successfully');
  373. }
  374. catch( Exception $e )
  375. {
  376. $db->rollBack();
  377. //throw $e;
  378. $this->view->status = false;
  379. }
  380. }
  381. }