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. ),
  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 you want to migrate data from one format to another, do it here
  82. }
  83. /**
  84. * Handler called after the creation/update of the database schema
  85. * @param $oConfiguration Config The new configuration of the application
  86. * @param $sPreviousVersion string PRevious version number of the module (empty string in case of first install)
  87. * @param $sCurrentVersion string Current version number of the module
  88. */
  89. public static function AfterDatabaseCreation(Config $oConfiguration, $sPreviousVersion, $sCurrentVersion)
  90. {
  91. // For each record having item_org_id unset,
  92. // get the org_id from the container object
  93. //
  94. // Prerequisite: change null into 0 (workaround to the fact that we cannot use IS NULL in OQL)
  95. SetupPage::log_info("Initializing attachment/item_org_id - null to zero");
  96. $sTableName = MetaModel::DBGetTable('Attachment');
  97. $sRepair = "UPDATE `$sTableName` SET `item_org_id` = 0 WHERE `item_org_id` IS NULL";
  98. CMDBSource::Query($sRepair);
  99. SetupPage::log_info("Initializing attachment/item_org_id - zero to the container");
  100. $oSearch = DBObjectSearch::FromOQL("SELECT Attachment WHERE item_org_id = 0");
  101. $oSet = new DBObjectSet($oSearch);
  102. $iUpdated = 0;
  103. while ($oAttachment = $oSet->Fetch())
  104. {
  105. $oContainer = MetaModel::GetObject($oAttachment->Get('item_class'), $oAttachment->Get('item_id'), false /* must be found */, true /* allow all data */);
  106. if ($oContainer)
  107. {
  108. $oAttachment->SetItem($oContainer, true /*updateonchange*/);
  109. $iUpdated++;
  110. }
  111. }
  112. SetupPage::log_info("Initializing attachment/item_org_id - $iUpdated records have been adjusted");
  113. }
  114. }
  115. }
  116. ?>