Api
Version
mediamosa-21Class
mediamosa_rest_call_collection_searchCode
File: /sites/all/modules/mediamosa/modules/collection/mediamosa_collection.rest.inc
<?php
/**
* URI: /collection
* Method: GET
*/
class mediamosa_rest_call_collection_search extends mediamosa_rest_call {
// ------------------------------------------------------------------ Consts.
// Rest vars;
const FAV_USER_ID = 'fav_user_id';
const ASSET_ID = 'asset_id';
const PRIVATE_ = 'private';
const PUBLIC_ = 'public';
const CATEGORY = 'category';
const PUBLIC_ASSIGN = 'public_assign';
const CQL = 'cql';
const USER_ID = 'user_id'; // alias of aut_user_id.
const AUT_USER_ID = 'aut_user_id';
const IS_PUBLIC_LIST = 'is_public_list';
const NUMOFVIDEOS = 'numofvideos';
const OPERATOR = 'operator';
const CALCULATE_TOTAL_COUNT = 'calculate_total_count';
// --------------------------------------------------------- Public Functions.
static public function get_search_params() {
return array(
'title' => mediamosa_type::TYPE_SEARCH_STRING,
'description' => mediamosa_type::TYPE_SEARCH_STRING,
'owner_id' => mediamosa_type::TYPE_SEARCH_STRING,
'group_id' => mediamosa_type::TYPE_SEARCH_STRING,
'created' => mediamosa_type::TYPE_SEARCH_DATETIME,
'changed' => mediamosa_type::TYPE_SEARCH_DATETIME,
'private' => mediamosa_type::TYPE_SEARCH_BOOL,
'public' => mediamosa_type::TYPE_SEARCH_BOOL,
'category' => mediamosa_type::TYPE_SEARCH_BOOL,
);
}
// ---------------------------------------------------------------- Var Setup.
public function get_var_setup() {
$var_setup = array();
$var_setup = array(
self::VARS => array(
self::ASSET_ID => array(
self::VAR_TYPE => mediamosa_type::TYPE_ASSET_ID,
self::VAR_DESCRIPTION => 'Search within the collection of this supplied asset ID.',
),
self::CQL => array(
self::VAR_TYPE => mediamosa_type::TYPE_CQL_COLLECTION,
self::VAR_DESCRIPTION => 'The CQL search string, used for extended and complex search options.',
),
self::FAV_USER_ID => array(
self::VAR_TYPE => mediamosa_type::TYPE_USER_ID,
self::VAR_DESCRIPTION => 'Search within the favorites of this supplied user ID.',
),
self::USER_ID => array(
self::VAR_TYPE => mediamosa_type::TYPE_USER_ID,
self::VAR_DESCRIPTION => 'Alias for aut_user_id parameter, is chosen above aut_user_id.',
),
self::AUT_USER_ID => array(
self::VAR_TYPE => mediamosa_type::TYPE_USER_ID,
self::VAR_DESCRIPTION => 'Authentication parameter, for including the collections that are private.',
),
self::PRIVATE_ => array(
self::VAR_TYPE => mediamosa_type::TYPE_BOOL,
self::VAR_DESCRIPTION => 'Search on the private flag status of the collection.',
),
self::PUBLIC_ => array(
self::VAR_TYPE => mediamosa_type::TYPE_BOOL,
self::VAR_DESCRIPTION => 'Search on the public flag status of the collection.',
),
self::CATEGORY => array(
self::VAR_TYPE => mediamosa_type::TYPE_BOOL,
self::VAR_DESCRIPTION => 'Search on the category flag status of the collection.',
),
self::PUBLIC_ASSIGN => array(
self::VAR_TYPE => mediamosa_type::TYPE_BOOL,
self::VAR_DESCRIPTION => 'Search on the public assign flag status of the collection.',
),
self::IS_APP_ADMIN => array(
self::VAR_TYPE => mediamosa_type::TYPE_BOOL,
self::VAR_DESCRIPTION => 'Allows seeing unappropriate assets.',
self::VAR_DEFAULT_VALUE => 'FALSE',
),
self::IS_PUBLIC_LIST => array(
self::VAR_TYPE => mediamosa_type::TYPE_BOOL,
self::VAR_DESCRIPTION => 'TRUE = include private assets, FALSE = exclude private assets (default),',
self::VAR_DEFAULT_VALUE => 'FALSE',
),
self::CALCULATE_TOTAL_COUNT => array(
self::VAR_TYPE => mediamosa_type::TYPE_BOOL,
self::VAR_DESCRIPTION => "Calculate the number of rows in the complete scope of the search. Performance wise to use this parameter with value 'FALSE' if you don't need to know the total number of rows in your scope result.",
self::VAR_DEFAULT_VALUE => 'TRUE',
),
// Old fasion search params.
self::OPERATOR => array(
self::VAR_TYPE => mediamosa_type::TYPE_OPERATOR,
self::VAR_DESCRIPTION => 'For non-CQL search only; provide either OR or AND for search parameters..',
self::VAR_DEFAULT_VALUE => 'and',
)
)
);
// Include limit, offset vars.
$var_setup = self::get_var_setup_range($var_setup);
// Include order by.
$var_setup = self::get_var_setup_order_by($var_setup);
// Include default.
$var_setup = self::get_var_setup_default($var_setup);
// Now include metadata search stuff.
$var_setup = mediamosa_core_cql::get_var_setup_search($var_setup, self::get_search_params());
return $var_setup;
}
// ----------------------------------------------- Override Process Rest Args.
// Fix for alias of user_id / aut_user_id.
protected function process_rest_args(array $var_setup) {
// Process input.
$var_setup = parent::process_rest_args($var_setup);
// If user_id is set, override aut_user_id, dont care if aut_user_id is set.
$user_id = $this->get_param_value(self::USER_ID);
if ($user_id != '') {
$this->set_param_value(self::AUT_USER_ID, $user_id);
}
// Return the var_setup.
return $var_setup;
}
// ---------------------------------------------- Override Validate Rest Args.
// Override for checking CQL string.
protected function validate_rest_args(array $var_setup) {
// Validate first.
parent::validate_rest_args($var_setup);
$cql = $this->get_param_value(self::CQL);
$a_app_ids = $this->get_param_value_app();
$error_text = '';
if ($cql != '' && !mediamosa_core_cql::verify_collection($cql, $a_app_ids, $error_text)) {
throw new mediamosa_exception_error(mediamosa_error::ERRORCODE_CQL_ERROR, array('@error' => $error_text));
}
}
// ------------------------------------------------------------------ Do Call.
public function do_call() {
$o_mediamosa = mediamosa::get();
$a_app_ids = $this->get_param_value_app();
// Now convert the old search parameters to CQL.
$var_setup = mediamosa_core_cql::get_var_setup_search(array(), self::get_search_params());
// Non CQL parameters.
$order_by = $this->get_param_value(self::ORDER_BY);
$order_direction = $this->get_param_value(self::ORDER_DIRECTION);
$cqls = array();
$a_translate_to = array();
foreach ($var_setup[mediamosa_rest_call::VARS] as $param => $a_param) {
if ($a_param[mediamosa_rest_call::VAR_TYPE] == mediamosa_type::TYPE_SEARCH_MATCH) {
continue;
}
// When provided, then convert.
if (!self::empty_param($param)) {
$a_value = $this->get_param_value($param);
$match = $this->get_param_value($param . '_match');
$cqls[] = mediamosa_core_cql::convert_searchparam_to_cql($param, $match, $a_value, $a_param[mediamosa_rest_call::PARAM_TYPE], $a_translate_to);
}
}
// Build either the CQL from scratch or build from the parameters.
$cql = '';
if (count($cqls)) {
// CQL must not be set.
if (!self::empty_param(self::CQL)) {
throw new mediamosa_exception_error_cql_exclusive();
}
// Get the glue operator.
$operator = $this->get_param_value(self::OPERATOR);
$cql = implode(' ' . mediamosa_unicode::strtoupper($operator) . ' ', $cqls);
}
// Add possible order by to the CQL.
if ($order_by != '') {
if (!self::empty_param(self::CQL)) {
throw new mediamosa_exception_error_cql_exclusive();
}
$cql .= ($cql == '' ? ' ' : '') . 'sortby ' . $order_by;
}
elseif ($cql == '') {
// Now normal search params, then take CQL (if any).
$cql = $this->get_param_value(self::CQL);
}
// If order by is set on NUMOFVIDEOS and no value for order_direction was given, then set it to other default.
if ($this->get_param_value_order_by() == self::NUMOFVIDEOS && $this->isset_default_param(self::ORDER_DIRECTION)) {
self::set_param_value(self::ORDER_DIRECTION, mediamosa_db::ORDER_DIRECTION_DESC);
}
// Now search.
$result = mediamosa_collection_search::collection_search(
$a_app_ids,
$cql,
$this->get_param_value(self::ASSET_ID),
$this->get_param_value(self::FAV_USER_ID),
$this->get_param_value(self::AUT_USER_ID),
$this->get_param_value(self::IS_APP_ADMIN),
$this->get_param_value(self::IS_PUBLIC_LIST),
$this->get_param_value(self::CALCULATE_TOTAL_COUNT),
$this->get_param_value_limit(),
$this->get_param_value_offset()
);
// Total rows.
$o_mediamosa->item_count_total = $result->found_rows;
// Set result.
foreach ($result as $a_row) {
$o_mediamosa->add_item($a_row);
}
}
}