MDL-44079 Javascript: Closing a tooltip should pass event details
[moodle.git] / repository / alfresco / lib.php
blobec71506cf10957dd731c952b6f6489db5abd4f38
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * This plugin is used to access alfresco repository
20 * @since 2.0
21 * @package repository_alfresco
22 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once($CFG->dirroot . '/repository/lib.php');
27 /**
28 * repository_alfresco class
29 * This is a class used to browse files from alfresco
31 * @since 2.0
32 * @package repository_alfresco
33 * @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class repository_alfresco extends repository {
37 private $ticket = null;
38 private $user_session = null;
39 private $store = null;
40 private $alfresco;
42 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
43 global $SESSION, $CFG;
44 parent::__construct($repositoryid, $context, $options);
45 $this->sessname = 'alfresco_ticket_'.$this->id;
46 if (class_exists('SoapClient')) {
47 require_once($CFG->libdir . '/alfresco/Service/Repository.php');
48 require_once($CFG->libdir . '/alfresco/Service/Session.php');
49 require_once($CFG->libdir . '/alfresco/Service/SpacesStore.php');
50 require_once($CFG->libdir . '/alfresco/Service/Node.php');
51 // setup alfresco
52 $server_url = '';
53 if (!empty($this->options['alfresco_url'])) {
54 $server_url = $this->options['alfresco_url'];
55 } else {
56 return;
58 $this->alfresco = new Alfresco_Repository($this->options['alfresco_url']);
59 $this->username = optional_param('al_username', '', PARAM_RAW);
60 $this->password = optional_param('al_password', '', PARAM_RAW);
61 try{
62 // deal with user logging in
63 if (empty($SESSION->{$this->sessname}) && !empty($this->username) && !empty($this->password)) {
64 $this->ticket = $this->alfresco->authenticate($this->username, $this->password);
65 $SESSION->{$this->sessname} = $this->ticket;
66 } else {
67 if (!empty($SESSION->{$this->sessname})) {
68 $this->ticket = $SESSION->{$this->sessname};
71 $this->user_session = $this->alfresco->createSession($this->ticket);
72 $this->store = new SpacesStore($this->user_session);
73 } catch (Exception $e) {
74 $this->logout();
76 $this->current_node = null;
77 } else {
78 $this->disabled = true;
82 public function print_login() {
83 if ($this->options['ajax']) {
84 $user_field = new stdClass();
85 $user_field->label = get_string('username', 'repository_alfresco').': ';
86 $user_field->id = 'alfresco_username';
87 $user_field->type = 'text';
88 $user_field->name = 'al_username';
90 $passwd_field = new stdClass();
91 $passwd_field->label = get_string('password', 'repository_alfresco').': ';
92 $passwd_field->id = 'alfresco_password';
93 $passwd_field->type = 'password';
94 $passwd_field->name = 'al_password';
96 $ret = array();
97 $ret['login'] = array($user_field, $passwd_field);
98 return $ret;
99 } else {
100 echo '<table>';
101 echo '<tr><td><label>'.get_string('username', 'repository_alfresco').'</label></td>';
102 echo '<td><input type="text" name="al_username" /></td></tr>';
103 echo '<tr><td><label>'.get_string('password', 'repository_alfresco').'</label></td>';
104 echo '<td><input type="password" name="al_password" /></td></tr>';
105 echo '</table>';
106 echo '<input type="submit" value="Enter" />';
110 public function logout() {
111 global $SESSION;
112 unset($SESSION->{$this->sessname});
113 return $this->print_login();
116 public function check_login() {
117 global $SESSION;
118 return !empty($SESSION->{$this->sessname});
121 private function get_url($node) {
122 $result = null;
123 if ($node->type == "{http://www.alfresco.org/model/content/1.0}content") {
124 $contentData = $node->cm_content;
125 if ($contentData != null) {
126 $result = $contentData->getUrl();
128 } else {
129 $result = "index.php?".
130 "&uuid=".$node->id.
131 "&name=".$node->cm_name.
132 "&path=".'Company Home';
134 return $result;
138 * Get a file list from alfresco
140 * @param string $uuid a unique id of directory in alfresco
141 * @param string $path path to a directory
142 * @return array
144 public function get_listing($uuid = '', $path = '') {
145 global $CFG, $SESSION, $OUTPUT;
146 $ret = array();
147 $ret['dynload'] = true;
148 $ret['list'] = array();
149 $server_url = $this->options['alfresco_url'];
150 $pattern = '#^(.*)api#';
151 if ($return = preg_match($pattern, $server_url, $matches)) {
152 $ret['manage'] = $matches[1].'faces/jsp/dashboards/container.jsp';
155 $ret['path'] = array(array('name'=>get_string('pluginname', 'repository_alfresco'), 'path'=>''));
157 try {
158 if (empty($uuid)) {
159 $this->current_node = $this->store->companyHome;
160 } else {
161 $this->current_node = $this->user_session->getNode($this->store, $uuid);
164 $folder_filter = "{http://www.alfresco.org/model/content/1.0}folder";
165 $file_filter = "{http://www.alfresco.org/model/content/1.0}content";
167 // top level sites folder
168 $sites_filter = "{http://www.alfresco.org/model/site/1.0}sites";
169 // individual site
170 $site_filter = "{http://www.alfresco.org/model/site/1.0}site";
172 foreach ($this->current_node->children as $child)
174 if ($child->child->type == $folder_filter or
175 $child->child->type == $sites_filter or
176 $child->child->type == $site_filter)
178 $ret['list'][] = array('title'=>$child->child->cm_name,
179 'path'=>$child->child->id,
180 'thumbnail'=>$OUTPUT->pix_url(file_folder_icon(90))->out(false),
181 'children'=>array());
182 } elseif ($child->child->type == $file_filter) {
183 $ret['list'][] = array('title'=>$child->child->cm_name,
184 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($child->child->cm_name, 90))->out(false),
185 'source'=>$child->child->id);
188 } catch (Exception $e) {
189 unset($SESSION->{$this->sessname});
190 $ret = $this->print_login();
192 return $ret;
196 * Download a file from alfresco
198 * @param string $uuid a unique id of directory in alfresco
199 * @param string $path path to a directory
200 * @return array
202 public function get_file($uuid, $file = '') {
203 $node = $this->user_session->getNode($this->store, $uuid);
204 $url = $this->get_url($node);
205 return parent::get_file($url, $file);
209 * Return file URL
211 * @param string $url the url of file
212 * @return string
214 public function get_link($uuid) {
215 $node = $this->user_session->getNode($this->store, $uuid);
216 $url = $this->get_url($node);
217 return $url;
220 public function print_search() {
221 $str = parent::print_search();
222 $str .= html_writer::label(get_string('space', 'repository_alfresco'), 'space', false, array('class' => 'accesshide'));
223 $str .= html_writer::empty_tag('br');
224 $str .= '<select id="space" name="space">';
225 foreach ($this->user_session->stores as $v) {
226 $str .= '<option ';
227 if ($v->__toString() === 'workspace://SpacesStore') {
228 $str .= 'selected ';
230 $str .= 'value="';
231 $str .= $v->__toString().'">';
232 $str .= $v->__toString();
233 $str .= '</option>';
235 $str .= '</select>';
236 return $str;
240 * Look for a file
242 * @param string $search_text
243 * @return array
245 public function search($search_text, $page = 0) {
246 $space = optional_param('space', 'workspace://SpacesStore', PARAM_RAW);
247 $currentStore = $this->user_session->getStoreFromString($space);
248 $nodes = $this->user_session->query($currentStore, $search_text);
249 $ret = array();
250 $ret['list'] = array();
251 foreach($nodes as $v) {
252 $ret['list'][] = array('title'=>$v->cm_name, 'source'=>$v->id);
254 return $ret;
258 * Enable mulit-instance
260 * @return array
262 public static function get_instance_option_names() {
263 return array('alfresco_url');
267 * define a configuration form
269 * @return bool
271 public static function instance_config_form($mform) {
272 if (!class_exists('SoapClient')) {
273 $mform->addElement('static', null, get_string('notice'), get_string('soapmustbeenabled', 'repository_alfresco'));
274 return false;
276 $mform->addElement('text', 'alfresco_url', get_string('alfresco_url', 'repository_alfresco'), array('size' => '40'));
277 $mform->setType('alfresco_url', PARAM_URL);
278 $mform->addElement('static', 'alfreco_url_intro', '', get_string('alfrescourltext', 'repository_alfresco'));
279 $mform->addRule('alfresco_url', get_string('required'), 'required', null, 'client');
280 return true;
284 * Check if SOAP extension enabled
286 * @return bool
288 public static function plugin_init() {
289 if (!class_exists('SoapClient')) {
290 print_error('soapmustbeenabled', 'repository_alfresco');
291 return false;
292 } else {
293 return true;
296 public function supported_returntypes() {
297 return (FILE_INTERNAL | FILE_EXTERNAL);