PageRenderTime 51ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

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

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