/ezpublish-4.3.0/kernel/content/copy.php

https://github.com/stevoland/ez_patch · PHP · 261 lines · 157 code · 32 blank · 72 comment · 22 complexity · abe07a81551218cfb38ade1027566978 MD5 · raw file

  1. <?php
  2. //
  3. // Created on: <17-Jan-2003 12:47:11 amos>
  4. //
  5. // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
  6. // SOFTWARE NAME: eZ Publish
  7. // SOFTWARE RELEASE: 4.3.0
  8. // COPYRIGHT NOTICE: Copyright (C) 1999-2010 eZ Systems AS
  9. // SOFTWARE LICENSE: GNU General Public License v2.0
  10. // NOTICE: >
  11. // This program is free software; you can redistribute it and/or
  12. // modify it under the terms of version 2.0 of the GNU General
  13. // Public License as published by the Free Software Foundation.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of version 2.0 of the GNU General
  21. // Public License along with this program; if not, write to the Free
  22. // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  23. // MA 02110-1301, USA.
  24. //
  25. //
  26. // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
  27. //
  28. $Module = $Params['Module'];
  29. $ObjectID = $Params['ObjectID'];
  30. $http = eZHTTPTool::instance();
  31. if ( $http->hasPostVariable( 'BrowseCancelButton' ) )
  32. {
  33. if ( $http->hasPostVariable( 'BrowseCancelURI' ) )
  34. {
  35. return $Module->redirectTo( $http->postVariable( 'BrowseCancelURI' ) );
  36. }
  37. }
  38. if ( $ObjectID === null )
  39. {
  40. // ObjectID is returned after browsing
  41. $ObjectID = $http->postVariable( 'ObjectID' );
  42. }
  43. $object = eZContentObject::fetch( $ObjectID );
  44. if ( $object === null )
  45. return $Module->handleError( eZError::KERNEL_NOT_AVAILABLE, 'kernel' );
  46. if ( !$object->attribute( 'can_read' ) )
  47. return $Module->handleError( eZError::KERNEL_ACCESS_DENIED, 'kernel' );
  48. if ( $Module->isCurrentAction( 'Cancel' ) )
  49. {
  50. $mainParentNodeID = $object->attribute( 'main_parent_node_id' );
  51. return $Module->redirectToView( 'view', array( 'full', $mainParentNodeID ) );
  52. }
  53. $contentINI = eZINI::instance( 'content.ini' );
  54. /*!
  55. Copy the specified object to a given node
  56. */
  57. function copyObject( $Module, $object, $allVersions, $newParentNodeID )
  58. {
  59. // HACK! for my toolbar
  60. // TODO - how to persist ViewMode action var?
  61. $viewMode = 'detail';
  62. if ( !$newParentNodeID )
  63. return $Module->redirectToView( 'view', array( $viewMode, 2 ) );
  64. // check if we can create node under the specified parent node
  65. if( ( $newParentNode = eZContentObjectTreeNode::fetch( $newParentNodeID ) ) === null )
  66. return $Module->redirectToView( 'view', array( $viewMode, 2 ) );
  67. // END HACK!
  68. $classID = $object->attribute('contentclass_id');
  69. if ( !$newParentNode->checkAccess( 'create', $classID ) )
  70. {
  71. $objectID = $object->attribute( 'id' );
  72. eZDebug::writeError( "Cannot copy object $objectID to node $newParentNodeID, " .
  73. "the current user does not have create permission for class ID $classID",
  74. 'content/copy' );
  75. return $Module->handleError( eZError::KERNEL_ACCESS_DENIED, 'kernel' );
  76. }
  77. $db = eZDB::instance();
  78. $db->begin();
  79. $newObject = $object->copy( $allVersions );
  80. // We should reset section that will be updated in updateSectionID().
  81. // If sectionID is 0 then the object has been newly created
  82. $newObject->setAttribute( 'section_id', 0 );
  83. $newObject->store();
  84. $curVersion = $newObject->attribute( 'current_version' );
  85. $curVersionObject = $newObject->attribute( 'current' );
  86. $newObjAssignments = $curVersionObject->attribute( 'node_assignments' );
  87. unset( $curVersionObject );
  88. // remove old node assignments
  89. foreach( $newObjAssignments as $assignment )
  90. {
  91. $assignment->purge();
  92. }
  93. // and create a new one
  94. $nodeAssignment = eZNodeAssignment::create( array(
  95. 'contentobject_id' => $newObject->attribute( 'id' ),
  96. 'contentobject_version' => $curVersion,
  97. 'parent_node' => $newParentNodeID,
  98. 'is_main' => 1
  99. ) );
  100. $nodeAssignment->store();
  101. // publish the newly created object
  102. eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $newObject->attribute( 'id' ),
  103. 'version' => $curVersion ) );
  104. // Update "is_invisible" attribute for the newly created node.
  105. $newNode = $newObject->attribute( 'main_node' );
  106. eZContentObjectTreeNode::updateNodeVisibility( $newNode, $newParentNode );
  107. $db->commit();
  108. // HACK!
  109. return $Module->redirectToView( 'view', array( $viewMode, $newParentNodeID ) );
  110. }
  111. /*!
  112. Browse for node to place the object copy into
  113. */
  114. function browse( $Module, $object )
  115. {
  116. if ( $Module->hasActionParameter( 'LanguageCode' ) )
  117. $languageCode = $Module->actionParameter( 'LanguageCode' );
  118. else
  119. {
  120. $languageCode = false;
  121. }
  122. $objectID = $object->attribute( 'id' );
  123. $node = $object->attribute( 'main_node' );
  124. $class = $object->contentClass();
  125. $ignoreNodesSelect = array();
  126. $ignoreNodesClick = array();
  127. foreach ( $object->assignedNodes( false ) as $element )
  128. {
  129. $ignoreNodesSelect[] = $element['node_id'];
  130. $ignoreNodesClick[] = $element['node_id'];
  131. }
  132. $ignoreNodesSelect = array_unique( $ignoreNodesSelect );
  133. $ignoreNodesClick = array_unique( $ignoreNodesClick );
  134. // HACK! for front side editing
  135. $viewMode = 'full';
  136. $http = eZHTTPTool::instance();
  137. if ( $http->hasVariable( 'ViewMode' ) )
  138. $Module->setActionParameter( 'ViewMode', $http->variable( 'ViewMode' ) );
  139. if ( $Module->hasActionParameter( 'ViewMode' ) )
  140. $viewMode = $Module->actionParameter( 'ViewMode' );
  141. $sourceParentNodeID = $node->attribute( 'parent_node_id' );
  142. eZContentBrowse::browse( array( 'action_name' => 'CopyNode',
  143. 'description_template' => 'design:content/browse_copy_node.tpl',
  144. 'keys' => array( 'class' => $class->attribute( 'id' ),
  145. 'class_id' => $class->attribute( 'identifier' ),
  146. 'classgroup' => $class->attribute( 'ingroup_id_list' ),
  147. 'section' => $object->attribute( 'section_id' ) ),
  148. 'ignore_nodes_select' => $ignoreNodesSelect,
  149. 'ignore_nodes_click' => $ignoreNodesClick,
  150. 'persistent_data' => array( 'ObjectID' => $objectID ),
  151. 'permission' => array( 'access' => 'create', 'contentclass_id' => $class->attribute( 'id' ) ),
  152. 'content' => array( 'object_id' => $objectID,
  153. 'object_version' => $object->attribute( 'current_version' ),
  154. 'object_language' => $languageCode ),
  155. 'start_node' => $sourceParentNodeID,
  156. 'cancel_page' => $Module->redirectionURIForModule( $Module, 'view',
  157. array( $viewMode, $sourceParentNodeID, $languageCode ) ),
  158. 'from_page' => "/content/copy" ),
  159. $Module );
  160. }
  161. /*!
  162. Redirect to the page that lets a user to choose which versions to copy:
  163. either all version or the current one.
  164. */
  165. function chooseObjectVersionsToCopy( $Module, &$Result, $object )
  166. {
  167. $selectedNodeIDArray = eZContentBrowse::result( $Module->currentAction() );
  168. $tpl = eZTemplate::factory();
  169. $tpl->setVariable( 'object', $object );
  170. $tpl->setVariable( 'selected_node_id', $selectedNodeIDArray[0] );
  171. $Result['content'] = $tpl->fetch( 'design:content/copy.tpl' );
  172. $Result['path'] = array( array( 'url' => false,
  173. 'text' => ezpI18n::tr( 'kernel/content', 'Content' ) ),
  174. array( 'url' => false,
  175. 'text' => ezpI18n::tr( 'kernel/content', 'Copy' ) ) );
  176. }
  177. /*
  178. Object copying logic in pseudo-code:
  179. $targetNodeID = browse();
  180. $versionsToCopy = fetchObjectVersionsToCopyFromContentINI();
  181. if ( $versionsToCopy != 'user-defined' )
  182. $versionsToCopy = askUserAboutVersionsToCopy();
  183. copyObject( $object, $versionsToCopy, $targeNodeID );
  184. Action parameters:
  185. 1. initially: null
  186. 2. when user has selected the target node: 'CopyNode'
  187. 3. when/if user has selected versions to copy: 'Copy' or 'Cancel'
  188. */
  189. $versionHandling = $contentINI->variable( 'CopySettings', 'VersionHandling' );
  190. $chooseVersions = ( $versionHandling == 'user-defined' );
  191. if( $chooseVersions )
  192. $allVersions = ( $Module->actionParameter( 'VersionChoice' ) == 1 ) ? true : false;
  193. else
  194. $allVersions = ( $versionHandling == 'last-published' ) ? false : true;
  195. if ( $Module->isCurrentAction( 'Copy' ) )
  196. {
  197. // actually do copying after a user has selected object versions to copy
  198. $newParentNodeID = $http->postVariable( 'SelectedNodeID' );
  199. return copyObject( $Module, $object, $allVersions, $newParentNodeID );
  200. }
  201. else if ( $Module->isCurrentAction( 'CopyNode' ) )
  202. {
  203. // we get here after a user selects target node to place the source object under
  204. if( $chooseVersions )
  205. {
  206. // redirect to the page with choice of versions to copy
  207. $Result = array();
  208. chooseObjectVersionsToCopy( $Module, $Result, $object );
  209. }
  210. else
  211. {
  212. // actually do copying of the pre-configured object version(s)
  213. $selectedNodeIDArray = eZContentBrowse::result( $Module->currentAction() );
  214. $newParentNodeID = $selectedNodeIDArray[0];
  215. return copyObject( $Module, $object, $allVersions, $newParentNodeID );
  216. }
  217. }
  218. else // default, initial action
  219. {
  220. /*
  221. Browse for target node.
  222. We get here when a user clicks "copy" button when viewing some node.
  223. */
  224. browse( $Module, $object );
  225. }
  226. ?>