MDL-13766

introduce new search API
This commit is contained in:
dongsheng 2008-09-12 08:16:09 +00:00
parent b467ee7539
commit fbd508b463
3 changed files with 65 additions and 23 deletions

View File

@ -36,7 +36,6 @@ class repository_flickr_public extends repository{
}
public function global_search(){
global $SESSION;
if (empty($this->flickr_account)) {
return false;
} else {
@ -45,7 +44,7 @@ class repository_flickr_public extends repository{
}
public function __construct($repositoryid, $context = SITEID, $options = array()){
global $SESSION, $action, $CFG;
global $action, $CFG;
$options['page'] = optional_param('p', 1, PARAM_INT);
parent::__construct($repositoryid, $context, $options);
$this->api_key = $this->get_option('api_key');
@ -73,7 +72,6 @@ class repository_flickr_public extends repository{
}
}
public function print_login($ajax = true){
global $SESSION;
if(empty($this->flickr_account)) {
if($ajax){
$ret = array();
@ -94,17 +92,46 @@ class repository_flickr_public extends repository{
return $this->get_listing();
}
}
public function search(){
$people = $this->flickr->people_findByEmail($this->flickr_account);
$tag = optional_param('tag', '', PARAM_CLEANHTML);
$search = optional_param('s', '', PARAM_CLEANHTML);
if(!empty($tag)){
$photos = $this->flickr->photos_search(array(
'tags'=>$tag
));
} else {
$photos = $this->flickr->photos_search(array(
'user_id'=>$people['nsid'],
'text'=>$search));
}
$ret = array();
$ret['list'] = array();
$ret['nologin'] = true;
$ret['pages'] = $photos['pages'];
foreach ($photos['photo'] as $p) {
if(empty($p['title'])) {
$p['title'] = get_string('notitle', 'repository_flickr_public');
}
if (isset($p['originalformat'])) {
$format = $p['originalformat'];
} else {
$format = 'jpg';
}
$ret['list'][] =
array('title'=>$p['title'].'.'.$format,'source'=>$p['id'],'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'), 'date'=>'', 'size'=>'unknown', 'url'=>'http://www.flickr.com/photos/'.$p['owner'].'/'.$p['id']);
}
if(empty($ret)) {
throw new repository_exception('nullphotolist', 'repository_flickr_public');
} else {
return $ret;
}
}
public function get_listing($path = '1', $search = ''){
global $SESSION;
$people = $this->flickr->people_findByEmail($this->flickr_account);
$photos_url = $this->flickr->urls_getUserPhotos($people['nsid']);
if(!empty($search)) {
// do searching, if $path is not empty, ignore it.
$photos = $this->flickr->photos_search(array('user_id'=>$people['nsid'], 'text'=>$search));
} elseif(!empty($path) && empty($search)) {
$photos = $this->flickr->people_getPublicPhotos($people['nsid'], 'original_format', 25, $path);
}
$ret = array();
$ret['manage'] = $photos_url;
@ -139,7 +166,8 @@ class repository_flickr_public extends repository{
}
public function print_search(){
parent::print_search();
echo '<input type="text" name="s" />';
echo '<label>Keyword: </label><input type="text" name="s" /><br />';
echo '<label>Tag: </label><input type="text" name="tag" /><br />';
return true;
}
public function get_file($photo_id, $file = ''){

View File

@ -829,6 +829,15 @@ abstract class repository {
*/
abstract public function get_listing($parent = '/', $search = '');
/**
* Search
* @return mixed, see get_listing()
*/
public function search(){
$search = optional_param('s', '', PARAM_CLEANHTML);
return $this->get_listing(null, $search);
}
/**
* Show the login screen, if required
@ -1236,21 +1245,21 @@ function repository_get_client($context){
.fp-searchbar{float:right}
.fp-viewbar{width:300px;float:left}
.fp-toolbar{padding: .8em;background: #FFFFCC;color:white;text-align:center}
.fp-toolbar a{padding: 0 5px}
.fp-toolbar a{padding: 0 .5em}
.fp-list{list-style-type:none;padding:0}
.fp-list li{border-bottom:1px dotted gray;margin-bottom: 1em;}
.fp-repo-name{display:block;padding: 3px;margin-bottom: 5px}
.fp-pathbar{margin: 4px;border-bottom: 1px dotted gray;}
.fp-pathbar a{padding: 4px;}
.fp-repo-name{display:block;padding: .5em;margin-bottom: .5em}
.fp-pathbar{margin: .4em;border-bottom: 1px dotted gray;}
.fp-pathbar a{padding: .4em;}
.fp-rename-form{text-align:center}
.fp-rename-form p{margin: 1em;}
.fp-upload-form{margin: 2em 0;text-align:center}
.fp-upload-btn a{font-size: 14px;background: #ccc;color:white;padding: 5px}
.fp-upload-btn a{font-size: 1.5em;background: #ccc;color:white;padding: .5em}
.fp-upload-btn a:hover {background: grey;color:white}
.fp-paging{margin:10px 5px; clear:both;text-align:center}
.fp-paging a{padding: 4px;border: 1px solid #CCC}
.fp-paging{margin:1em .5em; clear:both;text-align:center;line-height: 2.5em;}
.fp-paging a{padding: .5em;border: 1px solid #CCC}
.fp-popup{text-align:center}
.fp-popup a{font-size: 24px}
.fp-popup a{font-size: 3em}
.fp-grid{width:80px; float:left;text-align:center;}
.fp-grid div{width: 80px; overflow: hidden}
.fp-grid p{margin:0;padding:0;background: #FFFFCC}

View File

@ -14,7 +14,6 @@ $file = optional_param('file', '', PARAM_RAW);
// rename the file name
$title = optional_param('title', '', PARAM_FILE);
$action = optional_param('action', '', PARAM_ALPHA);
$search = optional_param('s', '', PARAM_CLEANHTML);
$callback = optional_param('callback', '', PARAM_CLEANHTML);
// repository ID
$repo_id = optional_param('repo_id', 1, PARAM_INT);
@ -124,13 +123,19 @@ case 'login':
die(json_encode($err));
}
break;
case 'list':
case 'search':
try {
echo json_encode($repo->search());
} catch (repository_exception $e) {
$err = new stdclass;
$err->e = $e->getMessage();
die(json_encode($err));
}
break;
case 'list':
try {
if(!empty($p)) {
echo json_encode($repo->get_listing($p));
} else if(!empty($search)) {
echo json_encode($repo->get_listing('', $search));
} else {
echo json_encode($repo->get_listing());
}