editinstance_form.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class ChamiloForm.
  5. */
  6. abstract class ChamiloForm
  7. {
  8. public $_definition_finalized;
  9. protected $_form;
  10. protected $_mode;
  11. protected $_cancelurl;
  12. protected $_customdata;
  13. /**
  14. * ChamiloForm constructor.
  15. *
  16. * @param $mode
  17. * @param $returnurl
  18. * @param $cancelurl
  19. * @param $customdata
  20. */
  21. public function __construct($mode, $returnurl, $cancelurl, $customdata = [])
  22. {
  23. global $text_dir;
  24. $this->_mode = $mode;
  25. $this->_cancelurl = $cancelurl;
  26. $this->_customdata = $customdata;
  27. $attributes = ['style' => 'width: 60%; float: '.($text_dir == 'rtl' ? 'right;' : 'left;')];
  28. $this->_form = new FormValidator(
  29. $mode.'_instance',
  30. 'post',
  31. $returnurl,
  32. '',
  33. $attributes
  34. );
  35. }
  36. abstract public function definition();
  37. abstract public function validation($data, $files = null);
  38. public function validate()
  39. {
  40. return $this->_form->validate();
  41. }
  42. public function display()
  43. {
  44. return $this->_form->display();
  45. }
  46. public function definition_after_data()
  47. {
  48. }
  49. public function return_form()
  50. {
  51. return $this->_form->toHtml();
  52. }
  53. public function is_in_add_mode()
  54. {
  55. return $this->_mode == 'add';
  56. }
  57. /**
  58. * Return submitted data if properly submitted or returns NULL if validation fails or
  59. * if there is no submitted data.
  60. *
  61. * @param bool $slashed true means return data with addslashes applied
  62. *
  63. * @return object submitted data; NULL if not valid or not submitted
  64. */
  65. public function get_data($slashed = true)
  66. {
  67. $cform = &$this->_form;
  68. if ($this->is_submitted() and $this->is_validated()) {
  69. $data = $cform->exportValues(null, $slashed);
  70. unset($data['sesskey']); // we do not need to return sesskey
  71. if (empty($data)) {
  72. return null;
  73. } else {
  74. return (object) $data;
  75. }
  76. } else {
  77. return null;
  78. }
  79. }
  80. /**
  81. * Return submitted data without validation or NULL if there is no submitted data.
  82. *
  83. * @param bool $slashed true means return data with addslashes applied
  84. *
  85. * @return object submitted data; NULL if not submitted
  86. */
  87. public function get_submitted_data($slashed = true)
  88. {
  89. $cform = &$this->_form;
  90. if ($this->is_submitted()) {
  91. $data = $cform->exportValues(null, $slashed);
  92. unset($data['sesskey']); // we do not need to return sesskey
  93. if (empty($data)) {
  94. return null;
  95. } else {
  96. return (object) $data;
  97. }
  98. } else {
  99. return null;
  100. }
  101. }
  102. /**
  103. * Check that form was submitted. Does not check validity of submitted data.
  104. *
  105. * @return bool true if form properly submitted
  106. */
  107. public function is_submitted()
  108. {
  109. return $this->_form->isSubmitted();
  110. }
  111. /**
  112. * Return true if a cancel button has been pressed resulting in the form being submitted.
  113. *
  114. * @return bool true if a cancel button has been pressed
  115. */
  116. public function is_cancelled()
  117. {
  118. $cform = &$this->_form;
  119. if ($cform->isSubmitted()) {
  120. foreach ($cform->_cancelButtons as $cancelbutton) {
  121. if (optional_param($cancelbutton, 0, PARAM_RAW)) {
  122. return true;
  123. }
  124. }
  125. }
  126. return false;
  127. }
  128. /**
  129. * Check that form data is valid.
  130. * You should almost always use this, rather than {@see validate_defined_fields}.
  131. *
  132. * @return bool true if form data valid
  133. */
  134. public function is_validated()
  135. {
  136. //finalize the form definition before any processing
  137. if (!$this->_definition_finalized) {
  138. $this->_definition_finalized = true;
  139. $this->definition_after_data();
  140. }
  141. return $this->validate_defined_fields();
  142. }
  143. /**
  144. * Validate the form.
  145. *
  146. * You almost always want to call {@see is_validated} instead of this
  147. * because it calls {@see definition_after_data} first, before validating the form,
  148. * which is what you want in 99% of cases.
  149. *
  150. * This is provided as a separate function for those special cases where
  151. * you want the form validated before definition_after_data is called
  152. * for example, to selectively add new elements depending on a no_submit_button press,
  153. * but only when the form is valid when the no_submit_button is pressed,
  154. *
  155. * @param bool $validateonnosubmit optional, defaults to false. The default behaviour
  156. * is NOT to validate the form when a no submit button has been pressed.
  157. * pass true here to override this behaviour
  158. *
  159. * @return bool true if form data valid
  160. */
  161. public function validate_defined_fields($validateonnosubmit = false)
  162. {
  163. static $validated = null; // one validation is enough
  164. $cform = &$this->_form;
  165. if ($this->no_submit_button_pressed() && empty($validateonnosubmit)) {
  166. return false;
  167. } elseif ($validated === null) {
  168. $internal_val = $cform->validate();
  169. $files = [];
  170. $file_val = $this->_validate_files($files);
  171. if ($file_val !== true) {
  172. if (!empty($file_val)) {
  173. foreach ($file_val as $element => $msg) {
  174. $cform->setElementError($element, $msg);
  175. }
  176. }
  177. $file_val = false;
  178. }
  179. $data = $cform->exportValues(null, true);
  180. $chamilo_val = $this->validation($data, $files);
  181. if ((is_array($chamilo_val) && count($chamilo_val) !== 0)) {
  182. // non-empty array means errors
  183. foreach ($chamilo_val as $element => $msg) {
  184. $cform->setElementError($element, $msg);
  185. }
  186. $chamilo_val = false;
  187. } else {
  188. // anything else means validation ok
  189. $chamilo_val = true;
  190. }
  191. $validated = ($internal_val && $chamilo_val && $file_val);
  192. }
  193. return $validated;
  194. }
  195. public function no_submit_button_pressed()
  196. {
  197. static $nosubmit = null; // one check is enough
  198. if (!is_null($nosubmit)) {
  199. return $nosubmit;
  200. }
  201. $cform = &$this->_form;
  202. $nosubmit = false;
  203. if (!$this->is_submitted()) {
  204. return false;
  205. }
  206. /*
  207. foreach ($cform->_noSubmitButtons as $nosubmitbutton){
  208. if (optional_param($nosubmitbutton, 0, PARAM_RAW)){
  209. $nosubmit = true;
  210. break;
  211. }
  212. }
  213. return $nosubmit;
  214. */
  215. return false;
  216. }
  217. /**
  218. * Load in existing data as form defaults. Usually new entry defaults are stored directly in
  219. * form definition (new entry form); this function is used to load in data where values
  220. * already exist and data is being edited (edit entry form).
  221. *
  222. * @param mixed $default_values object or array of default values
  223. * @param bool $slashed true if magic quotes applied to data values
  224. */
  225. public function set_data($default_values, $slashed = false)
  226. {
  227. if (is_object($default_values)) {
  228. $default_values = (array) $default_values;
  229. }
  230. $filter = $slashed ? 'stripslashes' : null;
  231. $this->_form->setDefaults($default_values, $filter);
  232. }
  233. /**
  234. * Internal method. Validates all uploaded files.
  235. */
  236. public function _validate_files(&$files)
  237. {
  238. $files = [];
  239. if (empty($_FILES)) {
  240. // we do not need to do any checks because no files were submitted
  241. // note: server side rules do not work for files - use custom verification in validate() instead
  242. return true;
  243. }
  244. $errors = [];
  245. $mform = &$this->_form;
  246. // check the files
  247. $status = $this->_upload_manager->preprocess_files();
  248. // now check that we really want each file
  249. foreach ($_FILES as $elname => $file) {
  250. if ($mform->elementExists($elname) and $mform->getElementType($elname) == 'file') {
  251. $required = $mform->isElementRequired($elname);
  252. if (!empty($this->_upload_manager->files[$elname]['uploadlog']) &&
  253. empty($this->_upload_manager->files[$elname]['clear'])
  254. ) {
  255. if (!$required and $file['error'] == UPLOAD_ERR_NO_FILE) {
  256. // file not uploaded and not required - ignore it
  257. continue;
  258. }
  259. $errors[$elname] = $this->_upload_manager->files[$elname]['uploadlog'];
  260. } elseif (!empty($this->_upload_manager->files[$elname]['clear'])) {
  261. $files[$elname] = $this->_upload_manager->files[$elname]['tmp_name'];
  262. }
  263. } else {
  264. error('Incorrect upload attempt!');
  265. }
  266. }
  267. // return errors if found
  268. if ($status && 0 == count($errors)) {
  269. return true;
  270. } else {
  271. $files = [];
  272. return $errors;
  273. }
  274. }
  275. }
  276. /**
  277. * Class InstanceForm.
  278. */
  279. class InstanceForm extends ChamiloForm
  280. {
  281. /** @var Plugin */
  282. public $_plugin;
  283. public $instance;
  284. /**
  285. * InstanceForm constructor.
  286. *
  287. * @param $plugin
  288. * @param string $mode
  289. */
  290. public function __construct($plugin, $mode = 'add', $instance = [])
  291. {
  292. global $_configuration;
  293. $this->_plugin = $plugin;
  294. $returnUrl = $_configuration['root_web'].'plugin/vchamilo/views/editinstance.php';
  295. if ($mode == 'update') {
  296. $returnUrl = $_configuration['root_web'].'plugin/vchamilo/views/editinstance.php?vid='.intval($_GET['vid']);
  297. }
  298. $cancelurl = $_configuration['root_web'].'plugin/vchamilo/views/manage.php';
  299. parent::__construct($mode, $returnUrl, $cancelurl);
  300. $this->instance = $instance;
  301. $this->definition();
  302. }
  303. public function definition()
  304. {
  305. global $_configuration;
  306. $form = $this->_form;
  307. $plugin = $this->_plugin;
  308. $form->addElement('hidden', 'vid');
  309. $form->addElement('hidden', 'what', $this->_mode.'instance');
  310. $form->addElement('hidden', 'registeronly');
  311. $form->addHeader($plugin->get_lang('hostdefinition'));
  312. $form->addText(
  313. 'sitename',
  314. [
  315. $plugin->get_lang('sitename'),
  316. $plugin->get_lang('SiteNameExample'),
  317. ]
  318. );
  319. $form->applyFilter('sitename', 'trim');
  320. $form->addText(
  321. 'institution',
  322. [
  323. $plugin->get_lang('institution'),
  324. $plugin->get_lang('InstitutionExample'),
  325. ]
  326. );
  327. $form->applyFilter('institution', 'trim');
  328. // Host's name.
  329. $elementWeb = $form->addElement(
  330. 'text',
  331. 'root_web',
  332. [$this->_plugin->get_lang('rootweb'), $plugin->get_lang('RootWebExample')]
  333. );
  334. $form->applyFilter('root_web', 'trim');
  335. $form->addElement(
  336. 'text',
  337. 'url_append',
  338. ['url_append', $plugin->get_lang('UrlAppendExample')]
  339. );
  340. if ($this->_mode == 'update') {
  341. $encryptList = Virtual::getEncryptList();
  342. $encryptMethod = $form->addElement(
  343. 'select',
  344. 'password_encryption',
  345. get_lang('EncryptMethodUserPass'),
  346. $encryptList
  347. );
  348. $encryptMethod->freeze();
  349. $elementWeb->freeze();
  350. }
  351. /*
  352. * Database fieldset.
  353. */
  354. $form->addElement('header', $plugin->get_lang('dbgroup'));
  355. // Database host.
  356. $form->addElement(
  357. 'text',
  358. 'db_host',
  359. $this->_plugin->get_lang('dbhost'),
  360. ['id' => 'id_vdbhost']
  361. );
  362. $form->applyFilter('db_host', 'trim');
  363. // Database login.
  364. $form->addElement(
  365. 'text',
  366. 'db_user',
  367. $this->_plugin->get_lang('dbuser'),
  368. ['id' => 'id_vdbuser']
  369. );
  370. $form->applyFilter('db_user', 'trim');
  371. // Database password.
  372. $form->addElement(
  373. 'password',
  374. 'db_password',
  375. $this->_plugin->get_lang('dbpassword'),
  376. ['id' => 'id_vdbpassword']
  377. );
  378. // Database name.
  379. $form->addText(
  380. 'main_database',
  381. [
  382. $plugin->get_lang('maindatabase'),
  383. $plugin->get_lang('DatabaseDescription'),
  384. ]
  385. );
  386. // Button for testing database connection.
  387. $form->addElement(
  388. 'button',
  389. 'testconnection',
  390. $this->_plugin->get_lang('testconnection'),
  391. 'check',
  392. 'default',
  393. 'default',
  394. '',
  395. 'onclick="opencnxpopup(\''.$_configuration['root_web'].'\'); return false;"'
  396. );
  397. $form->addText('archive_url', $this->_plugin->get_lang('ArchiveUrl'));
  398. $form->addText('home_url', $this->_plugin->get_lang('HomeUrl'));
  399. $form->addText('upload_url', $this->_plugin->get_lang('UploadUrl'));
  400. $form->addText(
  401. 'css_theme_folder',
  402. [
  403. $this->_plugin->get_lang('ThemeFolder'),
  404. $this->_plugin->get_lang('ThemeFolderExplanation'),
  405. ],
  406. false
  407. );
  408. //$form->addText('course_url', $this->_plugin->get_lang('CourseUrl'));
  409. /**
  410. * Template selection.
  411. */
  412. if ($this->is_in_add_mode()) {
  413. $form->addElement('header', $this->_plugin->get_lang('templating'));
  414. $templateoptions = Virtual::getAvailableTemplates();
  415. // Template choice
  416. $form->addSelect(
  417. 'template',
  418. $this->_plugin->get_lang('template'),
  419. $templateoptions
  420. );
  421. } else {
  422. if ($this->instance) {
  423. $form->addLabel(
  424. 'slug',
  425. $this->instance['slug']
  426. );
  427. $form->addLabel(
  428. 'archive_real_root',
  429. api_add_trailing_slash(Virtual::getConfig('vchamilo', 'archive_real_root')).
  430. $this->instance['slug']
  431. );
  432. $form->addLabel(
  433. 'course_real_root',
  434. api_add_trailing_slash(Virtual::getConfig('vchamilo', 'course_real_root')).
  435. $this->instance['slug']
  436. );
  437. $form->addLabel(
  438. 'home_real_root',
  439. api_add_trailing_slash(Virtual::getConfig('vchamilo', 'home_real_root')).$this->instance['slug']
  440. );
  441. $form->addLabel(
  442. 'upload_real_root',
  443. api_add_trailing_slash(Virtual::getConfig('vchamilo', 'upload_real_root')).$this->instance['slug']
  444. );
  445. $form->addLabel(
  446. $this->_plugin->get_lang('template'),
  447. $this->instance['template']
  448. );
  449. }
  450. }
  451. $form->addButtonSave(
  452. $this->_plugin->get_lang('savechanges'),
  453. 'submitbutton'
  454. );
  455. // Rules
  456. $form->addRule(
  457. 'sitename',
  458. $this->_plugin->get_lang('sitenameinputerror'),
  459. 'required',
  460. null,
  461. 'client'
  462. );
  463. $form->addRule(
  464. 'institution',
  465. $this->_plugin->get_lang('institutioninputerror'),
  466. 'required',
  467. null,
  468. 'client'
  469. );
  470. $form->addRule(
  471. 'root_web',
  472. $this->_plugin->get_lang('rootwebinputerror'),
  473. 'required',
  474. null,
  475. 'client'
  476. );
  477. $form->addRule(
  478. 'main_database',
  479. $this->_plugin->get_lang('databaseinputerror'),
  480. 'required',
  481. null,
  482. 'client'
  483. );
  484. }
  485. /**
  486. * @param array $data
  487. * @param null $files
  488. *
  489. * @return array
  490. */
  491. public function validation($data, $files = null)
  492. {
  493. global $plugin;
  494. $errors = [];
  495. $tablename = Database::get_main_table('vchamilo');
  496. $vchamilo = Database::select(
  497. '*',
  498. $tablename,
  499. ['where' => [' root_web = ? ' => [$data['root_web']]]],
  500. 'first'
  501. );
  502. if ($vchamilo && isset($data['vid']) && $data['vid'] != $vchamilo['id']) {
  503. $errors['root_web'] = $plugin->get_lang('RootWebExists');
  504. }
  505. if (!empty($errors)) {
  506. return $errors;
  507. }
  508. }
  509. }