module.attachments.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/2.3.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. 'preview_max_width' => 290,
  59. ),
  60. )
  61. );
  62. if (!class_exists('AttachmentInstaller'))
  63. {
  64. // Module installation handler
  65. //
  66. class AttachmentInstaller extends ModuleInstallerAPI
  67. {
  68. public static function BeforeWritingConfig(Config $oConfiguration)
  69. {
  70. // If you want to override/force some configuration values, do it here
  71. return $oConfiguration;
  72. }
  73. /**
  74. * Handler called before creating or upgrading the database schema
  75. * @param $oConfiguration Config The new configuration of the application
  76. * @param $sPreviousVersion string PRevious version number of the module (empty string in case of first install)
  77. * @param $sCurrentVersion string Current version number of the module
  78. */
  79. public static function BeforeDatabaseCreation(Config $oConfiguration, $sPreviousVersion, $sCurrentVersion)
  80. {
  81. if ($sPreviousVersion != '')
  82. {
  83. // Migrating from a previous version
  84. // Check for records where item_id = '', since they are not attached to any object and cannot be migrated to the objkey schema
  85. $sTableName = MetaModel::DBGetTable('Attachment');
  86. $sCountQuery = "SELECT COUNT(*) FROM `$sTableName` WHERE (`item_id`='' OR `item_id` IS NULL)";
  87. $iCount = CMDBSource::QueryToScalar($sCountQuery);
  88. if ($iCount > 0)
  89. {
  90. SetupPage::log_info("Cleanup of orphan attachments that cannot be migrated to the new ObjKey model: $iCount record(s) must be deleted.");
  91. $sRepairQuery = "DELETE FROM `$sTableName` WHERE (`item_id`='' OR `item_id` IS NULL)";
  92. $iRet = CMDBSource::Query($sRepairQuery); // Throws an exception in case of error
  93. SetupPage::log_info("Cleanup of orphan attachments successfully completed.");
  94. }
  95. else
  96. {
  97. SetupPage::log_info("No orphan attachment found.");
  98. }
  99. }
  100. }
  101. /**
  102. * Handler called after the creation/update of the database schema
  103. * @param $oConfiguration Config The new configuration of the application
  104. * @param $sPreviousVersion string PRevious version number of the module (empty string in case of first install)
  105. * @param $sCurrentVersion string Current version number of the module
  106. */
  107. public static function AfterDatabaseCreation(Config $oConfiguration, $sPreviousVersion, $sCurrentVersion)
  108. {
  109. // For each record having item_org_id unset,
  110. // get the org_id from the container object
  111. //
  112. // Prerequisite: change null into 0 (workaround to the fact that we cannot use IS NULL in OQL)
  113. SetupPage::log_info("Initializing attachment/item_org_id - null to zero");
  114. $sTableName = MetaModel::DBGetTable('Attachment');
  115. $sRepair = "UPDATE `$sTableName` SET `item_org_id` = 0 WHERE `item_org_id` IS NULL";
  116. CMDBSource::Query($sRepair);
  117. SetupPage::log_info("Initializing attachment/item_org_id - zero to the container");
  118. $oSearch = DBObjectSearch::FromOQL("SELECT Attachment WHERE item_org_id = 0");
  119. $oSet = new DBObjectSet($oSearch);
  120. $iUpdated = 0;
  121. while ($oAttachment = $oSet->Fetch())
  122. {
  123. $oContainer = MetaModel::GetObject($oAttachment->Get('item_class'), $oAttachment->Get('item_id'), false /* must be found */, true /* allow all data */);
  124. if ($oContainer)
  125. {
  126. $oAttachment->SetItem($oContainer, true /*updateonchange*/);
  127. $iUpdated++;
  128. }
  129. }
  130. SetupPage::log_info("Initializing attachment/item_org_id - $iUpdated records have been adjusted");
  131. }
  132. }
  133. }
  134. ?>