MDL-40266 improve emulate_bound_params() for mysqli
[moodle.git] / blog / external_blog_edit_form.php
CommitLineData
cae83708 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18
19/**
20 * Moodleform for the user interface for managing external blog links.
21 *
22 * @package moodlecore
23 * @subpackage blog
24 * @copyright 2009 Nicolas Connault
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
bfebaf64
MD
28if (!defined('MOODLE_INTERNAL')) {
29 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
30}
31
cae83708 32require_once($CFG->libdir.'/formslib.php');
b57d3959 33
cae83708 34class blog_edit_external_form extends moodleform {
35 public function definition() {
36 global $CFG;
37
38 $mform =& $this->_form;
39
2650f44d 40 $mform->addElement('text', 'url', get_string('url', 'blog'), array('size' => 50));
b73d1ca4 41 $mform->addRule('url', get_string('emptyurl', 'blog'), 'required', null, 'client');
2650f44d 42 $mform->addHelpButton('url', 'url', 'blog');
b73d1ca4 43
2650f44d
DM
44 $mform->addElement('text', 'name', get_string('name', 'blog'));
45 $mform->addHelpButton('name', 'name', 'blog');
cae83708 46
2650f44d
DM
47 $mform->addElement('textarea', 'description', get_string('description', 'blog'), array('cols' => 50, 'rows' => 7));
48 $mform->addHelpButton('description', 'description', 'blog');
cae83708 49
cae83708 50 if (!empty($CFG->usetags)) {
b57d3959 51 $mform->addElement('text', 'filtertags', get_string('filtertags', 'blog'), array('size' => 50));
2650f44d 52 $mform->addHelpButton('filtertags', 'filtertags', 'blog');
b57d3959 53 $mform->addElement('text', 'autotags', get_string('autotags', 'blog'), array('size' => 50));
2650f44d 54 $mform->addHelpButton('autotags', 'autotags', 'blog');
cae83708 55 }
56
57 $this->add_action_buttons();
58
59 $mform->addElement('hidden', 'id');
60 $mform->setType('id', PARAM_INT);
61 $mform->setDefault('id', 0);
62
63 $mform->addElement('hidden', 'returnurl');
64 $mform->setType('returnurl', PARAM_URL);
65 $mform->setDefault('returnurl', 0);
66 }
67
68 /**
69 * Additional validation includes checking URL and tags
70 */
71 public function validation($data, $files) {
b57d3959
NC
72 global $CFG;
73
cae83708 74 $errors = parent::validation($data, $files);
75
b57d3959
NC
76 require_once($CFG->libdir . '/simplepie/moodle_simplepie.php');
77
78 $rssfile = new moodle_simplepie_file($data['url']);
79 $filetest = new SimplePie_Locator($rssfile);
80
81 if (!$filetest->is_feed($rssfile)) {
82 $errors['url'] = get_string('feedisinvalid', 'blog');
cae83708 83 } else {
e14de6f9 84 $rss = new moodle_simplepie($data['url']);
85 if (!$rss->init()) {
cae83708 86 $errors['url'] = get_string('emptyrssfeed', 'blog');
87 }
88 }
89
90 return $errors;
91 }
92
cae83708 93 public function definition_after_data() {
94 global $CFG, $COURSE;
95 $mform =& $this->_form;
96
97 $name = trim($mform->getElementValue('name'));
98 $description = trim($mform->getElementValue('description'));
99 $url = $mform->getElementValue('url');
100
101 if (empty($name) || empty($description)) {
e14de6f9 102 $rss = new moodle_simplepie($url);
cae83708 103
e14de6f9 104 if (empty($name) && $rss->get_title()) {
105 $mform->setDefault('name', $rss->get_title());
cae83708 106 }
107
e14de6f9 108 if (empty($description) && $rss->get_description()) {
109 $mform->setDefault('description', $rss->get_description());
cae83708 110 }
111 }
112
113 if ($id = $mform->getElementValue('id')) {
b57d3959
NC
114 $mform->setDefault('autotags', implode(',', tag_get_tags_array('blog_external', $id)));
115 $mform->freeze('url');
116 $mform->freeze('filtertags');
117 // TODO change the filtertags element to a multiple select, using the tags of the external blog
118 // Use $rss->get_channel_tags()
cae83708 119 }
120 }
121}