class.AssociateController.php
Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 require_once(BASE."wcmf/lib/presentation/class.Controller.php");
00020 require_once(BASE."wcmf/lib/persistence/class.PersistenceFacade.php");
00021 require_once(BASE."wcmf/lib/model/class.Node.php");
00022 require_once(BASE."wcmf/lib/model/class.NullNode.php");
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 class AssociateController extends Controller
00044 {
00045
00046
00047
00048 function validate()
00049 {
00050 if(!PersistenceFacade::isValidOID($this->_request->getValue('oid')))
00051 {
00052 $this->setErrorMsg("No valid 'oid' given in data.");
00053 return false;
00054 }
00055 return true;
00056 }
00057
00058
00059
00060 function hasView()
00061 {
00062 return false;
00063 }
00064
00065
00066
00067
00068
00069 function executeKernel()
00070 {
00071 $persistenceFacade = &PersistenceFacade::getInstance();
00072 $lockManager = &LockManager::getInstance();
00073
00074
00075 $parentOID = $this->_request->getValue('oid');
00076 $lockManager->releaseLock($parentOID);
00077 $parentNode = &$persistenceFacade->load($parentOID, BUILDDEPTH_SINGLE);
00078
00079
00080 $associateoids = $this->_request->getValue('associateoids');
00081 $associateoidArray = split(',', $associateoids);
00082 foreach ($associateoidArray as $associateoid)
00083 {
00084 $associateoid = trim($associateoid);
00085 if(!PersistenceFacade::isValidOID($associateoid))
00086 {
00087 $this->setErrorMsg("Invalid oid given in data.");
00088 $this->_response->setAction('ok');
00089 return true;
00090 }
00091
00092
00093 $lockManager->releaseLock($associateoid);
00094 $childNode = &$persistenceFacade->load($associateoid, BUILDDEPTH_SINGLE);
00095
00096 if ($parentNode != null && $childNode != null)
00097 {
00098
00099 $parentType = PersistenceFacade::getOIDParameter($parentOID, 'type');
00100 $parentTemplate = &$persistenceFacade->create($parentType, 1);
00101 $childType = PersistenceFacade::getOIDParameter($associateoid, 'type');
00102 $childTemplate = &$persistenceFacade->create($childType, 1);
00103
00104
00105 if ($this->_request->getAction() == 'associate')
00106 {
00107
00108 if ($this->_request->getValue('associateAs') == 'child' && $this->isDirectAssociation($parentTemplate, $childTemplate))
00109 {
00110 $parentNode->addChild($childNode);
00111 $parentNode->setType($parentNode->getBaseType());
00112 $childNode->save();
00113 }
00114
00115 else if ($this->_request->getValue('associateAs') == 'parent' && $this->isDirectAssociation($childTemplate, $parentTemplate))
00116 {
00117 $childNode->addChild($parentNode);
00118 $parentNode->save();
00119 }
00120 else
00121 {
00122
00123 $linkType = $this->findAssociationType($parentTemplate, $childTemplate);
00124 if ($linkType != null)
00125 {
00126 $link = &$persistenceFacade->create($linkType, BUILDTYPE_SINGLE);
00127 $parentNode->addChild($link);
00128 $link->save();
00129 $link = &$persistenceFacade->load($link->getOID(), BUILDTYPE_SINGLE);
00130 $childNode->addChild($link);
00131 $link->save();
00132 }
00133 else
00134 {
00135 $this->appendErrorMsg(Message::get("Cannot associate %1% and %2%. No direct connection and no connection type found.",
00136 array($associateoid, $parentOID)));
00137 }
00138 }
00139 }
00140 elseif ($this->_request->getAction() == 'disassociate')
00141 {
00142
00143 if ($this->isDirectAssociation($parentTemplate, $childTemplate))
00144 {
00145
00146 $parentNode = new NullNode($parentTemplate->getBaseType());
00147 $parentNode->addChild($childNode);
00148 $childNode->save();
00149 }
00150
00151 else if ($this->isDirectAssociation($childTemplate, $parentTemplate))
00152 {
00153
00154 $childNode = new NullNode($childTemplate->getType());
00155 $childNode->addChild($parentNode);
00156 $parentNode->save();
00157 }
00158 else
00159 {
00160
00161 $linkType = $this->findAssociationType($parentTemplate, $childTemplate);
00162 if ($linkType != null)
00163 {
00164
00165 $parentLinks = $parentNode->getProperty('childoids');
00166 $childLinks = $childNode->getProperty('childoids');
00167 $linkOIDs = array_intersect($parentLinks, $childLinks);
00168 foreach($linkOIDs as $linkOID)
00169 {
00170 if (PersistenceFacade::getOIDParameter($linkOID, 'type') == $linkType)
00171 {
00172
00173 $obj = &$persistenceFacade->load($linkOID, BUILDDEPTH_SINGLE);
00174 $obj->delete();
00175 }
00176 }
00177 }
00178 else
00179 {
00180 $this->appendErrorMsg(Message::get("Cannot disassociate %1% and %2%. No direct connection and no connection type found.",
00181 array($associateoid, $parentOID)));
00182 }
00183 }
00184 }
00185 }
00186 }
00187 $this->_response->setAction('ok');
00188 return true;
00189 }
00190
00191
00192
00193
00194
00195
00196
00197 function isDirectAssociation(&$parent, &$child)
00198 {
00199 $childTypeChildren = $parent->getChildrenEx(null, $child->getType(), null, null);
00200 if (sizeof($childTypeChildren) > 0)
00201 return true;
00202 else
00203 return false;
00204 }
00205
00206
00207
00208
00209
00210
00211
00212
00213 function findAssociationType(&$parent, &$child)
00214 {
00215 foreach ($parent->getChildren() as $possibleChild)
00216 {
00217 if (in_array('manyToMany', $possibleChild->getPropertyNames()))
00218 {
00219 $associationEnds = $possibleChild->getProperty('manyToMany');
00220 if (in_array($child->getType(), $associationEnds))
00221 return $possibleChild->getType();
00222 }
00223 }
00224 foreach ($child->getChildren() as $possibleChild)
00225 {
00226 if (in_array('manyToMany', $possibleChild->getPropertyNames()))
00227 {
00228 $associationEnds = $possibleChild->getProperty('manyToMany');
00229 if (in_array($parent->getType(), $associationEnds))
00230 return $possibleChild->getType();
00231 }
00232 }
00233 return null;
00234 }
00235 }
00236 ?>
00237