AbstractMigrationChamilo.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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' => api_get_configuration_value('db_host'),
  32. 'user' => api_get_configuration_value('db_user'),
  33. 'password' => api_get_configuration_value('db_password'),
  34. 'dbname' => api_get_configuration_value('main_database')
  35. );
  36. $database = new \Database();
  37. $database->connect(
  38. $dbParams,
  39. __DIR__.'/../../',
  40. __DIR__.'/../../'
  41. );
  42. $this->manager = $database->getManager();
  43. }
  44. return $this->manager;
  45. }
  46. /**
  47. * Speeds up SettingsCurrent creation
  48. * @param string $variable The variable itself
  49. * @param string $subKey The subkey
  50. * @param string $type The type of setting (text, radio, select, etc)
  51. * @param string $category The category (Platform, User, etc)
  52. * @param string $selectedValue The default value
  53. * @param string $title The setting title string name
  54. * @param string $comment The setting comment string name
  55. * @param string $scope The scope
  56. * @param string $subKeyText Text if there is a subKey
  57. * @param int $accessUrl What URL it is for
  58. * @param bool $accessUrlChangeable Whether it can be changed on each url
  59. * @param bool $accessUrlLocked Whether the setting for the current URL is
  60. * locked to the current value
  61. * @param array $options Optional array in case of a radio-type field,
  62. * to insert options
  63. */
  64. public function addSettingCurrent(
  65. $variable,
  66. $subKey,
  67. $type,
  68. $category,
  69. $selectedValue,
  70. $title,
  71. $comment,
  72. $scope = '',
  73. $subKeyText = '',
  74. $accessUrl = 1,
  75. $accessUrlChangeable = false,
  76. $accessUrlLocked = true,
  77. $options = array()
  78. ) {
  79. $setting = new SettingsCurrent();
  80. $setting
  81. ->setVariable($variable)
  82. ->setSubkey($subKey)
  83. ->setType($type)
  84. ->setCategory($category)
  85. ->setSelectedValue($selectedValue)
  86. ->setTitle($title)
  87. ->setComment($comment)
  88. ->setScope($scope)
  89. ->setSubkeytext($subKeyText)
  90. ->setAccessUrl($accessUrl)
  91. ->setAccessUrlChangeable($accessUrlChangeable)
  92. ->setAccessUrlLocked($accessUrlLocked);
  93. $this->getEntityManager()->persist($setting);
  94. //$this->getEntityManager()->flush();
  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. }