module.attachments.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. SetupWebPage::AddModule(
  17. __FILE__, // Path to the current file, all other file names are relative to the directory containing this file
  18. 'itop-attachments/1.0.0',
  19. array(
  20. // Identification
  21. //
  22. 'label' => 'Tickets attachments',
  23. 'category' => 'business',
  24. // Setup
  25. //
  26. 'dependencies' => array(
  27. ),
  28. 'mandatory' => false,
  29. 'visible' => true,
  30. 'installer' => 'AttachmentInstaller',
  31. // Components
  32. //
  33. 'datamodel' => array(
  34. 'model.itop-attachments.php',
  35. 'main.attachments.php',
  36. ),
  37. 'webservice' => array(
  38. ),
  39. 'dictionary' => array(
  40. 'en.dict.attachments.php',
  41. 'fr.dict.attachments.php',
  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. // Module installation handler
  62. //
  63. class AttachmentInstaller extends ModuleInstallerAPI
  64. {
  65. public static function BeforeWritingConfig(Config $oConfiguration)
  66. {
  67. // If you want to override/force some configuration values, do it here
  68. return $oConfiguration;
  69. }
  70. /**
  71. * Handler called before creating or upgrading the database schema
  72. * @param $oConfiguration Config The new configuration of the application
  73. * @param $sPreviousVersion string PRevious version number of the module (empty string in case of first install)
  74. * @param $sCurrentVersion string Current version number of the module
  75. */
  76. public static function BeforeDatabaseCreation(Config $oConfiguration, $sPreviousVersion, $sCurrentVersion)
  77. {
  78. // If you want to migrate data from one format to another, do it here
  79. }
  80. /**
  81. * Handler called after the creation/update of the database schema
  82. * @param $oConfiguration Config The new configuration of the application
  83. * @param $sPreviousVersion string PRevious version number of the module (empty string in case of first install)
  84. * @param $sCurrentVersion string Current version number of the module
  85. */
  86. public static function AfterDatabaseCreation(Config $oConfiguration, $sPreviousVersion, $sCurrentVersion)
  87. {
  88. // For each record having item_org_id unset,
  89. // get the org_id from the container object
  90. //
  91. // Prerequisite: change null into 0 (workaround to the fact that we cannot use IS NULL in OQL)
  92. SetupPage::log_info("Initializing attachment/item_org_id - null to zero");
  93. $sTableName = MetaModel::DBGetTable('Attachment');
  94. $sRepair = "UPDATE `$sTableName` SET `item_org_id` = 0 WHERE `item_org_id` IS NULL";
  95. CMDBSource::Query($sRepair);
  96. SetupPage::log_info("Initializing attachment/item_org_id - zero to the container");
  97. $oSearch = DBObjectSearch::FromOQL("SELECT Attachment WHERE item_org_id = 0");
  98. $oSet = new DBObjectSet($oSearch);
  99. $iUpdated = 0;
  100. while ($oAttachment = $oSet->Fetch())
  101. {
  102. $oContainer = MetaModel::GetObject($oAttachment->Get('item_class'), $oAttachment->Get('item_id'), false /* must be found */, true /* allow all data */);
  103. if ($oContainer)
  104. {
  105. $oAttachment->SetItem($oContainer, true /*updateonchange*/);
  106. $iUpdated++;
  107. }
  108. }
  109. SetupPage::log_info("Initializing attachment/item_org_id - $iUpdated records have been adjusted");
  110. }
  111. }
  112. ?>