AssociateController.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 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 
13 use \Exception;
14 
20 
21 /**
22  * AssociateController is used to (dis-)associates Node instances,
23  * e.g. in a parent/child association.
24  *
25  * The controller supports the following actions:
26  *
27  * <div class="controller-action">
28  * <div> __Action__ associate </div>
29  * <div>
30  * Connect two Node instances.
31  * | Parameter | Description
32  * |-----------------------|-------------------------
33  * | _in_ `sourceOid` | The object id of the association's source Node
34  * | _in_ `targetOid` | The object id of the association's target Node
35  * | _in_ `role` | The role of the target Node in the source Node
36  * | __Response Actions__ | |
37  * | `ok` | In all cases
38  * </div>
39  * </div>
40  *
41  * <div class="controller-action">
42  * <div> __Action__ disassociate </div>
43  * <div>
44  * Disconnect two Node instances.
45  * | Parameter | Description
46  * |-----------------------|-------------------------
47  * | _in_ `sourceOid` | The object id of the association's source Node
48  * | _in_ `targetOid` | The object id of the association's target Node
49  * | _in_ `role` | The role of the target Node in the source Node
50  * | __Response Actions__ | |
51  * | `ok` | In all cases
52  * </div>
53  * </div>
54  *
55  * @author ingo herwig <ingo@wemove.com>
56  */
58 
59  /**
60  * @see Controller::validate()
61  */
62  protected function validate() {
63  $request = $this->getRequest();
64  $response = $this->getResponse();
65 
66  // check object id validity
67  $sourceOid = ObjectId::parse($request->getValue('sourceOid'));
68  if(!$sourceOid) {
69  $response->addError(ApplicationError::get('OID_INVALID',
70  ['invalidOids' => [$request->getValue('sourceOid')]]));
71  return false;
72  }
73  $targetOid = ObjectId::parse($request->getValue('targetOid'));
74  if(!$targetOid) {
75  $response->addError(ApplicationError::get('OID_INVALID',
76  ['invalidOids' => [$request->getValue('targetOid')]]));
77  return false;
78  }
79 
80  // check association
81  $persistenceFacade = $this->getPersistenceFacade();
82  $mapper = $persistenceFacade->getMapper($sourceOid->getType());
83  // try role
84  if ($request->hasValue('role')) {
85  $relationDesc = $mapper->getRelation($request->getValue('role'));
86  if ($relationDesc == null) {
87  $response->addError(ApplicationError::get('ROLE_INVALID'));
88  return false;
89  }
90  }
91  // try type
92  else {
93  $relationDesc = $mapper->getRelation($targetOid->getType());
94  if ($relationDesc == null) {
95  $response->addError(ApplicationError::get('ASSOCIATION_INVALID'));
96  return false;
97  }
98  }
99  return true;
100  }
101 
102  /**
103  * @see Controller::doExecute()
104  */
105  protected function doExecute($method=null) {
106  $this->requireTransaction();
107  $request = $this->getRequest();
108  $response = $this->getResponse();
109  $persistenceFacade = $this->getPersistenceFacade();
110 
111  // get the source node
112  $sourceOID = ObjectId::parse($request->getValue('sourceOid'));
113  $sourceNode = $persistenceFacade->load($sourceOID, BuildDepth::SINGLE);
114 
115  // get the target node
116  $targetOID = ObjectId::parse($request->getValue('targetOid'));
117  $targetNode = $persistenceFacade->load($targetOID, BuildDepth::SINGLE);
118 
119  // get the role
120  $role = null;
121  if ($request->hasValue('role')) {
122  $role = $request->getValue('role');
123  }
124 
125  if ($sourceNode != null && $targetNode != null) {
126  // process actions
127  if ($request->getAction() == 'associate') {
128  try {
129  $sourceNode->addNode($targetNode, $role);
130  }
131  catch (Exception $ex) {
132  throw new ApplicationException($request, $response,
133  ApplicationError::get('ASSOCIATION_INVALID'));
134  }
135  }
136  elseif ($request->getAction() == 'disassociate') {
137  try {
138  $sourceNode->deleteNode($targetNode, $role);
139  }
140  catch (Exception $ex) {
141  throw new ApplicationException($request, $response,
142  ApplicationError::get('ASSOCIATION_INVALID'));
143  }
144  }
145  }
146  else {
147  if ($sourceNode == null) {
148  $response->addError(ApplicationError::get('OID_INVALID',
149  ['invalidOids' => ['sourceOid']]));
150  }
151  if ($targetNode == null) {
152  $response->addError(ApplicationError::get('OID_INVALID',
153  ['invalidOids' => ['targetOid']]));
154  }
155  }
156 
157  $response->setAction('ok');
158  }
159 }
160 ?>
ObjectId is the unique identifier of an object.
Definition: ObjectId.php:28
BuildDepth values are used to define the depth when loading object trees.
Definition: BuildDepth.php:19
requireTransaction()
Start or join a transaction that will be committed at the end of execution.
Definition: Controller.php:334
ApplicationError is used to signal errors that occur while processing a request.
static parse($oid)
Parse a serialized object id string into an ObjectId instance.
Definition: ObjectId.php:135
static get($code, $data=null)
Factory method for retrieving a predefined error instance.
ApplicationException signals a general application exception.
getPersistenceFacade()
Get the PersistenceFacade instance.
Definition: Controller.php:283
AssociateController is used to (dis-)associates Node instances, e.g.
getRequest()
Get the Request instance.
Definition: Controller.php:251
Application controllers.
Definition: namespaces.php:3
Controller is the base class of all controllers.
Definition: Controller.php:49
getResponse()
Get the Response instance.
Definition: Controller.php:259