Skip to content

Instantly share code, notes, and snippets.

@ahomu
Last active December 31, 2015 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahomu/7938826 to your computer and use it in GitHub Desktop.
Save ahomu/7938826 to your computer and use it in GitHub Desktop.
サンプルファイル ( filename の _ は / と脳内変換で )
<?php
class AAPP_JSONProvider extends ACMS_APP
{
public $version = '0.1';
public $name = 'ACMS JSON Provider';
public $author = 'mu.aho';
public $desc = 'a-blog cms のデータをJSON APIとして扱うための拡張です';
/**
* インストールする前の環境チェック処理
* @return bool
*/
public function checkRequirements()
{
return true;
}
/**
* インストールするときの処理
* データベーステーブルの初期化など
* @return void
*/
public function install() { }
/**
* アンインストールするときの処理
* データベーステーブルの始末など
* @return void
*/
public function uninstall() { }
/**
* アップデートするときの処理
* @return bool
*/
public function update() { return true; }
/**
* 有効化するときの処理
* @return bool
*/
public function activate() { return true; }
/**
* 無効化するときの処理
* @return bool
*/
public function deactivate() { return true; }
}
<?php
class AAPP_JSONProvider_GET_Entries extends ACMS_GET
{
var $_axis = array(
'bid' => 'self',
'cid' => 'self',
);
function get()
{
$DB = DB::singleton(dsn());
$SQL = SQL::newSelect('entry');
$limit = !!LIMIT ? LIMIT : 10;
$page = !!PAGE ? PAGE : 1;
if ( $uid = intval($this->uid) ) {
$SQL->addWhereOpr('entry_user_id', $uid);
}
if ( empty($this->cid) and null !== $this->cid ) {
$SQL->addWhereOpr('entry_category_id', null);
}
if ( !empty($this->eid) ) {
$SQL->addWhereOpr('entry_id', $this->eid);
}
ACMS_Filter::entrySession($SQL);
ACMS_Filter::entrySpan($SQL, $this->start, $this->end);
if ( !empty($this->tags) ) {
ACMS_Filter::entryTag($SQL, $this->tags);
}
if ( !empty($this->keyword) ) {
ACMS_Filter::entryKeyword($SQL, $this->keyword);
}
if ( !empty($this->Field) ) {
ACMS_Filter::entryField($SQL, $this->Field);
}
$Amount = new SQL_Select($SQL);
$Amount->setSelect('*', 'entry_amount', null, 'count');
if ( !$itemsAmount = intval($DB->query($Amount->get(dsn()), 'one')) ) {
return json_encode(array('entries' => array()));
}
ACMS_Filter::entryOrder($SQL, $this->order, $this->uid, $this->cid);
$from = ($page - 1) * $limit;
$limit = ((($from + $limit) > $itemsAmount) ? ($itemsAmount - $from) : $limit);
if ( 1 > $limit ) return '';
$SQL->setLimit($limit, ($from));
$q = $SQL->get(dsn());
$rows = $DB->query($q, 'all');
return json_encode(array('entries' => array_map(function($row) {
$row['entry_url'] = acmsLink(array(
'bid' => $row['entry_blog_id'],
'cid' => $row['entry_category_id'],
'eid' => $row['entry_id']
), false);
return $row;
}, $rows)));
}
}
%{callback}(<!-- BEGIN_MODULE Entries --><!-- END_MODULE Entries -->)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment