"MDL-13766, repository settings page"
[moodle.git] / repository / flickr / repository.class.php
1 <?php
2 /**
3  * repository_flickr class
4  * This is a subclass of repository class
5  *
6  * @author Dongsheng Cai
7  * @version 0.1 dev
8  * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9  */
11 require_once($CFG->dirroot.'/repository/flickr/'.'phpFlickr.php');
13 class repository_flickr extends repository{
14     private $flickr;
15     public $photos;
16     public $type = 'flickr';
18     public function __construct($repositoryid, $context = SITEID, $options = array()){
19         global $SESSION, $action, $CFG;
20         $options['page']    = optional_param('p', 1, PARAM_INT);
21         $options['api_key'] = 'bf85ae2b5b105a2c645f32a32cd6ad59';
22         $options['secret']  = '7cb2f9d7cf70aebe';
23         parent::__construct($repositoryid, $context, $options);
24         $this->flickr = new phpFlickr($this->options['api_key'], $this->options['secret']);
26         $reset = optional_param('reset', 0, PARAM_INT);
27         if(!empty($reset)) {
28             // logout from flickr
29             unset($SESSION->flickrmail);
30             set_user_preference('flickrmail', '');
31         }
33         if(!empty($SESSION->flickrmail)) {
34             if(empty($action)) {
35                 $action = 'list';
36             }
37         } else {
38             // get flickr account
39             $options['flickrmail'] = optional_param('flickrmail', '', PARAM_RAW);
40             if(!empty($options['flickrmail'])) {
41                 $people = $this->flickr->people_findByEmail($options['flickrmail']);
42                 if(!empty($people)) {
43                     $remember = optional_param('remember', '', PARAM_RAW);
44                     if(!empty($remember)) {
45                         set_user_preference('flickrmail', $options['flickrmail']);
46                     }
47                     $SESSION->flickrmail = $options['flickrmail'];
48                     if(empty($action)) {
49                         $action = 'list';
50                     }
51                 } else {
52                     throw new repository_exception('invalidemail', 'repository');
53                 }
54             } else {
55                 if($account = get_user_preferences('flickrmail', '')){
56                     $SESSION->flickrmail = $account;
57                     if(empty($action)) {
58                         $action = 'list';
59                     }
60                 }
61             }
62         }
63     }
64     public function print_login($ajax = true){
65         global $SESSION;
66         if(empty($SESSION->flickrmail)) {
67         $str =<<<EOD
68 <form id="moodle-repo-login">
69 <label for="account">Account (Email)</label><br/>
70 <input type='text' name='flickrmail' id='account' />
71 <input type='hidden' name='id' value='$this->repositoryid' /><br/>
72 <input type='checkbox' name='remember' id="keepid" value='true' /> <label for="keepid">Remember? </label>
73 <p><input type='button' onclick="repository_client.login()" value="Go" /></p>
74 </form>
75 EOD;
76             if($ajax){
77                 $ret = array();
78                 $ret['l'] = $str;
79                 return $ret;
80             }else{
81                 echo $str;
82             }
83         } else {
84             return $this->get_listing();
85         }
86     }
87     public function get_listing($path = '1', $search = ''){
88         global $SESSION;
89         $people = $this->flickr->people_findByEmail($SESSION->flickrmail);
90         $photos_url = $this->flickr->urls_getUserPhotos($people['nsid']);
92         if(!empty($search)) {
93             // do searching, if $path is not empty, ignore it.
94             $photos = $this->flickr->photos_search(array('user_id'=>$people['nsid'], 'text'=>$search));
95         } elseif(!empty($path) && empty($search)) {
96             $photos = $this->flickr->people_getPublicPhotos($people['nsid'], null, 36, $path);
97         }
99         $ret = new stdclass;
100         $ret->url   = $photos_url;
101         $ret->list  = array();
102         $ret->pages = $photos['pages'];
103         if(is_int($path) && $path <= $ret->pages) {
104             $ret->page = $path;
105         } else {
106             $ret->page = 1;
107         }
108         foreach ($photos['photo'] as $p) {
109             if(empty($p['title'])) {
110                 $p['title'] = get_string('notitle', 'repository_flickr');
111             }
112             $ret->list[] =
113                 array('title'=>$p['title'],'source'=>$p['id'],'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'), 'date'=>'', 'size'=>'unknown');
114         }
115         if(empty($ret)) {
116             throw new repository_exception('nullphotolist', 'repository');
117         } else {
118             return $ret;
119         }
120     }
121     public function print_listing(){
122         if(empty($this->photos)){
123             $this->get_listing();
124         }
125         $str = '';
126         $str .= '<h2>Account: <span>'.$this->photos['a'].'</span></h2>';
127         foreach ((array)$this->photos['photo'] as $photo) {
128             $str .= "<a href='".$this->photos['url'].$photo[id]."'>";
129             $str .= "<img border='0' alt='$photo[title]' ".
130                 "src=" . $photo['thumbnail'] . ">";
131             $str .= "</a>";
132             $i++;
134             if ($i % 4 == 0) {
135                 $str .= "<br/>";
136             }
137         }
138         $str .= <<<EOD
139 <style type='text/css'>
140 #paging{margin-top: 10px; clear:both}
141 #paging a{padding: 4px; border: 1px solid gray}
142 </style>
143 EOD;
144         $str .= '<div id="paging">';
145         for($i=1; $i <= $this->photos['pages']; $i++) {
146             $str .= '<a href="###" onclick="cr('.$this->repositoryid.', '.$i.', 0)">';
147             $str .= $i;
148             $str .= '</a> ';
149         }
150         $str .= '</div>';
151         echo $str;
152     }
153     public function print_search(){
154         echo '<input type="text" name="Search" value="search terms..." size="40" class="right"/>';
155         return true;
156     }
157     public function get_file($photo_id, $file = ''){
158         global $CFG;
159         $result = $this->flickr->photos_getSizes($photo_id);
160         $url = '';
161         if(!empty($result[4])) {
162             $url = $result[4]['source'];
163         } elseif(!empty($result[3])) {
164             $url = $result[3]['source'];
165         } elseif(!empty($result[2])) {
166             $url = $result[2]['source'];
167         }
168         if (!file_exists($CFG->dataroot.'/repository/download')) {
169             mkdir($CFG->dataroot.'/repository/download/', 0777, true);
170         }
171         if(is_dir($CFG->dataroot.'/repository/download')) {
172             $dir = $CFG->dataroot.'/repository/download/';
173         }
175         if(empty($file)) {
176             $file = $photo_id.'_'.time().'.jpg';
177         }
178         if(file_exists($dir.$file)) {
179             $file = uniqid('m').$file;
180         }
181         $fp = fopen($dir.$file, 'w');
182         $c = new curl;
183         $c->download(array(
184             array('url'=>$url, 'file'=>$fp)
185         ));
186         return $dir.$file;
187     }
189 ?>