MDL-15402: remove has_multiple_instance(), API will know there is multiple instance...
[moodle.git] / repository / flickr / repository.class.php
CommitLineData
36ea9a6c 1<?php
2/**
6a391ebf 3 * repository_flickr class
539b3d13 4 * This plugin is used to access user's private flickr repository
36ea9a6c 5 *
539b3d13 6 * @author Dongsheng Cai <dongsheng@moodle.com>
7 * @version $Id$
36ea9a6c 8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9 */
10
539b3d13 11require_once($CFG->libdir.'/flickrlib.php');
6a391ebf 12
dc0976b8 13class repository_flickr extends repository {
6a391ebf 14 private $flickr;
15 public $photos;
19add4c0 16
dc0976b8 17 public function __construct($repositoryid, $context = SITEID, $options = array()) {
d68c527f 18 global $SESSION, $CFG;
19 $options['page'] = optional_param('p', 1, PARAM_INT);
20 parent::__construct($repositoryid, $context, $options);
21
22 $this->setting = 'flickr_';
23
24 $this->api_key = $this->get_option('api_key');
25 $this->secret = $this->get_option('secret');
26
27 $this->token = get_user_preferences($this->setting, '');
28 $this->nsid = get_user_preferences($this->setting.'_nsid', '');
29
30 $this->flickr = new phpFlickr($this->api_key, $this->secret, $this->token);
31
353d5cf3 32 $frob = optional_param('frob', '', PARAM_RAW);
33 if (empty($this->token) && !empty($frob)) {
34 $auth_info = $this->flickr->auth_getToken($frob);
35 $this->token = $auth_info['token'];
36 $this->nsid = $auth_info['user']['nsid'];
37 set_user_preference($this->setting, $auth_info['token']);
38 set_user_preference($this->setting.'_nsid', $auth_info['user']['nsid']);
d68c527f 39 }
40
41 }
dc0976b8 42 public function check_login() {
d68c527f 43 return !empty($this->token);
44 }
dc0976b8 45 public function logout() {
d68c527f 46 set_user_preference($this->setting, '');
47 set_user_preference($this->setting.'_nsid', '');
48 $this->token = '';
49 $this->nsid = '';
50 return $this->print_login();
51 }
dc0976b8 52 public function set_option($options = array()) {
27051e43 53 if (!empty($options['api_key'])) {
70d91d90 54 set_config('api_key', trim($options['api_key']), 'flickr');
27051e43 55 }
7fab56b7 56 if (!empty($options['secret'])) {
57 set_config('secret', trim($options['secret']), 'flickr');
58 }
27051e43 59 unset($options['api_key']);
7fab56b7 60 unset($options['secret']);
27051e43 61 $ret = parent::set_option($options);
62 return $ret;
63 }
64
dc0976b8 65 public function get_option($config = '') {
66 if ($config==='api_key') {
70d91d90 67 return trim(get_config('flickr', 'api_key'));
7fab56b7 68 } elseif ($config ==='secret') {
69 return trim(get_config('flickr', 'secret'));
27051e43 70 } else {
70d91d90 71 $options['api_key'] = trim(get_config('flickr', 'api_key'));
7fab56b7 72 $options['secret'] = trim(get_config('flickr', 'secret'));
27051e43 73 }
74 $options = parent::get_option($config);
75 return $options;
76 }
77
dc0976b8 78 public function global_search() {
8988c36b 79 if (empty($this->token)) {
455860ce 80 return false;
81 } else {
82 return true;
83 }
84 }
dc0976b8 85 public function print_login($ajax = true) {
353d5cf3 86 if ($ajax) {
d68c527f 87 $ret = array();
88 $popup_btn = new stdclass;
89 $popup_btn->type = 'popup';
90 $popup_btn->url = $this->flickr->auth();
91 $ret['login'] = array($popup_btn);
92 return $ret;
6a391ebf 93 }
36ea9a6c 94 }
353d5cf3 95 private function build_list($photos, $path = 1) {
96 $photos_url = $this->flickr->urls_getUserPhotos($this->nsid);
d05ef4a3 97 $ret = array();
98 $ret['manage'] = $photos_url;
99 $ret['list'] = array();
100 $ret['pages'] = $photos['pages'];
353d5cf3 101 if(is_int($path) && $path <= $ret['pages']) {
d05ef4a3 102 $ret['page'] = $path;
6a391ebf 103 } else {
d05ef4a3 104 $ret['page'] = 1;
6a391ebf 105 }
dc0976b8 106 if (!empty($photos['photo'])) {
8988c36b 107 foreach ($photos['photo'] as $p) {
353d5cf3 108 if(empty($p['title'])) {
8988c36b 109 $p['title'] = get_string('notitle', 'repository_flickr');
110 }
111 if (isset($p['originalformat'])) {
112 $format = $p['originalformat'];
113 } else {
114 $format = 'jpg';
115 }
353d5cf3 116 $ret['list'][] = array('title'=>$p['title'].'.'.$format,'source'=>$p['id'],
117 'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'),
118 'date'=>'', 'size'=>'unknown', 'url'=>$photos_url.$p['id']);
733cfd80 119 }
6a391ebf 120 }
353d5cf3 121 return $ret;
122 }
123 public function search($search_text) {
124 $photos = $this->flickr->photos_search(array(
125 'user_id'=>$this->nsid,
126 'per_page'=>25,
127 'extras'=>'original_format',
128 'text'=>$search_text
129 ));
130 return $this->build_list($photos);
131 }
132 public function get_listing($path = '1') {
133 $photos_url = $this->flickr->urls_getUserPhotos($this->nsid);
134
135 $photos = $this->flickr->photos_search(array(
136 'user_id'=>$this->nsid,
137 'per_page'=>25,
138 'page'=>$path,
139 'extras'=>'original_format'
140 ));
141 return $this->build_list($photos, $path);
36ea9a6c 142 }
dc0976b8 143 public function print_listing() {
539b3d13 144 return false;
36ea9a6c 145 }
dc0976b8 146 public function get_file($photo_id, $file = '') {
6a391ebf 147 global $CFG;
148 $result = $this->flickr->photos_getSizes($photo_id);
149 $url = '';
353d5cf3 150 if(!empty($result[4])) {
6a391ebf 151 $url = $result[4]['source'];
353d5cf3 152 } elseif(!empty($result[3])) {
6a391ebf 153 $url = $result[3]['source'];
353d5cf3 154 } elseif(!empty($result[2])) {
6a391ebf 155 $url = $result[2]['source'];
156 }
157 if (!file_exists($CFG->dataroot.'/repository/download')) {
158 mkdir($CFG->dataroot.'/repository/download/', 0777, true);
159 }
160 if(is_dir($CFG->dataroot.'/repository/download')) {
161 $dir = $CFG->dataroot.'/repository/download/';
162 }
163
353d5cf3 164 if(empty($file)) {
6a391ebf 165 $file = $photo_id.'_'.time().'.jpg';
166 }
353d5cf3 167 if(file_exists($dir.$file)) {
6a391ebf 168 $file = uniqid('m').$file;
169 }
170 $fp = fopen($dir.$file, 'w');
171 $c = new curl;
172 $c->download(array(
173 array('url'=>$url, 'file'=>$fp)
174 ));
175 return $dir.$file;
176 }
a6600395 177
daff8f50 178 public function type_config_form(&$mform) {
7fab56b7 179 global $CFG;
336bb44b 180 $api_key = get_config('flickr', 'api_key');
7892948d 181 $secret = get_config('flickr', 'secret');
182
336bb44b 183 if (empty($api_key)) {
184 $api_key = '';
185 }
0868f619 186 if (empty($secret)) {
187 $secret = '';
188 }
7892948d 189
4a65c39a 190 $strrequired = get_string('required');
0868f619 191 $mform->addElement('text', 'api_key', get_string('apikey', 'repository_flickr'), array('value'=>$api_key,'size' => '40'));
192 $mform->addElement('text', 'secret', get_string('secret', 'repository_flickr'), array('value'=>$secret,'size' => '40'));
e49eaacb 193
194 //retrieve the flickr instances
195 $instances = repository_get_instances(array(),null,false,"flickr");
196 if (empty($instances)) {
197 $callbackurl = get_string("callbackwarning","repository_flickr");
198 $mform->addElement('static', null, '', $callbackurl);
199 }
200 else {
201 $callbackurl = $CFG->wwwroot.'/repository/ws.php?callback=yes&amp;repo_id='.$instances[0]->id;
202 $mform->addElement('static', 'callbackurl', '', get_string('callbackurltext', 'repository_flickr', $callbackurl));
203 }
204
4a65c39a 205 $mform->addRule('api_key', $strrequired, 'required', null, 'client');
0868f619 206 $mform->addRule('secret', $strrequired, 'required', null, 'client');
4a65c39a 207 }
0868f619 208
1b79955a 209 public static function get_type_option_names() {
7fab56b7 210 return array('api_key', 'secret');
4a65c39a 211 }
a6600395 212
36ea9a6c 213}