AbstractMigrationChamilo.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. namespace Application\Migrations;
  4. use Chamilo\CoreBundle\Entity\SettingsCurrent;
  5. use Chamilo\CoreBundle\Entity\SettingsOptions;
  6. use Doctrine\DBAL\Migrations\AbstractMigration;
  7. use Doctrine\ORM\EntityManager;
  8. /**
  9. * Class AbstractMigrationChamilo
  10. *
  11. * @package Chamilo\CoreBundle\Migrations
  12. */
  13. abstract class AbstractMigrationChamilo extends AbstractMigration
  14. {
  15. private $manager;
  16. /**
  17. * @param EntityManager $manager
  18. */
  19. public function setEntityManager(EntityManager $manager)
  20. {
  21. $this->manager = $manager;
  22. }
  23. /**
  24. * @return EntityManager
  25. */
  26. public function getEntityManager()
  27. {
  28. if (empty($this->manager)) {
  29. $dbParams = array(
  30. 'driver' => 'pdo_mysql',
  31. 'host' => $this->connection->getHost(),
  32. 'user' => $this->connection->getUsername(),
  33. 'password' => $this->connection->getPassword(),
  34. 'dbname' => $this->connection->getDatabase(),
  35. 'port' => $this->connection->getPort()
  36. );
  37. $database = new \Database();
  38. $database->connect(
  39. $dbParams,
  40. __DIR__.'/../../',
  41. __DIR__.'/../../'
  42. );
  43. $this->manager = $database->getManager();
  44. }
  45. return $this->manager;
  46. }
  47. /**
  48. * Speeds up SettingsCurrent creation
  49. * @param string $variable The variable itself
  50. * @param string $subKey The subkey
  51. * @param string $type The type of setting (text, radio, select, etc)
  52. * @param string $category The category (Platform, User, etc)
  53. * @param string $selectedValue The default value
  54. * @param string $title The setting title string name
  55. * @param string $comment The setting comment string name
  56. * @param string $scope The scope
  57. * @param string $subKeyText Text if there is a subKey
  58. * @param int $accessUrl What URL it is for
  59. * @param bool $accessUrlChangeable Whether it can be changed on each url
  60. * @param bool $accessUrlLocked Whether the setting for the current URL is
  61. * locked to the current value
  62. * @param array $options Optional array in case of a radio-type field,
  63. * to insert options
  64. */
  65. public function addSettingCurrent(
  66. $variable,
  67. $subKey,
  68. $type,
  69. $category,
  70. $selectedValue,
  71. $title,
  72. $comment,
  73. $scope = '',
  74. $subKeyText = '',
  75. $accessUrl = 1,
  76. $accessUrlChangeable = false,
  77. $accessUrlLocked = true,
  78. $options = array()
  79. ) {
  80. $setting = new SettingsCurrent();
  81. $setting
  82. ->setVariable($variable)
  83. ->setSubkey($subKey)
  84. ->setType($type)
  85. ->setCategory($category)
  86. ->setSelectedValue($selectedValue)
  87. ->setTitle($title)
  88. ->setComment($comment)
  89. ->setScope($scope)
  90. ->setSubkeytext($subKeyText)
  91. ->setAccessUrl($accessUrl)
  92. ->setAccessUrlChangeable($accessUrlChangeable)
  93. ->setAccessUrlLocked($accessUrlLocked);
  94. $this->getEntityManager()->persist($setting);
  95. if (count($options) > 0) {
  96. foreach ($options as $option) {
  97. if (empty($option['text'])) {
  98. if ($option['value'] == 'true') {
  99. $option['text'] = 'Yes';
  100. } else {
  101. $option['text'] = 'No';
  102. }
  103. }
  104. $settingOption = new SettingsOptions();
  105. $settingOption
  106. ->setVariable($variable)
  107. ->setValue($option['value'])
  108. ->setDisplayText($option['text']);
  109. $this->getEntityManager()->persist($settingOption);
  110. }
  111. }
  112. $this->getEntityManager()->flush();
  113. }
  114. /**
  115. * @param string $variable
  116. * @return mixed
  117. */
  118. public function getConfigurationValue($variable)
  119. {
  120. global $_configuration;
  121. if (isset($_configuration[$variable])) {
  122. return $_configuration[$variable];
  123. }
  124. return false;
  125. }
  126. /**
  127. * Remove a setting completely
  128. * @param string $variable The setting variable name
  129. * @return void
  130. */
  131. public function removeSettingCurrent($variable)
  132. {
  133. //to be implemented
  134. }
  135. }