MDL-28117 include repo lib when hacking file_picker renderer
[moodle.git] / mod / data / field / url / field.class.php
1 <?php
2 ///////////////////////////////////////////////////////////////////////////
3 //                                                                       //
4 // NOTICE OF COPYRIGHT                                                   //
5 //                                                                       //
6 // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
7 //          http://moodle.org                                            //
8 //                                                                       //
9 // Copyright (C) 1999-onwards Moodle Pty Ltd  http://moodle.com          //
10 //                                                                       //
11 // This program is free software; you can redistribute it and/or modify  //
12 // it under the terms of the GNU General Public License as published by  //
13 // the Free Software Foundation; either version 2 of the License, or     //
14 // (at your option) any later version.                                   //
15 //                                                                       //
16 // This program is distributed in the hope that it will be useful,       //
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of        //
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
19 // GNU General Public License for more details:                          //
20 //                                                                       //
21 //          http://www.gnu.org/copyleft/gpl.html                         //
22 //                                                                       //
23 ///////////////////////////////////////////////////////////////////////////
25 class data_field_url extends data_field_base {
26     var $type = 'url';
28     function display_add_field($recordid=0) {
29         global $CFG, $DB, $OUTPUT, $PAGE;
31         require_once($CFG->dirroot. '/repository/lib.php'); // necessary for the constants used in args
33         $args = new stdClass();
34         $args->accepted_types = '*';
35         $args->return_types = FILE_EXTERNAL;
36         $args->context = $this->context;
37         $args->env = 'url';
38         $fp = new file_picker($args);
39         $options = $fp->options;
41         $fieldid = 'field_url_'.$options->client_id;
43         $straddlink = get_string('choosealink', 'repository');
44         $url = '';
45         $text = '';
46         if ($recordid) {
47             if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
48                 $url  = $content->content;
49                 $text = $content->content1;
50             }
51         }
52         $str = '<div title="'.s($this->field->description).'">';
53         if (!empty($this->field->param1) and empty($this->field->param2)) {
54             $str .= '<table><tr><td align="right">';
55             $str .= get_string('url','data').':</td><td><input type="text" name="field_'.$this->field->id.'_0" id="'.$fieldid.'" value="'.$url.'" size="60" /></td></tr>';
56             $str .= '<tr><td align="right">'.get_string('text','data').':</td><td><input type="text" name="field_'.$this->field->id.'_1" id="field_'.$this->field->id.'_1" value="'.s($text).'" size="60" /></td></tr>';
57             $str .= '</table>';
58         } else {
59             // Just the URL field
60             $str .= '<input type="text" name="field_'.$this->field->id.'_0" id="'.$fieldid.'" value="'.s($url).'" size="60" />';
61         }
63         $str .= '<button id="filepicker-button-'.$options->client_id.'" style="display:none">'.$straddlink.'</button>';
65         // print out file picker
66         //$str .= $OUTPUT->render($fp);
68         $module = array('name'=>'data_urlpicker', 'fullpath'=>'/mod/data/data.js', 'requires'=>array('core_filepicker'));
69         $PAGE->requires->js_init_call('M.data_urlpicker.init', array($options), true, $module);
70         $PAGE->requires->js_function_call('show_item', array('filepicker-button-'.$options->client_id));
72         $str .= '</div>';
73         return $str;
74     }
76     function display_search_field($value = '') {
77         return '<input type="text" size="16" name="f_'.$this->field->id.'" value="'.$value.'" />';
78     }
80     function parse_search_field() {
81         return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
82     }
84     function generate_sql($tablealias, $value) {
85         global $DB;
87         static $i=0;
88         $i++;
89         $name = "df_url_$i";
90         return array(" ({$tablealias}.fieldid = {$this->field->id} AND ".$DB->sql_like("{$tablealias}.content", ":$name", false).") ", array($name=>"%$value%"));
91     }
93     function display_browse_field($recordid, $template) {
94         global $DB;
96         if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
97             $url = empty($content->content)? '':$content->content;
98             $text = empty($content->content1)? '':$content->content1;
99             if (empty($url) or ($url == 'http://')) {
100                 return '';
101             }
102             if (!empty($this->field->param2)) {
103                 // param2 forces the text to something
104                 $text = $this->field->param2;
105             }
106             if ($this->field->param1) {
107                 // param1 defines whether we want to autolink the url.
108                 if (!empty($text)) {
109                     $str = '<a href="'.$url.'">'.$text.'</a>';
110                 } else {
111                     $str = '<a href="'.$url.'">'.$url.'</a>';
112                 }
113             } else {
114                 $str = $url;
115             }
116             return $str;
117         }
118         return false;
119     }
121     function update_content($recordid, $value, $name='') {
122         global $DB;
124         $content = new stdClass();
125         $content->fieldid = $this->field->id;
126         $content->recordid = $recordid;
127         $names = explode('_', $name);
129         switch ($names[2]) {
130             case 0:
131                 // update link
132                 $content->content = clean_param($value, PARAM_URL);
133                 break;
134             case 1:
135                 // add text
136                 $content->content1 = clean_param($value, PARAM_NOTAGS);
137                 break;
138             default:
139                 break;
140         }
142         if (!empty($content->content) && (strpos($content->content, '://') === false) && (strpos($content->content, '/', 0) === false)) {
143             $content->content = 'http://' . $content->content;
144         }
146         if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
147             $content->id = $oldcontent->id;
148             return $DB->update_record('data_content', $content);
149         } else {
150             return $DB->insert_record('data_content', $content);
151         }
152     }
154     function notemptyfield($value, $name) {
155         $names = explode('_',$name);
156         $value = clean_param($value, PARAM_URL);
157         //clean first
158         if ($names[2] == '0') {
159             return ($value!='http://' && !empty($value));
160         }
161         return false;
162     }
164     function export_text_value($record) {
165         return $record->content . " " . $record->content1;
166     }