PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/core/krevisions.class.php

http://awarenet.googlecode.com/
PHP | 221 lines | 113 code | 37 blank | 71 comment | 37 complexity | 054ad554c7ef229b3f5c560a68217fd6 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?
  2. require_once($kapenta->installPath . 'modules/revisions/models/deleted.mod.php');
  3. require_once($kapenta->installPath . 'modules/revisions/models/revision.mod.php');
  4. //--------------------------------------------------------------------------------------------------
  5. //* interface to revisioning system
  6. //--------------------------------------------------------------------------------------------------
  7. //+ This component keeps track of changes to module objects and archives them after deletion from
  8. //+ the live system. Note that revision control is performed according to the 'nodiff' array
  9. //+ of model's dbSchema, and that objects marked 'archive' => 'no' are not kept.
  10. //+
  11. //+ New deletions of objects create new Revisions_Deleted objects which record the final state of
  12. //+ the deleted item, witht he status set to delete. There may thus be a set of Revisions_Deleted
  13. //+ objects for any item in the live database.
  14. //+
  15. //+ If an object is restored to the live database, the status of that Revisions_Deleted object is
  16. //+ set to 'restore'. Thus, if any of the set of Revisions_Deleted objects for some item have
  17. //+ status 'restore' then the object is NOT currently deleted. Any later re-deletions should reset
  18. //+ the 'restore' status to 'deleted', and update the Revisions_Deleted object.
  19. //+
  20. //+ Since dependant obejcts are usually deleted along with their owner (eg, comments and images on
  21. //+ a deleted blog post), restoring the owner will also restore dependant objects.
  22. class KRevisions {
  23. //----------------------------------------------------------------------------------------------
  24. // member variables
  25. //----------------------------------------------------------------------------------------------
  26. // none yet
  27. //----------------------------------------------------------------------------------------------
  28. //. constructor
  29. //----------------------------------------------------------------------------------------------
  30. function KRevisions() {
  31. // nothing at present, may in future read settings controlling behavior of this object
  32. }
  33. //----------------------------------------------------------------------------------------------
  34. //. record changes to an object
  35. //----------------------------------------------------------------------------------------------
  36. //arg: changes - associative array of fields and values [array]
  37. //arg: dbSchema - a database table definition [array]
  38. //arg: UID - UID of the object to be stored [string]
  39. //returns: true on success, false on failure [bool]
  40. function storeRevision($changes, $dbSchema, $UID) {
  41. global $db;
  42. if (false == $db->checkSchema($dbSchema)) { return false; }
  43. if ((true == array_key_exists('archive', $dbSchema)) && ('no' == $dbSchema['archive']))
  44. { return false; }
  45. $model = new Revisions_Revision();
  46. $model->refModule = $dbSchema['module'];
  47. $model->refModel = $dbSchema['model'];
  48. $model->refUID = $UID;
  49. $model->fields = $changes;
  50. $report = $model->save();
  51. if ('' != $report) { return false; }
  52. return true;
  53. }
  54. //----------------------------------------------------------------------------------------------
  55. //. record deleteion of an object
  56. //----------------------------------------------------------------------------------------------
  57. //arg: changes - associative array of fields and values [array]
  58. //arg: dbSchema - a database table definition [array]
  59. //returns: true on success, false on failure [bool]
  60. function recordDeletion($fields, $dbSchema) {
  61. global $db, $session;
  62. //------------------------------------------------------------------------------------------
  63. // check table schema
  64. //------------------------------------------------------------------------------------------
  65. if (false == $db->checkSchema($dbSchema)) {
  66. $session->msgAdmin('<b>Error:</b> Bad schema', 'bad');
  67. return false;
  68. }
  69. //------------------------------------------------------------------------------------------
  70. // check whether objects of this type are not archived
  71. //------------------------------------------------------------------------------------------
  72. if ((true == array_key_exists('archive', $dbSchema)) && ('no' == $dbSchema['archive'])) {
  73. $session->msgAdmin('Objects of this type are not archived or revisioned.');
  74. return false;
  75. }
  76. //------------------------------------------------------------------------------------------
  77. // check whether object has already been deleted
  78. //------------------------------------------------------------------------------------------
  79. if (true == $this->isDeleted($dbSchema['model'], $dbSchema['fields']['UID'])) {
  80. $session->msgAdmin('Object is already deleted.', 'bad');
  81. return false;
  82. }
  83. //------------------------------------------------------------------------------------------
  84. // create new Revisions_Deleted object
  85. //------------------------------------------------------------------------------------------
  86. $model = new Revisions_Deleted();
  87. $model->refModule = $dbSchema['module'];
  88. $model->refModel = $dbSchema['model'];
  89. $model->refUID = $fields['UID'];
  90. $model->fields = $fields;
  91. $model->shared = 'yes';
  92. if (false == $db->isShared($dbSchema['model'], $fields['UID'])) { $model->shared = 'no'; }
  93. $report = $model->save();
  94. if ('' != $report) {
  95. $msg = 'Could not move '. $model->refModel .'::'. $model->refUID .' to recycle bin: ';
  96. $session->msgAdmin($msg . $report, 'bad');
  97. return false;
  98. } else {
  99. $msg = 'Moved ' . $model->refModule . '::' . $model->refUID . ' to the recycle bin.';
  100. $session->msgAdmin($msg, 'ok');
  101. }
  102. //------------------------------------------------------------------------------------------
  103. // undo any previous restore of this item
  104. //------------------------------------------------------------------------------------------
  105. $conditions = array();
  106. $conditions[] = "refModel='" . $db->addMarkup($model->refModel) . "'";
  107. $conditions[] = "refUID='" . $db->addMarkup($model->refUID) . "'";
  108. $range = $db->loadRange('revisions_deleted', '*', $conditions);
  109. foreach($range as $item) {
  110. if ('restore' == $item['status']) {
  111. $deletion = new Revisions_Deletion($item['UID']);
  112. $deletion->status = 'delete';
  113. $report = $deletion->save();
  114. if ('' != $report) { $session->msg('Could not re-delete: ' . $report, 'bad'); }
  115. }
  116. }
  117. return true;
  118. }
  119. //----------------------------------------------------------------------------------------------
  120. //. discover if an object has been deleted
  121. //----------------------------------------------------------------------------------------------
  122. //arg: model - type of deleted object [string]
  123. //arg: UID - Unique identifier of deleted object [string]
  124. //returns: true if item is in recycle bin, false if not [bool]
  125. function isDeleted($model, $UID) {
  126. global $db;
  127. $retored = false;
  128. $conditions = array();
  129. $conditions[] = "refModel='" . $db->addMarkup($model) . "'";
  130. $conditions[] = "refUID='" . $db->addMarkup($UID) . "'";
  131. $range = $db->loadRange('revisions_deleted', '*', $conditions);
  132. if (0 == count($range)) { return false; } // no record of being deleted
  133. foreach($range as $item) {
  134. if ('restore' == $item['status']) { return false; } // deleted and restored
  135. }
  136. return false; // record of being deleted, but none of being undeleted
  137. }
  138. //----------------------------------------------------------------------------------------------
  139. //. revert the most recent deletion of an obejct
  140. //----------------------------------------------------------------------------------------------
  141. //arg: model - type of deleted object [string]
  142. //arg: UID - Unique identifier of deleted object [string]
  143. //returns: true on success, false on failure [string]
  144. //TODO: consider adding option to restore this item only (and not dependants)
  145. function undoLastDeletion($model, $UID) {
  146. global $db;
  147. if (false == $this->isDeleted($model, $UID)) { return false; }
  148. $conditions = array();
  149. $conditions[] = "refModel='" . $db->addMarkup($model) . "'";
  150. $conditions[] = "refUID='" . $db->addMarkup($UID) . "'";
  151. $range = $db->loadRange('revisions_deleted', '*', $conditions, 'createdOn DESC');
  152. foreach($range as $item) {
  153. $model = new Revisions_Deleted($item['UID']);
  154. $check = $model->restore();
  155. if (true == $check) {
  156. $this->restoreDependant($model, $UID);
  157. }
  158. return $check;
  159. }
  160. }
  161. //----------------------------------------------------------------------------------------------
  162. //. restore items belonging to some undeleted item
  163. //----------------------------------------------------------------------------------------------
  164. //returns: true on success, false on failure [bool]
  165. function restoreDependant($model, $UID) {
  166. global $db;
  167. $allOk = true;
  168. $conditions = array("owner='" . $db->addMarkup($UID) . "'");
  169. $range = $db->loadRange('revisions_deleted', '*', $conditions);
  170. foreach($range as $item) {
  171. $model = new Revisions_Deleted($item['UID']);
  172. if ($model->fields['refModel'] == $model) {
  173. $check = $model->restore();
  174. if (false == $check) {
  175. $msg = "Could not restore deleted item: " . $item['UID'];
  176. $session->msg($msg, 'bad');
  177. $allOk = false;
  178. }
  179. }
  180. }
  181. return $allOk;
  182. }
  183. }
  184. ?>