- Fixed #16160: Call to undefined function ezi18n()
[tinyz:tinyz.git] / kernel / content / restore.php
1 <?php
2 //
3 // Created on: <03-May-2002 15:17:01 bf>
4 //
5 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
6 // SOFTWARE NAME: eZ Publish
7 // SOFTWARE RELEASE: 4.1.x
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
29
30 require_once( 'kernel/common/template.php' );
31
32 $objectID = $Params['ObjectID'];
33 $module = $Params['Module'];
34
35 $object = eZContentObject::fetch( $objectID );
36 if ( !is_object( $object ) )
37 {
38     return $Module->handleError( EZ_ERROR_KERNEL_NOT_AVAILABLE, 'kernel' );
39 }
40 if ( $object->attribute( 'status' ) != eZContentObject::STATUS_ARCHIVED )
41 {
42     eZDebug::writeError( "Object with ID " . (int)$objectID . " is not archived, cannot restore." );
43     return $module->redirectToView( 'trash' );
44 }
45
46 $ini = eZINI::instance();
47 $userClassID = $ini->variable( "UserSettings", "UserClassID" );
48
49 if ( $module->isCurrentAction( 'Cancel' ) )
50 {
51     return $module->redirectToView( 'trash' );
52 }
53
54 $class = $object->contentClass();
55 $version = $object->attribute( 'current' );
56
57 $location = null;
58 $assignments = $version->attribute( 'node_assignments' );
59 foreach ( $assignments as $assignment )
60 {
61     $opCode = $assignment->attribute( 'op_code' );
62     $opCode &= ~1;
63     // We only include assignments which create or nops.
64     if ( $opCode == eZNodeAssignment::OP_CODE_CREATE_NOP ||
65          $opCode == eZNodeAssignment::OP_CODE_NOP )
66     {
67         $node = $assignment->attribute( 'parent_node_obj' );
68         if ( !$node )
69         {
70             continue;
71         }
72         if ( $assignment->attribute( 'is_main' ) )
73         {
74             $parentNode = $assignment->attribute( 'parent_node_obj' );
75             $parentNodeObject = $parentNode->attribute( 'object' );
76             $canCreate = $parentNode->checkAccess( 'create', $class->attribute( 'id' ), $parentNodeObject->attribute( 'contentclass_id' ) ) == 1;
77             if ( !$canCreate )
78             {
79                 continue;
80             }
81             $location = $assignment;
82             break;
83         }
84         else if ( !$location )
85         {
86             $parentNode = $assignment->attribute( 'parent_node_obj' );
87             $parentNodeObject = $parentNode->attribute( 'object' );
88             $canCreate = $parentNode->checkAccess( 'create', $class->attribute( 'id' ), $parentNodeObject->attribute( 'contentclass_id' ) ) == 1;
89             if ( !$canCreate )
90             {
91                 continue;
92             }
93             $location = $assignment;
94         }
95     }
96 }
97
98 if ( $module->isCurrentAction( 'Confirm' ) )
99 {
100     $type = $module->actionParameter( 'RestoreType' );
101     if ( $type == 1 )
102     {
103         $selectedNodeIDArray = array( $location->attribute( 'parent_node' ) );
104         $module->setCurrentAction( 'AddLocation' );
105     }
106     elseif ( $type == 2 )
107     {
108         $languageCode = $object->attribute( 'initial_language_code' );
109         eZContentBrowse::browse( array( 'action_name' => 'AddNodeAssignment',
110                                         'description_template' => 'design:content/browse_placement.tpl',
111                                         'keys' => array( 'class' => $class->attribute( 'id' ),
112                                                          'class_id' => $class->attribute( 'identifier' ),
113                                                          'classgroup' => $class->attribute( 'ingroup_id_list' ),
114                                                          'section' => $object->attribute( 'section_id' ) ),
115                                         'ignore_nodes_select' => array(),
116                                         'ignore_nodes_click'  => array(),
117                                         'persistent_data' => array( 'ContentObjectID' => $objectID,
118                                                                     'AddLocationAction' => '1' ),
119                                         'content' => array( 'object_id' => $objectID,
120                                                             'object_version' => $version->attribute( 'version' ),
121                                                             'object_language' => $languageCode ),
122                                         'cancel_page' => '/content/trash/',
123                                         'from_page' => "/content/restore/" . $objectID ),
124                                  $module );
125
126         return;
127     }
128 }
129
130 if ( $module->isCurrentAction( 'AddLocation' ) )
131 {
132     // If $selectedNodeIDArray is already set then use it as it is,
133     // if not get the browse data.
134     if ( !isset( $selectedNodeIDArray ) )
135     {
136         $selectedNodeIDArray = eZContentBrowse::result( 'AddNodeAssignment' );
137         if ( !$selectedNodeIDArray )
138         {
139             return $module->redirectToView( 'trash' );
140         }
141     }
142
143     $db = eZDB::instance();
144     $db->begin();
145     $locationAdded = false;
146     $mainNodeID = false;
147
148     $newLocationList    = array();
149     $failedLocationList = array();
150     foreach ( $selectedNodeIDArray as $selectedNodeID )
151     {
152         $parentNode = eZContentObjectTreeNode::fetch( $selectedNodeID );
153         $parentNodeObject = $parentNode->attribute( 'object' );
154
155         $canCreate = $parentNode->checkAccess( 'create', $class->attribute( 'id' ), $parentNodeObject->attribute( 'contentclass_id' ) ) == 1;
156
157         if ( $canCreate )
158         {
159             if ( $mainNodeID === false )
160             {
161                 $isMain = true;
162             }
163             $newLocationList[] = array( 'parent_node_id' => $selectedNodeID,
164                                         'is_main'        => $isMain );
165
166             $locationAdded = true;
167         }
168         else
169         {
170             $failedLocationList[] = array( 'parent_node_id' => $selectedNodeID );
171         }
172     }
173
174     // Check if we have failures
175     if ( count( $failedLocationList ) > 0 )
176     {
177         return $module->handleError( eZError::KERNEL_ACCESS_DENIED, 'kernel' );
178     }
179
180     // Remove all existing assignments, only our new ones should be present.
181     foreach ( $version->attribute( 'node_assignments' ) as $assignment )
182     {
183         $assignment->purge();
184     }
185
186     // Add all new locations
187     foreach ( $newLocationList as $newLocation )
188     {
189         $version->assignToNode( $newLocation['parent_node_id'], $newLocation['is_main'] );
190     }
191
192     $object->setAttribute( 'status', eZContentObject::STATUS_DRAFT );
193     $object->store();
194     $version->setAttribute( 'status', eZContentObjectVersion::STATUS_DRAFT );
195     $version->store();
196
197     $user = eZUser::currentUser();
198     $operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $objectID,
199                                                                                  'version' => $version->attribute( 'version' ) ) );
200     if ( ( array_key_exists( 'status', $operationResult ) && $operationResult['status'] != eZModuleOperationInfo::STATUS_CONTINUE ) )
201     {
202         switch( $operationResult['status'] )
203         {
204             case eZModuleOperationInfo::STATUS_HALTED:
205             case eZModuleOperationInfo::STATUS_CANCELLED:
206             {
207                 $module->redirectToView( 'trash' );
208             }
209         }
210     }
211     $objectID = $object->attribute( 'id' );
212     $object = eZContentObject::fetch( $objectID );
213     $mainNodeID = $object->attribute( 'main_node_id' );
214
215     eZContentObjectTrashNode::purgeForObject( $objectID  );
216
217     if ( $locationAdded )
218     {
219         if ( $object->attribute( 'contentclass_id' ) == $userClassID )
220         {
221             eZUser::cleanupCache();
222         }
223     }
224
225     eZContentObject::fixReverseRelations( $objectID, 'restore' );
226
227     $db->commit();
228     $module->redirectToView( 'view', array( 'full', $mainNodeID ) );
229     return;
230 }
231
232 $tpl = templateInit();
233
234 $res = eZTemplateDesignResource::instance();
235
236 $designKeys = array( array( 'object', $object->attribute( 'id' ) ), // Object ID
237                      array( 'remote_id', $object->attribute( 'remote_id' ) ),
238                      array( 'class', $class->attribute( 'id' ) ), // Class ID
239                      array( 'class_identifier', $class->attribute( 'identifier' ) ) ); // Class identifier
240
241 $res->setKeys( $designKeys );
242
243 $Result = array();
244
245 $tpl->setVariable( "object",   $object );
246 $tpl->setVariable( "version",  $version );
247 $tpl->setVariable( "location", $location );
248
249 $Result['content'] = $tpl->fetch( 'design:content/restore.tpl' );
250 $Result['path'] = array( array( 'uri'  => false,
251                                 'text' => ezpI18n::translate( "kernel/content/restore", "Restore object" ) ),
252                          array( 'uri'  => false,
253                                 'text' => $object->attribute( 'name' ) ) );
254
255 ?>