deletionplan.class.inc.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. // Copyright (C) 2010-2012 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Algorithm to delete object(s) and maintain data integrity
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. class DeleteException extends CoreException
  25. {
  26. }
  27. /**
  28. * Deletion plan (other objects to be deleted/modified, eventual issues, etc.)
  29. *
  30. * @package iTopORM
  31. */
  32. class DeletionPlan
  33. {
  34. //protected $m_aIssues;
  35. protected $m_bFoundStopper;
  36. protected $m_bFoundSecurityIssue;
  37. protected $m_bFoundManualDelete;
  38. protected $m_bFoundManualOperation;
  39. protected $m_iToDelete;
  40. protected $m_iToUpdate;
  41. protected $m_aToDelete;
  42. protected $m_aToUpdate;
  43. protected static $m_aModeUpdate = array(
  44. DEL_SILENT => array(
  45. DEL_SILENT => DEL_SILENT,
  46. DEL_AUTO => DEL_AUTO,
  47. DEL_MANUAL => DEL_MANUAL
  48. ),
  49. DEL_MANUAL => array(
  50. DEL_SILENT => DEL_MANUAL,
  51. DEL_AUTO => DEL_AUTO,
  52. DEL_MANUAL => DEL_MANUAL
  53. ),
  54. DEL_AUTO => array(
  55. DEL_SILENT => DEL_AUTO,
  56. DEL_AUTO => DEL_AUTO,
  57. DEL_MANUAL => DEL_AUTO
  58. )
  59. );
  60. public function __construct()
  61. {
  62. $this->m_iToDelete = 0;
  63. $this->m_iToUpdate = 0;
  64. $this->m_aToDelete = array();
  65. $this->m_aToUpdate = array();
  66. $this->m_bFoundStopper = false;
  67. $this->m_bFoundSecurityIssue = false;
  68. $this->m_bFoundManualDelete = false;
  69. $this->m_bFoundManualOperation = false;
  70. }
  71. public function ComputeResults()
  72. {
  73. $this->m_iToDelete = 0;
  74. $this->m_iToUpdate = 0;
  75. foreach($this->m_aToDelete as $sClass => $aToDelete)
  76. {
  77. foreach($aToDelete as $iId => $aData)
  78. {
  79. $this->m_iToDelete++;
  80. if (isset($aData['issue']))
  81. {
  82. $this->m_bFoundStopper = true;
  83. $this->m_bFoundManualOperation = true;
  84. if (isset($aData['issue_security']))
  85. {
  86. $this->m_bFoundSecurityIssue = true;
  87. }
  88. }
  89. if ($aData['mode'] == DEL_MANUAL)
  90. {
  91. $this->m_bFoundStopper = true;
  92. $this->m_bFoundManualDelete = true;
  93. }
  94. }
  95. }
  96. foreach($this->m_aToUpdate as $sClass => $aToUpdate)
  97. {
  98. foreach($aToUpdate as $iId => $aData)
  99. {
  100. $this->m_iToUpdate++;
  101. $oObject = $aData['to_reset'];
  102. $aExtKeyLabels = array();
  103. foreach ($aData['attributes'] as $sRemoteExtKey => $aRemoteAttDef)
  104. {
  105. $oObject->Set($sRemoteExtKey, $aData['values'][$sRemoteExtKey]);
  106. $aExtKeyLabels[] = $aRemoteAttDef->GetLabel();
  107. }
  108. $this->m_aToUpdate[$sClass][$iId]['attributes_list'] = implode(', ', $aExtKeyLabels);
  109. list($bRes, $aIssues, $bSecurityIssues) = $oObject->CheckToWrite();
  110. if (!$bRes)
  111. {
  112. $this->m_aToUpdate[$sClass][$iId]['issue'] = implode(', ', $aIssues);
  113. $this->m_bFoundStopper = true;
  114. if ($bSecurityIssues)
  115. {
  116. $this->m_aToUpdate[$sClass][$iId]['issue_security'] = true;
  117. $this->m_bFoundSecurityIssue = true;
  118. }
  119. }
  120. }
  121. }
  122. }
  123. public function GetIssues()
  124. {
  125. $aIssues = array();
  126. foreach ($this->m_aToDelete as $sClass => $aToDelete)
  127. {
  128. foreach ($aToDelete as $iId => $aData)
  129. {
  130. if (isset($aData['issue']))
  131. {
  132. $aIssues[] = $aData['issue'];
  133. }
  134. }
  135. }
  136. foreach ($this->m_aToUpdate as $sClass => $aToUpdate)
  137. {
  138. foreach ($aToUpdate as $iId => $aData)
  139. {
  140. if (isset($aData['issue']))
  141. {
  142. $aIssues[] = $aData['issue'];
  143. }
  144. }
  145. }
  146. return $aIssues;
  147. }
  148. public function ListDeletes()
  149. {
  150. return $this->m_aToDelete;
  151. }
  152. public function ListUpdates()
  153. {
  154. return $this->m_aToUpdate;
  155. }
  156. public function GetTargetCount()
  157. {
  158. return $this->m_iToDelete + $this->m_iToUpdate;
  159. }
  160. public function FoundStopper()
  161. {
  162. return $this->m_bFoundStopper;
  163. }
  164. public function FoundSecurityIssue()
  165. {
  166. return $this->m_bFoundSecurityIssue;
  167. }
  168. public function FoundManualOperation()
  169. {
  170. return $this->m_bFoundManualOperation;
  171. }
  172. public function FoundManualDelete()
  173. {
  174. return $this->m_bFoundManualDelete;
  175. }
  176. public function FoundManualUpdate()
  177. {
  178. }
  179. public function AddToDelete($oObject, $iDeletionMode = null)
  180. {
  181. if (is_null($iDeletionMode))
  182. {
  183. $bRequestedExplicitely = true;
  184. $iDeletionMode = DEL_AUTO;
  185. }
  186. else
  187. {
  188. $bRequestedExplicitely = false;
  189. }
  190. $sClass = get_class($oObject);
  191. $iId = $oObject->GetKey();
  192. if (isset($this->m_aToUpdate[$sClass][$iId]))
  193. {
  194. unset($this->m_aToUpdate[$sClass][$iId]);
  195. }
  196. if (isset($this->m_aToDelete[$sClass][$iId]))
  197. {
  198. if ($this->m_aToDelete[$sClass][$iId]['requested_explicitely'])
  199. {
  200. // No change: let it in mode DEL_AUTO
  201. }
  202. else
  203. {
  204. $iPrevDeletionMode = $this->m_aToDelete[$sClass][$iId]['mode'];
  205. $iNewDeletionMode = self::$m_aModeUpdate[$iPrevDeletionMode][$iDeletionMode];
  206. $this->m_aToDelete[$sClass][$iId]['mode'] = $iNewDeletionMode;
  207. if ($bRequestedExplicitely)
  208. {
  209. // This object was in the root list
  210. $this->m_aToDelete[$sClass][$iId]['requested_explicitely'] = true;
  211. $this->m_aToDelete[$sClass][$iId]['mode'] = DEL_AUTO;
  212. }
  213. }
  214. }
  215. else
  216. {
  217. $this->m_aToDelete[$sClass][$iId] = array(
  218. 'to_delete' => $oObject,
  219. 'mode' => $iDeletionMode,
  220. 'requested_explicitely' => $bRequestedExplicitely,
  221. );
  222. }
  223. }
  224. public function SetDeletionIssues($oObject, $aIssues, $bSecurityIssue)
  225. {
  226. if (count($aIssues) > 0)
  227. {
  228. $sClass = get_class($oObject);
  229. $iId = $oObject->GetKey();
  230. $this->m_aToDelete[$sClass][$iId]['issue'] = implode(', ', $aIssues);
  231. if ($bSecurityIssue)
  232. {
  233. $this->m_aToDelete[$sClass][$iId]['issue_security'] = true;
  234. }
  235. }
  236. }
  237. public function AddToUpdate($oObject, $oAttDef, $value = 0)
  238. {
  239. $sClass = get_class($oObject);
  240. $iId = $oObject->GetKey();
  241. if (isset($this->m_aToDelete[$sClass][$iId]))
  242. {
  243. // skip... it should be deleted anyhow !
  244. }
  245. else
  246. {
  247. if (!isset($this->m_aToUpdate[$sClass][$iId]))
  248. {
  249. $this->m_aToUpdate[$sClass][$iId] = array(
  250. 'to_reset' => $oObject,
  251. );
  252. }
  253. $this->m_aToUpdate[$sClass][$iId]['attributes'][$oAttDef->GetCode()] = $oAttDef;
  254. $this->m_aToUpdate[$sClass][$iId]['values'][$oAttDef->GetCode()] = $value;
  255. }
  256. }
  257. }
  258. ?>