module.attachments.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. 'en.dict.attachments.php',
  43. 'fr.dict.attachments.php',
  44. ),
  45. 'data.struct' => array(
  46. // add your 'structure' definition XML files here,
  47. ),
  48. 'data.sample' => array(
  49. // add your sample data XML files here,
  50. ),
  51. // Documentation
  52. //
  53. 'doc.manual_setup' => '', // hyperlink to manual setup documentation, if any
  54. 'doc.more_information' => '', // hyperlink to more information, if any
  55. // Default settings
  56. //
  57. 'settings' => array(
  58. 'allowed_classes' => array('Ticket'), // List of classes for which to manage "Attachments"
  59. 'position' => 'relations', // Where to display the attachments: relations | properties
  60. ),
  61. )
  62. );
  63. if (!class_exists('AttachmentInstaller'))
  64. {
  65. // Module installation handler
  66. //
  67. class AttachmentInstaller extends ModuleInstallerAPI
  68. {
  69. public static function BeforeWritingConfig(Config $oConfiguration)
  70. {
  71. // If you want to override/force some configuration values, do it here
  72. return $oConfiguration;
  73. }
  74. /**
  75. * Handler called before creating or upgrading the database schema
  76. * @param $oConfiguration Config The new configuration of the application
  77. * @param $sPreviousVersion string PRevious version number of the module (empty string in case of first install)
  78. * @param $sCurrentVersion string Current version number of the module
  79. */
  80. public static function BeforeDatabaseCreation(Config $oConfiguration, $sPreviousVersion, $sCurrentVersion)
  81. {
  82. // If you want to migrate data from one format to another, do it here
  83. }
  84. /**
  85. * Handler called after the creation/update of the database schema
  86. * @param $oConfiguration Config The new configuration of the application
  87. * @param $sPreviousVersion string PRevious version number of the module (empty string in case of first install)
  88. * @param $sCurrentVersion string Current version number of the module
  89. */
  90. public static function AfterDatabaseCreation(Config $oConfiguration, $sPreviousVersion, $sCurrentVersion)
  91. {
  92. // For each record having item_org_id unset,
  93. // get the org_id from the container object
  94. //
  95. // Prerequisite: change null into 0 (workaround to the fact that we cannot use IS NULL in OQL)
  96. SetupPage::log_info("Initializing attachment/item_org_id - null to zero");
  97. $sTableName = MetaModel::DBGetTable('Attachment');
  98. $sRepair = "UPDATE `$sTableName` SET `item_org_id` = 0 WHERE `item_org_id` IS NULL";
  99. CMDBSource::Query($sRepair);
  100. SetupPage::log_info("Initializing attachment/item_org_id - zero to the container");
  101. $oSearch = DBObjectSearch::FromOQL("SELECT Attachment WHERE item_org_id = 0");
  102. $oSet = new DBObjectSet($oSearch);
  103. $iUpdated = 0;
  104. while ($oAttachment = $oSet->Fetch())
  105. {
  106. $oContainer = MetaModel::GetObject($oAttachment->Get('item_class'), $oAttachment->Get('item_id'), false /* must be found */, true /* allow all data */);
  107. if ($oContainer)
  108. {
  109. $oAttachment->SetItem($oContainer, true /*updateonchange*/);
  110. $iUpdated++;
  111. }
  112. }
  113. SetupPage::log_info("Initializing attachment/item_org_id - $iUpdated records have been adjusted");
  114. }
  115. }
  116. }
  117. ?>