services_edit.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /* For license terms, see /license.txt */
  3. /**
  4. * Create new Services for the Buy Courses plugin
  5. * @package chamilo.plugin.buycourses
  6. */
  7. /**
  8. * Init
  9. */
  10. $cidReset = true;
  11. require_once '../../../main/inc/global.inc.php';
  12. $serviceId = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : null;
  13. if (!$serviceId) {
  14. header('Location: configuration.php');
  15. }
  16. $plugin = BuyCoursesPlugin::create();
  17. $currency = $plugin->getSelectedCurrency();
  18. $em = Database::getManager();
  19. $users = $em->getRepository('ChamiloUserBundle:User')->findAll();
  20. $userOptions = [];
  21. if (!empty($users)) {
  22. foreach ($users as $user) {
  23. $userOptions[$user->getId()] = $user->getCompleteNameWithUsername();
  24. }
  25. }
  26. api_protect_admin_script(true);
  27. $htmlHeadXtra[] = api_get_css_asset('cropper/dist/cropper.min.css');
  28. $htmlHeadXtra[] = api_get_asset('cropper/dist/cropper.min.js');
  29. //view
  30. $interbreadcrumb[] = [
  31. 'url' => 'configuration.php',
  32. 'name' => $plugin->get_lang('Configuration')
  33. ];
  34. $service = $plugin->getServices($serviceId);
  35. $formDefaultValues = [
  36. 'name' => $service['name'],
  37. 'description' => $service['description'],
  38. 'price' => $service['price'],
  39. 'duration_days' => $service['duration_days'],
  40. 'owner_id' => intval($service['owner_id']),
  41. 'applies_to' => intval($service['applies_to']),
  42. 'visibility' => ($service['visibility'] == 1) ? true : false,
  43. 'image' =>
  44. is_file(api_get_path(SYS_PLUGIN_PATH).'buycourses/uploads/services/images/simg-'.$serviceId.'.png')
  45. ?
  46. api_get_path(WEB_PLUGIN_PATH).'buycourses/uploads/services/images/simg-'.$serviceId.'.png'
  47. :
  48. api_get_path(WEB_CODE_PATH).'img/session_default.png',
  49. 'video_url' => $service['video_url'],
  50. 'service_information' => $service['service_information']
  51. ];
  52. $form = new FormValidator('Services');
  53. $form->addText('name', $plugin->get_lang('ServiceName'));
  54. $form->addTextarea('description', $plugin->get_lang('Description'));
  55. $form->addElement(
  56. 'number',
  57. 'price',
  58. [$plugin->get_lang('Price'), null, $currency['iso_code']],
  59. ['step' => 0.01]
  60. );
  61. $form->addElement(
  62. 'number',
  63. 'duration_days',
  64. [$plugin->get_lang('Duration'), null, get_lang('Days')],
  65. ['step' => 1]
  66. );
  67. $form->addElement(
  68. 'radio',
  69. 'applies_to',
  70. $plugin->get_lang('AppliesTo'),
  71. get_lang('None'),
  72. 0
  73. );
  74. $form->addElement(
  75. 'radio',
  76. 'applies_to',
  77. null,
  78. get_lang('User'),
  79. 1
  80. );
  81. $form->addElement(
  82. 'radio',
  83. 'applies_to',
  84. null,
  85. get_lang('Course'),
  86. 2
  87. );
  88. $form->addElement(
  89. 'radio',
  90. 'applies_to',
  91. null,
  92. get_lang('Session'),
  93. 3
  94. );
  95. $form->addSelect(
  96. 'owner_id',
  97. get_lang('Owner'),
  98. $userOptions
  99. );
  100. $form->addCheckBox('visibility', $plugin->get_lang('VisibleInCatalog'));
  101. $form->addFile(
  102. 'picture',
  103. ($formDefaultValues['image'] != '' ? get_lang('UpdateImage') : get_lang(
  104. 'AddImage'
  105. )),
  106. array('id' => 'picture', 'class' => 'picture-form', 'crop_image' => true, 'crop_ratio' => '16 / 9')
  107. );
  108. $form->addText('video_url', get_lang('VideoUrl'), false);
  109. $form->addHtmlEditor('service_information', $plugin->get_lang('ServiceInformation'), false);
  110. $form->addHidden('id', $serviceId);
  111. $form->addButtonSave(get_lang('Edit'));
  112. $form->setDefaults($formDefaultValues);
  113. if ($form->validate()) {
  114. $values = $form->getSubmitValues();
  115. $plugin->updateService($values, $serviceId);
  116. Display::addFlash(
  117. Display::return_message($plugin->get_lang('ServiceEdited'), 'success')
  118. );
  119. header('Location: configuration.php');
  120. exit;
  121. }
  122. $templateName = $plugin->get_lang('EditService');
  123. $tpl = new Template($templateName);
  124. $tpl->assign('header', $templateName);
  125. $tpl->assign('content', $form->returnForm());
  126. $tpl->display_one_col_template();