module.attachments.php 4.3 KB

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