PageRenderTime 29ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/application/modules/media/controllers/config.php

http://github.com/tcm-project/tangocms
PHP | 281 lines | 209 code | 11 blank | 61 comment | 37 complexity | fecd7caa9ada2644d27889a50b172d14 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * Zula Framework Module
  4. * Configure media module, such as adding categories
  5. *
  6. * @patches submit all patches to patches@tangocms.org
  7. *
  8. * @author Alex Cartwright
  9. * @copyright Copyright (C) 2007, 2008, 2009, 2010 Alex Cartwright
  10. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU/GPL 2
  11. * @package TangoCMS_Media
  12. */
  13. class Media_controller_config extends Zula_ControllerBase {
  14. /**
  15. * Constructor
  16. * Sets common page links for configuration
  17. *
  18. * @return object
  19. */
  20. public function __construct( $details, $config, $sector ) {
  21. parent::__construct( $details, $config, $sector );
  22. $this->setPageLinks( array(
  23. t('Manage Categories') => $this->_router->makeUrl( 'media', 'config' ),
  24. t('Add Category') => $this->_router->makeUrl( 'media', 'config', 'addcat'),
  25. t('Manage Outstanding') => $this->_router->makeUrl( 'media', 'manage', 'outstanding' ),
  26. t('Settings') => $this->_router->makeUrl( 'media', 'config', 'settings' ),
  27. ));
  28. }
  29. /**
  30. * Displays all of the categories that the user has permission to.
  31. *
  32. * @return string
  33. */
  34. public function indexSection() {
  35. $this->setTitle( t('Manage Media') );
  36. // Check user has correct permission
  37. if ( !$this->_acl->checkMulti( array('media_add_category', 'media_edit_category', 'media_delete_category') ) ) {
  38. throw new Module_NoPermission;
  39. }
  40. $view = $this->loadView( 'config/main.html' );
  41. $view->assign( array(
  42. 'CATEGORIES' => $this->_model( 'media' )->getAllCategories(),
  43. ));
  44. $view->assignHtml( array(
  45. 'CSRF' => $this->_input->createToken( true ),
  46. ));
  47. return $view->getOutput();
  48. }
  49. /**
  50. * Displays and handles adding of a new category.
  51. *
  52. * @return string
  53. */
  54. public function addCatSection() {
  55. $this->setTitle( t('Add Media Category') );
  56. // Check permission
  57. if ( !$this->_acl->check( 'media_add_category' ) ) {
  58. throw new Module_NoPermission;
  59. }
  60. $form = $this->buildCatForm();
  61. if ( $form->hasInput() && $form->isValid() ) {
  62. $fd = $form->getValues( 'media' );
  63. $cid = $this->_model()->addCategory( $fd['name'], $fd['desc'] );
  64. // Update/Add all needed ACL resources
  65. foreach( array('media-cat_view_', 'media-cat_upload_', 'media-cat_moderate_') as $resource ) {
  66. try {
  67. $roles = $this->_input->post( 'acl_resources/'.$resource );
  68. } catch ( Input_KeyNoExist $e ) {
  69. $roles = array();
  70. }
  71. $this->_acl->allowOnly( $resource.$cid, $roles );
  72. }
  73. $this->_event->success( t('Added new media category') );
  74. return zula_redirect( $this->_router->makeUrl( 'media', 'config' ) );
  75. }
  76. return $form->getOutput();
  77. }
  78. /**
  79. * Displays and handles editing of an existing category
  80. *
  81. * @return string
  82. */
  83. public function editCatSection() {
  84. $this->setTitle( t('Edit Category') );
  85. if ( !$this->_acl->check( 'media_edit_category' ) ) {
  86. throw new Module_NoPermission;
  87. }
  88. // Get details for the category we are editing
  89. try {
  90. $cid = $this->_router->getArgument( 'id' );
  91. $category = $this->_model()->getCategory( $cid );
  92. $resource = 'media-cat_view_'.$category['id'];
  93. if ( !$this->_acl->resourceExists( $resource ) || !$this->_acl->check( $resource ) ) {
  94. throw new Module_NoPermission;
  95. }
  96. $form = $this->buildCatForm( $category['id'], $category['name'], $category['description'] );
  97. if ( $form->hasInput() && $form->isValid() ) {
  98. $fd = $form->getValues( 'media' );
  99. $this->_model()->editCategory( $category['id'], $fd['name'], $fd['desc'] );
  100. // Update/Add needed ACL resources
  101. foreach( array('media-cat_view_', 'media-cat_upload_', 'media-cat_moderate_') as $resource ) {
  102. try {
  103. $resource .= $category['id'];
  104. $roles = $this->_input->post( 'acl_resources/'.$resource );
  105. } catch ( Input_KeyNoExist $e ) {
  106. $roles = array();
  107. }
  108. $this->_acl->allowOnly( $resource, $roles );
  109. }
  110. $this->_event->success( t('Edited media category') );
  111. } else {
  112. return $form->getOutput();
  113. }
  114. } catch ( Router_ArgNoExist $e ) {
  115. $this->_event->error( t('No media category selected') );
  116. } catch ( Media_CategoryNoExist $e ) {
  117. $this->_event->error( t('Media category does not exist') );
  118. }
  119. return zula_redirect( $this->_router->makeUrl( 'media', 'config' ) );
  120. }
  121. /**
  122. * Builds form for adding or editing a media category
  123. *
  124. * @param int $cid
  125. * @param string $name
  126. * @param string $desc
  127. * @return object
  128. */
  129. protected function buildCatForm( $cid=null, $name=null, $desc=null ) {
  130. $form = new View_Form( 'config/form_cat.html', 'media', is_null($cid) );
  131. $form->addElement( 'media/name', $name, t('Name'), new Validator_Length(1, 255) );
  132. $form->addElement( 'media/desc', $desc, t('Description'), new Validator_Length(0, 255) );
  133. // Add additional data on
  134. $form->assign( array('OP' => is_null($cid) ? 'add' : 'edit') );
  135. $aclForm = $this->_acl->buildForm( array(
  136. t('View Media Category') => 'media-cat_view_'.$cid,
  137. t('Upload media items') => array('media-cat_upload_'.$cid, 'group_admin'),
  138. t('Edit/Delete media items') => array('media-cat_moderate_'.$cid, 'group_admin'),
  139. ));
  140. $form->assignHtml( array('ACL_FORM' => $aclForm) );
  141. return $form;
  142. }
  143. /**
  144. * Bridges between deleting, or purging a category.
  145. *
  146. * @return bool
  147. */
  148. public function bridgeSection() {
  149. $type = $this->_input->has( 'post', 'media_purge' ) ? 'purge' : 'delete';
  150. if ( !$this->_acl->resourceExists( 'media_'.$type.'_category' ) || !$this->_acl->check( 'media_'.$type.'_category' ) ) {
  151. throw new Module_NoPermission;
  152. } else if ( $this->_input->checkToken() ) {
  153. // Attempt to purge or delete
  154. try {
  155. $delCount = 0;
  156. $mediaDir = $this->_zula->getDir( 'uploads' ).'/media';
  157. foreach( $this->_input->post( 'media_cat_ids' ) as $cid ) {
  158. $resource = 'media-cat_moderate_'.$cid;
  159. if ( $this->_acl->resourceExists( $resource ) && $this->_acl->check( $resource ) ) {
  160. try {
  161. $method = $type == 'delete' ? 'deleteCategory' : 'purgeCategory';
  162. $this->_model()->$method( $cid );
  163. // Remove all media items
  164. zula_full_rmdir( $mediaDir.'/'.$cid );
  165. ++$delCount;
  166. } catch ( Media_CategoryNoExist $e ) {
  167. }
  168. }
  169. }
  170. $this->_event->success( $type == 'delete' ? t('Deleted selected categories') : t('Purged selected categories') );
  171. } catch ( Input_KeyNoExist $e ) {
  172. $this->_event->error( t('No media categories selected') );
  173. }
  174. } else {
  175. $this->_event->error( Input::csrfMsg() );
  176. }
  177. return zula_redirect( $this->_router->makeUrl( 'media', 'config' ) );
  178. }
  179. /**
  180. * Updates settings for the media module
  181. *
  182. * @return string
  183. */
  184. public function settingsSection() {
  185. $this->setTitle( t('Media Settings') );
  186. $this->setOutputType( self::_OT_CONFIG );
  187. if ( !$this->_acl->check( 'media_manage_settings' ) ) {
  188. throw new Module_NoPermission;
  189. }
  190. // Prepare the form of settings
  191. $mediaConf = $this->_config->get( 'media' );
  192. $form = new View_form( 'config/settings.html', 'media' );
  193. $form->addElement( 'media/per_page', $mediaConf['per_page'], t('Per page'), new Validator_Int )
  194. ->addElement( 'media/use_lightbox', $mediaConf['use_lightbox'], t('Use lightbox'), new Validator_Bool )
  195. ->addElement( 'media/max_fs', $mediaConf['max_fs'], t('Maximum file size'), new Validator_Int )
  196. ->addElement( 'media/thumb_dimension', $mediaConf['thumb_dimension'], t('Thumbnail width/height'), new Validator_Between(20, 200) )
  197. ->addElement( 'media/max_image_width', $mediaConf['max_image_width'], t('Maximum image width'), new Validator_Between(200, 90000) )
  198. ->addElement( 'media/wm_position', $mediaConf['wm_position'], t('Watermark position'),
  199. new Validator_InArray( array('t', 'tr', 'r', 'br', 'b', 'bl', 'l', 'tl') ),
  200. false
  201. );
  202. if ( $form->hasInput() && $form->isValid() ) {
  203. $purgeTmpImages = false;
  204. foreach( $form->getValues( 'media' ) as $key=>$val ) {
  205. if (
  206. ($key == 'max_image_width' && $mediaConf['max_image_width'] != $val)
  207. ||
  208. ($key == 'wm_position' && $mediaConf['wm_position'] != $val)
  209. ) {
  210. $purgeTmpImages = true;
  211. } else if ( $key == 'max_fs' ) {
  212. $val = zula_byte_value( $val.$this->_input->post('media/max_fs_unit') );
  213. }
  214. $this->_config_sql->update( 'media/'.$key, $val );
  215. }
  216. // Upload the watermark
  217. if ( $this->_input->has( 'post', 'media_wm_delete' ) ) {
  218. unlink( $this->_zula->getDir('uploads').'/media/wm.png' );
  219. unlink( $this->_zula->getDir('uploads').'/media/wm_thumb.png' );
  220. $purgeTmpImages = true;
  221. }
  222. try {
  223. $uploader = new Uploader( 'media_wm', $this->_zula->getDir('uploads').'/media' );
  224. $uploader->subDirectories( false )
  225. ->allowImages();
  226. $file = $uploader->getFile();
  227. if ( $file->upload() !== false ) {
  228. $image = new Image( $file->path );
  229. $image->mime = 'image/png';
  230. $image->save( $file->dirname.'/wm.png', false );
  231. $image->thumbnail( 80, 80 )
  232. ->save( $file->dirname.'/wm_thumb.png' );
  233. $purgeTmpImages = true;
  234. }
  235. } catch ( Uploader_NotEnabled $e ) {
  236. $this->_event->error( t('Sorry, it appears file uploads are disabled within your PHP configuration') );
  237. } catch ( Uploader_MaxFileSize $e ) {
  238. $msg = sprintf( t('Selected file exceeds the maximum allowed file size of %s'),
  239. zula_human_readable($e->getMessage())
  240. );
  241. $this->_event->error( $msg );
  242. } catch ( Uploader_InvalidMime $e ) {
  243. $this->_event->error( t('Sorry, the uploaded file is of the wrong file type') );
  244. } catch ( Uploader_Exception $e ) {
  245. $this->_log->message( $e->getMessage(), Log::L_WARNING );
  246. $this->_event->error( t('Oops, an error occurred while uploading your files') );
  247. } catch ( Image_Exception $e ) {
  248. $this->_log->message( $e->getMessage(), Log::L_WARNING );
  249. $this->_event->error( t('Oops, an error occurred while processing an image') );
  250. }
  251. // Purge tmp images if needed and redirect
  252. if ( $purgeTmpImages ) {
  253. $files = (array) glob( $this->_zula->getDir('tmp').'/media/max*-*' );
  254. foreach( array_filter( $files ) as $tmpFile ) {
  255. unlink( $tmpFile );
  256. }
  257. }
  258. $this->_event->success( t('Updated media settings') );
  259. return zula_redirect( $this->_router->makeUrl('media', 'config', 'settings') );
  260. }
  261. if ( is_file( $this->_zula->getDir('uploads').'/media/wm_thumb.png' ) ) {
  262. $wmThumbPath = $this->_zula->getDir( 'uploads', true ).'/media/wm_thumb.png';
  263. } else {
  264. $wmThumbPath = null;
  265. }
  266. $form->assign( array('WM_THUMB_PATH' => $wmThumbPath) );
  267. return $form->getOutput();
  268. }
  269. }
  270. ?>