Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //GenericApi.php
- <?php
- class GenericApi {
- private $class;
- private $validApiColumns;
- private $booleanColumns;
- public function __construct() {
- $auth = $GLOBALS['authentication'];
- if(!$auth->loggedIn()){
- header('HTTP/1.1 401 Api requires login');
- die();
- }
- $class = ucfirst(strtolower(fGrammar::singularize($_GET['class'])));
- $this->class = $class;
- if(!DriveApi::hasApi($class)){
- throw new fProgrammerException('the class %s has not been configured for DriveApi', $class);
- }
- $this->validApiColumns = $class::getApiSetting('validApiColumns');
- $this->booleanColumns = $class::getApiSetting('booleanColumns');
- }
- public function notFound(){
- PageData::generate404();
- }
- public function getObject(){
- $id = $_GET['id'];
- try{
- $obj = new $this->class($id);
- $output = $this->getObjectApiData($obj);
- fJSON::sendHeader();
- echo fJSON::encode($output);
- }
- catch(fExpectedException $e){
- header('HTTP/1.1 404 Not Found');
- echo 'The ' . $this->class . ' requested could not be found';
- }
- }
- public function listObjects(){
- try{
- $objects = fRecordSet::build($this->class);
- // TODO sort hook params
- $objects = $objects->sort('getLevel', 'asc');
- $output = array();
- foreach($objects as $obj){
- $output[] = $this->getObjectApiData($obj);
- }
- fJSON::sendHeader();
- echo fJSON::encode($output);
- }
- catch(Exception $e){
- expose($e);
- }
- }
- private function getObjectApiData($obj){
- $values = $obj->getValuesAsArray();
- // filter validvalues
- $validApiColumns = array_flip($this->validApiColumns);
- $apiData = array_intersect_key($values, $validApiColumns);
- // TODO add hook
- $apiData['level'] = $obj->getLevel();
- if($this->booleanColumns != null){
- foreach($this->booleanColumns as $column){
- //echo $column . ' : ' . $apiData[$column] . PHP_EOL;
- $apiData[$column] = (bool)($apiData[$column] == 'true');
- }
- }
- // TODO: create api data value post hook for stuff like url_rewrite_override ?
- if($obj->getUrlRewrite() == 'home'){
- $apiData['url_rewrite_override'] = '/';
- }
- return $apiData;
- }
- public function create(){
- $put = file_get_contents('php://input');
- $data = (array)json_decode($put);
- try {
- $page = $this->createFromArray($data);
- $page->validate(TRUE);
- $page->store();
- $output = $this->getObjectApiData($page);
- header('HTTP/1.1 201 ' . $this->class . ' Created');
- fJSON::sendHeader();
- // all values are new don't check for changed
- echo fJSON::encode($output);
- }
- catch (fValidationException $e) {
- header('HTTP/1.1 400 Validation Error');
- echo $e->getMessage();
- }
- }
- private function createFromArray(Array $data){
- $page = new Page();
- self::populateFromArray($data);
- $page->validate(TRUE);
- $page->store();
- return $page;
- }
- private function updateFromArray(Array $data){
- $page = new Page($data['id']);
- self::populateFromArray($data);
- $page->validate(TRUE);
- $changedValues = $page->getChanged();
- $page->store();
- $newValues = $page->getValuesAsArray();
- $changedValues = $page->getChanged();
- return $changedValues;
- }
- private static function populateFromArray(Array $data){
- $schema = fORMSchema::retrieve($this->class);
- $table = fORM::tablize($this->class);
- $column_info = $schema->getColumnInfo($table);
- foreach ($column_info as $column => $info) {
- if(array_key_exists($column, $data)){
- $method = 'set' . fGrammar::camelize($column, TRUE);
- $page->$method($data[$column]);
- }
- }
- }
- public function batchUpdate(){
- try {
- $put = file_get_contents('php://input');
- $data = (array)json_decode($put);
- $changed = array();
- foreach($data as $page){
- $changedPageValues = $this->updateFromArray((array)$page);
- // add id that will not be present because it was not changed
- $changedPageValues['id'] = $page->id;
- $changed[] = $changedPageValues;
- }
- header('HTTP/1.1 200 ' . fGrammar::pluralize('zxc') . ' Updated');
- fJSON::sendHeader();
- echo fJSON::encode($changed);
- }
- catch (fValidationException $e) {
- header('HTTP/1.1 400 Validation Error');
- echo $e->getMessage();
- }
- }
- public function update(){
- try {
- $put = file_get_contents('php://input');
- $data = (array)json_decode($put);
- $output = $this->updateFromArray((array)$data);
- header('HTTP/1.1 200 ' . $this->class . ' Updated');
- fJSON::sendHeader();
- echo fJSON::encode($output);
- }
- catch (fValidationException $e) {
- header('HTTP/1.1 400 Validation Error');
- echo $e->getMessage();
- }
- }
- public function delete(){
- try {
- $id = fRequest::get('id', 'int');
- $item = new $this->class($id);
- $item->validate(true);
- $item->delete();
- header('HTTP/1.1 200 ' . $this->class . ' Deleted');
- echo $this->class . ' was deleted';
- }
- catch (fValidationException $e) {
- header('HTTP/1.1 400 Validation Error');
- echo $e->getMessage();
- }
- }
- }
- // DriveApi.php (drive is application name not sure what to call this)
- <?php
- class DriveApi {
- static private $module_classes = array();
- static public function configure($class,Array $validApiColumns,Array $booleanColumns = null){
- $class = fORM::getClass($class);
- self::$module_classes[$class] = array();
- self::$module_classes[$class]['validApiColumns'] = $validApiColumns;
- self::$module_classes[$class]['booleanColumns'] = $booleanColumns;
- fORM::registerActiveRecordStaticMethod($class, 'getApiSetting', 'DriveApi::getApiSetting');
- // list all
- Anchor::add('get json /api/v1/:class', 'GenericApi::listObjects');
- // get obj with id
- Anchor::add('get json /api/v1/:class/:id', 'GenericApi::getObject');
- // create new obj
- Anchor::add('post json /api/v1/:class', 'GenericApi::create');
- // update obj
- Anchor::add('put json /api/v1/:class/:id', 'GenericApi::update');
- // batch update objects
- Anchor::add('put json /api/v1/:class/batch/update', 'GenericApi::batchUpdate');
- // delete obj
- Anchor::add('delete json /api/v1/:class/:id', 'GenericApi::delete');
- }
- static public function getApiSetting($class, $method_name, $parameters){
- $class = fORM::getClass($class);
- if(isset(self::$module_classes[$class])){
- return self::$module_classes[$class][$parameters[0]];
- }
- }
- static public function hasApi($class){
- return isset(self::$module_classes[$class]);
- }
- }
- // example useage
- class Foo extends fActiveRecord
- {
- protected function configure(){
- DriveApi::configure(
- $this,
- array(
- 'id',
- 'parent_id',
- 'name',
- 'url_rewrite',
- 'order',
- 'locked',
- 'target',
- 'status',
- 'external_url',
- 'primary_nav_visible'
- ),
- array('locked', 'primary_nav_visible')
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement