module.attachments.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. SetupWebPage::AddModule(
  19. __FILE__, // Path to the current file, all other file names are relative to the directory containing this file
  20. 'itop-attachments/1.0.0',
  21. array(
  22. // Identification
  23. //
  24. 'label' => 'Tickets attachments',
  25. 'category' => 'business',
  26. // Setup
  27. //
  28. 'dependencies' => array(
  29. ),
  30. 'mandatory' => false,
  31. 'visible' => true,
  32. 'installer' => 'AttachmentInstaller',
  33. // Components
  34. //
  35. 'datamodel' => array(
  36. 'model.itop-attachments.php',
  37. 'main.attachments.php',
  38. ),
  39. 'webservice' => array(
  40. ),
  41. 'dictionary' => array(
  42. ),
  43. 'data.struct' => array(
  44. // add your 'structure' definition XML files here,
  45. ),
  46. 'data.sample' => array(
  47. // add your sample data XML files here,
  48. ),
  49. // Documentation
  50. //
  51. 'doc.manual_setup' => '', // hyperlink to manual setup documentation, if any
  52. 'doc.more_information' => '', // hyperlink to more information, if any
  53. // Default settings
  54. //
  55. 'settings' => array(
  56. 'allowed_classes' => array('Ticket'), // List of classes for which to manage "Attachments"
  57. 'position' => 'relations', // Where to display the attachments: relations | properties
  58. ),
  59. )
  60. );
  61. if (!class_exists('AttachmentInstaller'))
  62. {
  63. // Module installation handler
  64. //
  65. class AttachmentInstaller extends ModuleInstallerAPI
  66. {
  67. public static function BeforeWritingConfig(Config $oConfiguration)
  68. {
  69. // If you want to override/force some configuration values, do it here
  70. return $oConfiguration;
  71. }
  72. /**
  73. * Handler called before creating or upgrading the database schema
  74. * @param $oConfiguration Config The new configuration of the application
  75. * @param $sPreviousVersion string PRevious version number of the module (empty string in case of first install)
  76. * @param $sCurrentVersion string Current version number of the module
  77. */
  78. public static function BeforeDatabaseCreation(Config $oConfiguration, $sPreviousVersion, $sCurrentVersion)
  79. {
  80. if ($sPreviousVersion != '')
  81. {
  82. // Migrating from a previous version
  83. // Check for records where item_id = '', since they are not attached to any object and cannot be migrated to the objkey schema
  84. $sTableName = MetaModel::DBGetTable('Attachment');
  85. $sCountQuery = "SELECT COUNT(*) FROM `$sTableName` WHERE (`item_id`='' OR `item_id` IS NULL)";
  86. $iCount = CMDBSource::QueryToScalar($sCountQuery);
  87. if ($iCount > 0)
  88. {
  89. SetupPage::log_info("Cleanup of orphan attachments that cannot be migrated to the new ObjKey model: $iCount record(s) must be deleted.");
  90. $sRepairQuery = "DELETE FROM `$sTableName` WHERE (`item_id`='' OR `item_id` IS NULL)";
  91. $iRet = CMDBSource::Query($sRepairQuery); // Throws an exception in case of error
  92. SetupPage::log_info("Cleanup of orphan attachments successfully completed.");
  93. }
  94. else
  95. {
  96. SetupPage::log_info("No orphan attachment found.");
  97. }
  98. }
  99. }
  100. /**
  101. * Handler called after the creation/update of the database schema
  102. * @param $oConfiguration Config The new configuration of the application
  103. * @param $sPreviousVersion string PRevious version number of the module (empty string in case of first install)
  104. * @param $sCurrentVersion string Current version number of the module
  105. */
  106. public static function AfterDatabaseCreation(Config $oConfiguration, $sPreviousVersion, $sCurrentVersion)
  107. {
  108. // For each record having item_org_id unset,
  109. // get the org_id from the container object
  110. //
  111. // Prerequisite: change null into 0 (workaround to the fact that we cannot use IS NULL in OQL)
  112. SetupPage::log_info("Initializing attachment/item_org_id - null to zero");
  113. $sTableName = MetaModel::DBGetTable('Attachment');
  114. $sRepair = "UPDATE `$sTableName` SET `item_org_id` = 0 WHERE `item_org_id` IS NULL";
  115. CMDBSource::Query($sRepair);
  116. SetupPage::log_info("Initializing attachment/item_org_id - zero to the container");
  117. $oSearch = DBObjectSearch::FromOQL("SELECT Attachment WHERE item_org_id = 0");
  118. $oSet = new DBObjectSet($oSearch);
  119. $iUpdated = 0;
  120. while ($oAttachment = $oSet->Fetch())
  121. {
  122. $oContainer = MetaModel::GetObject($oAttachment->Get('item_class'), $oAttachment->Get('item_id'), false /* must be found */, true /* allow all data */);
  123. if ($oContainer)
  124. {
  125. $oAttachment->SetItem($oContainer, true /*updateonchange*/);
  126. $iUpdated++;
  127. }
  128. }
  129. SetupPage::log_info("Initializing attachment/item_org_id - $iUpdated records have been adjusted");
  130. }
  131. }
  132. }
  133. ?>