Files
gallery3/modules/image_block/helpers/image_block_rest.php
Bharat Mediratta 0e3327bca7 Simplify the REST API code. Here's what I did:
1) Simplify gallery_rest to return flat models, no children and do no
   validation for now.
2) Flatten the REST replies and use HTTP codes to indicate
   success/failure instead of additional status messages.
3) Use the message and error code support in the base Exception class,
   instead of brewing our own in Rest_Exception.
4) Get rid of rest::success() and rest::fail() -- we only need
   rest::reply() since all failures are covered by throwing an
   exception.
5) Get rid of /rest/access_key and just use /rest for authentication.
6) Inline and simplify rest::normalize_request since we only use it once
7) Change rest::set_active_user to succeed or throw an exception
8) Extract Rest_Exception::sendHeaders into rest::send_headers()

Here's what's currently broken:
1) Data validation.  There currently is none
2) Logging.  That's gone too
3) image block and tag code is broken
4) Tests are broken
5) No movie support
2010-01-03 20:30:35 -08:00

63 lines
2.3 KiB
PHP

<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2009 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class image_block_rest_Core {
static function get($request) {
$type = empty($request->type) ? "random" : $request->type;
switch ($type) {
case "random":
$random = ((float)mt_rand()) / (float)mt_getrandmax();
$items = ORM::factory("item")
->viewable()
->where("type", "!=", "album")
->where("rand_key", "<", $random)
->order_by(array("rand_key" => "DESC"))
->find_all(1);
if ($items->count() == 0) {
// Try once more. If this fails, just ditch the block altogether
$items = ORM::factory("item")
->viewable()
->where("type", "!=", "album")
->where("rand_key", ">= ", $random)
->order_by(array("rand_key" => "DESC"))
->find_all(1);
}
break;
default:
return rest::fail("Unsupported image block type: '{$type}'");
}
if ($items->count() > 0) {
$item = $items->current();
$response_data = array("name" => $item->name,
"path" => $item->relative_url(),
"title" => $item->title,
"thumb_url" => $item->thumb_url(true),
"thumb_size" => array("height" => $item->thumb_height,
"width" => $item->thumb_width));
return rest::reply(array("resource" => $response_data));
} else {
return rest::reply();
}
}
}