deletionplan.class.inc.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Class dbObject: the root of persistent classes
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  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. foreach($this->m_aToDelete as $sClass => $aToDelete)
  74. {
  75. foreach($aToDelete as $iId => $aData)
  76. {
  77. $this->m_iToDelete++;
  78. if (isset($aData['issue']))
  79. {
  80. $this->m_bFoundStopper = true;
  81. $this->m_bFoundManualOperation = true;
  82. if (isset($aData['issue_security']))
  83. {
  84. $this->m_bFoundSecurityIssue = true;
  85. }
  86. }
  87. if ($aData['mode'] == DEL_MANUAL)
  88. {
  89. $this->m_bFoundStopper = true;
  90. $this->m_bFoundManualDelete = true;
  91. }
  92. }
  93. }
  94. foreach($this->m_aToUpdate as $sClass => $aToUpdate)
  95. {
  96. foreach($aToUpdate as $iId => $aData)
  97. {
  98. $this->m_iToUpdate++;
  99. $oObject = $aData['to_reset'];
  100. $aExtKeyLabels = array();
  101. foreach ($aData['attributes'] as $sRemoteExtKey => $aRemoteAttDef)
  102. {
  103. $oObject->Set($sRemoteExtKey, $aData['values'][$sRemoteExtKey]);
  104. $aExtKeyLabels[] = $aRemoteAttDef->GetLabel();
  105. }
  106. $this->m_aToUpdate[$sClass][$iId]['attributes_list'] = implode(', ', $aExtKeyLabels);
  107. list($bRes, $aIssues, $bSecurityIssues) = $oObject->CheckToWrite();
  108. if (!$bRes)
  109. {
  110. $this->m_aToUpdate[$sClass][$iId]['issue'] = implode(', ', $aIssues);
  111. $this->m_bFoundStopper = true;
  112. if ($bSecurityIssues)
  113. {
  114. $this->m_aToUpdate[$sClass][$iId]['issue_security'] = true;
  115. $this->m_bFoundSecurityIssue = true;
  116. }
  117. }
  118. }
  119. }
  120. }
  121. public function GetIssues()
  122. {
  123. $aIssues = array();
  124. foreach ($this->m_aToDelete as $sClass => $aToDelete)
  125. {
  126. foreach ($aToDelete as $iId => $aData)
  127. {
  128. if (isset($aData['issue']))
  129. {
  130. $aIssues[] = $aData['issue'];
  131. }
  132. }
  133. }
  134. foreach ($this->m_aToUpdate as $sClass => $aToUpdate)
  135. {
  136. foreach ($aToUpdate as $iId => $aData)
  137. {
  138. if (isset($aData['issue']))
  139. {
  140. $aIssues[] = $aData['issue'];
  141. }
  142. }
  143. }
  144. return $aIssues;
  145. }
  146. public function ListDeletes()
  147. {
  148. return $this->m_aToDelete;
  149. }
  150. public function ListUpdates()
  151. {
  152. return $this->m_aToUpdate;
  153. }
  154. public function GetTargetCount()
  155. {
  156. return $this->m_iToDelete + $this->m_iToUpdate;
  157. }
  158. public function FoundStopper()
  159. {
  160. return $this->m_bFoundStopper;
  161. }
  162. public function FoundSecurityIssue()
  163. {
  164. return $this->m_bFoundSecurityIssue;
  165. }
  166. public function FoundManualOperation()
  167. {
  168. return $this->m_bFoundManualOperation;
  169. }
  170. public function FoundManualDelete()
  171. {
  172. return $this->m_bFoundManualDelete;
  173. }
  174. public function FoundManualUpdate()
  175. {
  176. }
  177. public function AddToDelete($oObject, $iDeletionMode = null)
  178. {
  179. if (is_null($iDeletionMode))
  180. {
  181. $bRequestedExplicitely = true;
  182. $iDeletionMode = DEL_AUTO;
  183. }
  184. else
  185. {
  186. $bRequestedExplicitely = false;
  187. }
  188. $sClass = get_class($oObject);
  189. $iId = $oObject->GetKey();
  190. if (isset($this->m_aToUpdate[$sClass][$iId]))
  191. {
  192. unset($this->m_aToUpdate[$sClass][$iId]);
  193. }
  194. if (isset($this->m_aToDelete[$sClass][$iId]))
  195. {
  196. if ($this->m_aToDelete[$sClass][$iId]['requested_explicitely'])
  197. {
  198. // No change: let it in mode DEL_AUTO
  199. }
  200. else
  201. {
  202. $iPrevDeletionMode = $this->m_aToDelete[$sClass][$iId]['mode'];
  203. $iNewDeletionMode = self::$m_aModeUpdate[$iPrevDeletionMode][$iDeletionMode];
  204. $this->m_aToDelete[$sClass][$iId]['mode'] = $iNewDeletionMode;
  205. if ($bRequestedExplicitely)
  206. {
  207. // This object was in the root list
  208. $this->m_aToDelete[$sClass][$iId]['requested_explicitely'] = true;
  209. $this->m_aToDelete[$sClass][$iId]['mode'] = DEL_AUTO;
  210. }
  211. }
  212. }
  213. else
  214. {
  215. $this->m_aToDelete[$sClass][$iId] = array(
  216. 'to_delete' => $oObject,
  217. 'mode' => $iDeletionMode,
  218. 'requested_explicitely' => $bRequestedExplicitely,
  219. );
  220. }
  221. }
  222. public function SetDeletionIssues($oObject, $aIssues, $bSecurityIssue)
  223. {
  224. if (count($aIssues) > 0)
  225. {
  226. $sClass = get_class($oObject);
  227. $iId = $oObject->GetKey();
  228. $this->m_aToDelete[$sClass][$iId]['issue'] = implode(', ', $aIssues);
  229. if ($bSecurityIssue)
  230. {
  231. $this->m_aToDelete[$sClass][$iId]['issue_security'] = true;
  232. }
  233. }
  234. }
  235. public function AddToUpdate($oObject, $oAttDef, $value = 0)
  236. {
  237. $sClass = get_class($oObject);
  238. $iId = $oObject->GetKey();
  239. if (isset($this->m_aToDelete[$sClass][$iId]))
  240. {
  241. // skip... it should be deleted anyhow !
  242. }
  243. else
  244. {
  245. if (!isset($this->m_aToUpdate[$sClass][$iId]))
  246. {
  247. $this->m_aToUpdate[$sClass][$iId] = array(
  248. 'to_reset' => $oObject,
  249. );
  250. }
  251. $this->m_aToUpdate[$sClass][$iId]['attributes'][$oAttDef->GetCode()] = $oAttDef;
  252. $this->m_aToUpdate[$sClass][$iId]['values'][$oAttDef->GetCode()] = $value;
  253. }
  254. }
  255. }
  256. ?>