/actions/content_actionhandler.php

https://github.com/SylvainGuittard/ezlightbox · PHP · 364 lines · 322 code · 19 blank · 23 comment · 56 complexity · a689a4a63451be15db34e99fbb428509 MD5 · raw file

  1. <?php
  2. //
  3. // Created on: <2007-11-21 13:01:28 ab>
  4. //
  5. // SOFTWARE NAME: eZ Lightbox extension for eZ Publish
  6. // SOFTWARE RELEASE: 0.x
  7. // COPYRIGHT NOTICE: Copyright (C) 1999-2010 eZ Systems AS
  8. // SOFTWARE LICENSE: GNU General Public License v2.0
  9. // NOTICE: >
  10. // This program is free software; you can redistribute it and/or
  11. // modify it under the terms of version 2.0 of the GNU General
  12. // Public License as published by the Free Software Foundation.
  13. //
  14. // This program is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. // GNU General Public License for more details.
  18. //
  19. // You should have received a copy of version 2.0 of the GNU General
  20. // Public License along with this program; if not, write to the Free
  21. // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  22. // MA 02110-1301, USA.
  23. //
  24. //
  25. function ezlightbox_ContentActionHandler( $module, $http, $objectID )
  26. {
  27. $error_message = '';
  28. $lightboxID = false;
  29. if ( $http->hasPostVariable( 'LightboxID' ) )
  30. {
  31. $lightboxID = $http->postVariable( 'LightboxID' );
  32. }
  33. else if ( $http->hasPostVariable( 'selectedLightboxID' ) )
  34. {
  35. $lightboxID = $http->postVariable( 'selectedLightboxID' );
  36. }
  37. else
  38. {
  39. $lightboxID = eZPreferences::value( eZLightbox::PREFERENCE_CURRENT_LIGHTBOX );
  40. if ( !$lightboxID || !is_numeric( $lightboxID ) || $lightboxID <= 0 )
  41. {
  42. $lightboxID = false;
  43. $error_message = ezpI18n::tr( 'lightbox/error', 'Missing or invalid lightbox ID %1.',
  44. null, array( $lightboxID )
  45. );
  46. }
  47. }
  48. $itemID = $objectID;
  49. $typeID = eZLightboxObject::TYPE_OBJECT_ID;
  50. if ( $http->hasPostVariable( 'ItemID' ) && $http->hasPostVariable( 'ItemType' ) )
  51. {
  52. $itemID = $http->postVariable( 'ItemID' );
  53. $itemType = $http->postVariable( 'ItemType' );
  54. $itemObject = eZLightboxObjectItem::fetchByName( $itemType );
  55. if ( !is_object( $itemObject ) )
  56. {
  57. $itemObject = eZLightboxObjectItem::fetchByID( $itemType );
  58. }
  59. if ( is_object( $itemObject ) )
  60. {
  61. $typeID = $itemObject->getID();
  62. }
  63. else
  64. {
  65. eZDebug::writeWarning( 'Failed to fetch lightbox item object for item type ' . $itemType, 'lightbox/action' );
  66. }
  67. }
  68. if ( $http->hasPostVariable( 'ChangeUserCurrentLightbox' ) &&
  69. $http->hasPostVariable( 'newLightboxID' )
  70. )
  71. {
  72. $http = eZHTTPTool::instance();
  73. $http->removeSessionVariable( eZLightbox::PREFERENCE_SESSION_HASHKEY );
  74. eZPreferences::setValue( 'currentLightboxID', $http->postVariable( 'newLightboxID' ) );
  75. $redirectURI = $http->hasPostVariable( 'redirectAfterSelectionURI' ) ?
  76. $http->postVariable( 'redirectAfterSelectionURI' ) :
  77. $http->sessionVariable( 'LastAccessesURI' );
  78. $module->redirectTo( $redirectURI );
  79. return true;
  80. }
  81. if ( $http->hasPostVariable( 'GoBackButton' ) )
  82. {
  83. $redirectURI = '/';
  84. if ( $http->hasPostVariable( 'redirectURI' ) )
  85. {
  86. $redirectURI = $http->postVariable( 'redirectURI' );
  87. }
  88. else if ( $http->hasSessionVariable( 'LastAccessesURI' ) &&
  89. !preg_match( '/\b\(type\)\b/', $http->sessionVariable( 'LastAccessesURI' ) )
  90. )
  91. {
  92. $redirectURI = $http->sessionVariable( 'LastAccessesURI' );
  93. if ( $redirectURI == '' )
  94. {
  95. $redirectURI = '/';
  96. }
  97. }
  98. else if ( $http->hasSessionVariable( 'LastSearchURI' ) )
  99. {
  100. $redirectURI = $http->sessionVariable( 'LastSearchURI' );
  101. }
  102. $module->redirectTo( $redirectURI );
  103. return true;
  104. }
  105. if ( $http->hasPostVariable( 'ViewLightboxAction' ) )
  106. {
  107. if ( !$lightboxID )
  108. {
  109. $module->handleError( eZLightbox::ERROR_NOT_FOUND, 'lightbox',
  110. array( 'error_message' => $error_message )
  111. );
  112. return true;
  113. }
  114. $module->redirectTo( '/lightbox/view/list/' . $lightboxID );
  115. return true;
  116. }
  117. if ( $http->hasPostVariable( 'CreateLightboxAction' ) )
  118. {
  119. $module->redirectTo( '/lightbox/create' );
  120. return true;
  121. }
  122. if ( $http->hasPostVariable( 'EditLightboxAction' ) )
  123. {
  124. $module->redirectTo( '/lightbox/edit/' . $lightboxID );
  125. return true;
  126. }
  127. if ( $http->hasPostVariable( 'SendLightboxAction' ) )
  128. {
  129. $module->redirectTo( '/lightbox/send/' . $lightboxID );
  130. return true;
  131. }
  132. if ( $http->hasPostVariable( 'DeleteLightboxAction' ) )
  133. {
  134. if ( !$lightboxID )
  135. {
  136. $module->handleError( eZLightbox::ERROR_NOT_FOUND, 'lightbox',
  137. array( 'error_message' => $error_message )
  138. );
  139. return true;
  140. }
  141. $operationResult = eZOperationHandler::execute( 'lightbox', 'delete',
  142. array( 'id' => $lightboxID )
  143. );
  144. if ( $operationResult['status'] == eZModuleOperationInfo::STATUS_CONTINUE )
  145. {
  146. $module->redirectTo( $http->sessionVariable( 'LastAccessesURI' ) );
  147. return true;
  148. }
  149. else
  150. {
  151. $module->handleError( eZLightbox::ERROR_OPERATION_FAILED, 'lightbox',
  152. array( 'error_messages' => $operationResult['messages'] )
  153. );
  154. return true;
  155. }
  156. }
  157. if ( $http->hasPostVariable( 'EmptyLightboxAction' ) )
  158. {
  159. if ( !$lightboxID )
  160. {
  161. $module->handleError( eZLightbox::ERROR_NOT_FOUND, 'lightbox',
  162. array( 'error_message' => $error_message )
  163. );
  164. return true;
  165. }
  166. $operationResult = eZOperationHandler::execute( 'lightbox', 'empty',
  167. array( 'id' => $lightboxID )
  168. );
  169. if ( $operationResult['status'] == eZModuleOperationInfo::STATUS_CONTINUE )
  170. {
  171. $redirectURI = $http->hasPostVariable( 'redirectAfterSelectionURI' ) ?
  172. $http->postVariable( 'redirectAfterSelectionURI' ) :
  173. $http->sessionVariable( 'LastAccessesURI' );
  174. $module->redirectTo( $redirectURI );
  175. return true;
  176. }
  177. else
  178. {
  179. $module->handleError( eZLightbox::ERROR_OPERATION_FAILED, 'lightbox',
  180. array( 'error_messages' => $operationResult['messages'] )
  181. );
  182. return true;
  183. }
  184. }
  185. if ( $http->hasPostVariable( 'AddToLightboxAction' ) )
  186. {
  187. if ( !$lightboxID )
  188. {
  189. $lightboxID = eZPreferences::value( eZLightbox::PREFERENCE_CURRENT_LIGHTBOX );
  190. if ( !$lightboxID )
  191. {
  192. $module->redirect( 'lightbox', 'create', array(), null, array( 'ItemID' => $itemID,
  193. 'TypeID' => $typeID
  194. )
  195. );
  196. return true;
  197. }
  198. }
  199. $operationResult = eZOperationHandler::execute( 'lightbox', 'add',
  200. array( 'id' => $lightboxID,
  201. 'item_id' => $itemID,
  202. 'type_id' => $typeID
  203. )
  204. );
  205. if ( $operationResult['status'] == eZModuleOperationInfo::STATUS_CONTINUE )
  206. {
  207. if ( $http->hasPostVariable( 'RedirectToSearchResult' ) &&
  208. $http->hasSessionVariable( 'LastSearchURI' )
  209. )
  210. {
  211. $module->redirectTo( $http->sessionVariable( 'LastSearchURI' ) );
  212. return true;
  213. }
  214. else
  215. {
  216. $module->redirectTo( $http->sessionVariable( 'LastAccessesURI' ) );
  217. return true;
  218. }
  219. }
  220. else
  221. {
  222. $module->handleError( eZLightbox::ERROR_OPERATION_FAILED, 'lightbox',
  223. array( 'error_messages' => $operationResult['messages'] )
  224. );
  225. return true;
  226. }
  227. }
  228. if ( $http->hasPostVariable( 'RemoveFromLightboxAction' ) )
  229. {
  230. if ( !$lightboxID )
  231. {
  232. $module->handleError( eZLightbox::ERROR_NOT_FOUND, 'lightbox',
  233. array( 'error_message' => $error_message )
  234. );
  235. return true;
  236. }
  237. $operationResult = eZOperationHandler::execute( 'lightbox', 'remove',
  238. array( 'id' => $lightboxID,
  239. 'item_id' => $itemID,
  240. 'type_id' => $typeID
  241. )
  242. );
  243. if ( $operationResult['status'] == eZModuleOperationInfo::STATUS_CONTINUE )
  244. {
  245. $redirectURI = $http->hasPostVariable( 'redirectAfterSelectionURI' ) ?
  246. $http->postVariable( 'redirectAfterSelectionURI' ) :
  247. $http->sessionVariable( 'LastAccessesURI' );
  248. $module->redirectTo( $redirectURI );
  249. return true;
  250. }
  251. else
  252. {
  253. $module->handleError( eZLightbox::ERROR_OPERATION_FAILED, 'lightbox',
  254. array( 'error_messages' => $operationResult['messages'] )
  255. );
  256. return true;
  257. }
  258. }
  259. if ( $http->hasPostVariable( 'ActionMoveLightboxItemUp' ) ||
  260. $http->hasPostVariable( 'ActionMoveLightboxItemDown' )
  261. )
  262. {
  263. if ( !$lightboxID )
  264. {
  265. $module->handleError( eZLightbox::ERROR_NOT_FOUND, 'lightbox',
  266. array( 'error_message' => $error_message )
  267. );
  268. return true;
  269. }
  270. $direction = eZLightbox::MOVE_DIRECTION_UP;
  271. if ( $http->hasPostVariable( 'ActionMoveLightboxItemDown' ) )
  272. {
  273. $direction = eZLightbox::MOVE_DIRECTION_DOWN;
  274. }
  275. $lightboxObject = eZLightbox::fetch( $lightboxID );
  276. if ( is_object( $lightboxObject ) )
  277. {
  278. $moveResult = $lightboxObject->moveItem( $direction, $itemID );
  279. if ( !$moveResult )
  280. {
  281. eZDebug::writeWarning( 'Failed to move item with ID ' . $itemID . ' in lightbox with ID ' . $lightboxID . '.',
  282. 'ezlightbox_ContentActionHandler'
  283. );
  284. }
  285. }
  286. else
  287. {
  288. eZDebug::writeWarning( 'Failed to fetch lightbox object. Can not move item.', 'ezlightbox_ContentActionHandler' );
  289. }
  290. $redirectURI = $http->hasPostVariable( 'redirectAfterSelectionURI' ) ?
  291. $http->postVariable( 'redirectAfterSelectionURI' ) :
  292. $http->sessionVariable( 'LastAccessesURI' );
  293. $module->redirectTo( $redirectURI );
  294. return true;
  295. }
  296. if ( $http->hasPostVariable( 'MoveToLightboxAction' ) )
  297. {
  298. if ( !$lightboxID )
  299. {
  300. $module->handleError( eZLightbox::ERROR_NOT_FOUND, 'lightbox',
  301. array( 'error_message' => $error_message )
  302. );
  303. return true;
  304. }
  305. $targetLightboxID = false;
  306. $error_message = ezpI18n::tr( 'lightbox/error', 'Missing target lightbox ID.' );
  307. if ( $http->hasPostVariable( 'MoveToLightboxID' ) )
  308. {
  309. $targetLightboxID = $http->postVariable( 'MoveToLightboxID' );
  310. if ( !$targetLightboxID || !is_numeric( $targetLightboxID ) || $targetLightboxID <= 0 )
  311. {
  312. $targetLightboxID = false;
  313. }
  314. }
  315. if ( !$targetLightboxID )
  316. {
  317. $module->handleError( eZLightbox::ERROR_NOT_FOUND, 'lightbox',
  318. array( 'error_message' => $error_message )
  319. );
  320. return true;
  321. }
  322. $operationResult = eZOperationHandler::execute( 'lightbox', 'move',
  323. array( 'id' => $lightboxID,
  324. 'target_id' => $targetLightboxID,
  325. 'item_id' => $itemID,
  326. 'type_id' => $typeID
  327. )
  328. );
  329. if ( $operationResult['status'] == eZModuleOperationInfo::STATUS_CONTINUE )
  330. {
  331. $redirectURI = $http->hasPostVariable( 'redirectAfterSelectionURI' ) ?
  332. $http->postVariable( 'redirectAfterSelectionURI' ) :
  333. $http->sessionVariable( 'LastAccessesURI' );
  334. $module->redirectTo( $redirectURI );
  335. return true;
  336. }
  337. else
  338. {
  339. $module->handleError( eZLightbox::ERROR_OPERATION_FAILED, 'lightbox',
  340. array( 'error_messages' => $operationResult['messages'] )
  341. );
  342. }
  343. }
  344. }
  345. ?>