DeleteController.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
12 
18 
19 /**
20  * DeleteController is used to delete Node instances.
21  *
22  * The controller supports the following actions:
23  *
24  * <div class="controller-action">
25  * <div> __Action__ _default_ </div>
26  * <div>
27  * Delete the given Node instance.
28  * | Parameter | Description
29  * |-----------------------|-------------------------
30  * | _in_ / _out_ `oid` | The object id of the Node to delete
31  * | __Response Actions__ | |
32  * | `ok` | In all cases
33  * </div>
34  * </div>
35  *
36  * @author ingo herwig <ingo@wemove.com>
37  */
39 
40  /**
41  * @see Controller::validate()
42  */
43  protected function validate() {
44  if (!$this->checkLanguageParameter()) {
45  return false;
46  }
47  // do default validation
48  return parent::validate();
49  }
50 
51  /**
52  * @see Controller::doExecute()
53  */
54  protected function doExecute() {
55  $persistenceFacade = $this->getPersistenceFacade();
56  $request = $this->getRequest();
57  $response = $this->getResponse();
58  $logger = $this->getLogger();
59 
60  $oid = ObjectId::parse($request->getValue('oid'));
61 
62  // start the transaction
63  $transaction = $persistenceFacade->getTransaction();
64  $transaction->begin();
65  try {
66  // load the doomed node
67  if ($oid) {
68  $doomedNode = $persistenceFacade->load($oid, BuildDepth::SINGLE);
69  if ($doomedNode == null) {
70  $logger->warn("An object with oid ".$oid." is does not exist.");
71  }
72  else {
73  if($this->confirmDelete($doomedNode)) {
74  // commit changes
75  $localization = $this->getLocalization();
76  if ($this->isLocalizedRequest()) {
77  // delete the translations for the requested language
78  $localization->deleteTranslation($doomedNode->getOID(), $request->getValue('language'));
79  }
80  else {
81  // delete the real object data and all translations
82  $localization->deleteTranslation($doomedNode->getOID());
83  $doomedNode->delete();
84  }
85  // after delete
86  $this->afterDelete($oid);
87  }
88  }
89  }
90  $transaction->commit();
91  }
92  catch (PessimisticLockException $ex) {
93  $response->addError(ApplicationError::get('OBJECT_IS_LOCKED',
94  array('lockedOids' => array($oid->__toString()))));
95  $transaction->rollback();
96  }
97  catch (\Exception $ex) {
98  $response->addError(ApplicationError::fromException($ex));
99  $transaction->rollback();
100  }
101 
102  $response->setValue('oid', $oid);
103  $response->setAction('ok');
104  }
105 
106  /**
107  * Confirm delete action on given Node.
108  * @note subclasses will override this to implement special application requirements.
109  * @param $node A reference to the Node to confirm.
110  * @return Boolean whether the Node should be deleted (default: _true_)
111  */
112  protected function confirmDelete($node) {
113  return true;
114  }
115 
116  /**
117  * Called after delete.
118  * @note subclasses will override this to implement special application requirements.
119  * @param $oid The oid of the Node deleted
120  */
121  protected function afterDelete($oid) {}
122 }
123 ?>
getRequest()
Get the Request instance.
Definition: Controller.php:190
Controller is the base class of all controllers.
Definition: Controller.php:48
isLocalizedRequest()
Check if the current request is localized.
Definition: Controller.php:300
getLocalization()
Get the Localization instance.
Definition: Controller.php:246
checkLanguageParameter()
Checks the language request parameter and adds an response error, if it is not contained in the Local...
Definition: Controller.php:315
static fromException(\Exception $ex)
Factory method for transforming an exception into an ApplicationError instance.
static parse($oid)
Parse a serialized object id string into an ObjectId instance.
Definition: ObjectId.php:144
Application controllers.
Definition: namespaces.php:3
static get($code, $data=null)
Factory method for retrieving a predefind error instance.
DeleteController is used to delete Node instances.
PessimisticLockException signals an exception when trying to create an pessimistic lock...
getLogger()
Get the Logger instance.
Definition: Controller.php:206
getResponse()
Get the Response instance.
Definition: Controller.php:198
confirmDelete($node)
Confirm delete action on given Node.
getPersistenceFacade()
Get the PersistenceFacade instance.
Definition: Controller.php:222