<commit_before><?php
/**
 * MetaResources controller.
 *
 * The MetaResource controller is extended in ARCS and used as a template for
 * RESTful actions. It doesn't use views--all data is sent and received as
 * JSON.
 *
 * @package    ARCS
 * @link       http://github.com/calmsu/arcs
 * @copyright  Copyright 2012, Michigan State University Board of Trustees
 * @license    BSD License (http://www.opensource.org/licenses/bsd-license.php)
 */
class MetaResourcesController extends AppController {

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('view');
        $model = $this->modelClass;
        if (!isset($this->request->query['related'])) {
            $this->$model->recursive = -1;
            $this->$model->flatten = true;
        }
    }

    /**
     * Add a meta-resource
     */
    public function add() {
        if (!$this->request->is('post')) throw new MethodNotAllowedException();
        if (!$this->request->data) throw new BadRequestException;
        $model = $this->modelClass;
        //debug($model);
        # if ($model == 'bookmarks' || $model == 'comments' || $model == 'jobs' || $model == 'keywords') {
        $this->$model->permit('user_id');
        $this->request->data['user_id'] = $this->Auth->user('id');
        # } elseif ($model == 'annotations' || $model == 'flags') {
        #   add resource_name, user_name, user_email, user_username, user_id
        # }
        # if ($model == 'flags') {
        # add status
        # }
        if (!$this->$model->add($this->request->data)) 
            throw new InternalErrorException();
        $this->json(201, $this->$model->findById($this->$model->id));
    }

    /**
     * Edit a meta-resource
     *
     * @param string $id
     */
    public function edit($id) {
        if (!($this->request->is('put') || $this->request->is('post'))) 
            throw new MethodNotAllowedException();
            // return $this->json(405);
        $model = $this->modelClass;
        $this->$model->read(null, $id);
        if (!$this->$model->exists()) 
            throw new NotFoundException();
            // return $this->json(404);
        if (!$this->request->data) 
            throw new BadRequestException();
            // return $this->json(400);
        if (!$this->$model->add($this->request->data)) 
            throw new InternalErrorException();
            // return $this->json(500);
        $this->json(200, $this->$model->findById($this->$model->id));
    }

    /**
     * View a meta-resource
     *
     * @param string $id
     */
    public function view($id) {
        if (!$this->request->is('get')) throw new MethodNotAllowedException();
        $model = $this->modelClass;
        $result = $this->$model->findById($id);
        if (!$result) throw new NotFoundException(); 
        $this->json(200, $result);
    }

    /**
     * Delete a meta-resource
     *
     * @param string $id
     */
    public function delete($id) {
        if (!$this->request->is('delete')) throw new MethodNotAllowedException();
        $model = $this->modelClass;
        $this->$model->flatten = true;
        $result = $this->$model->findById($id);
        if (!$result) throw new NotFoundException();
        if (!($this->Access->isSrResearcher() || $this->Access->isCreator($result)))
            throw new ForbiddenException();
        if (!$this->$model->delete($id)) throw new InternalErrorException();
        $this->json(204);
    }
}
<commit_msg>Updated metaresources view method to be accessible by restful api<commit_after><?php
/**
 * MetaResources controller.
 *
 * The MetaResource controller is extended in ARCS and used as a template for
 * RESTful actions. It doesn't use views--all data is sent and received as
 * JSON.
 *
 * @package    ARCS
 * @link       http://github.com/calmsu/arcs
 * @copyright  Copyright 2012, Michigan State University Board of Trustees
 * @license    BSD License (http://www.opensource.org/licenses/bsd-license.php)
 */
class MetaResourcesController extends AppController {

    public function beforeFilter() {
		if ($this->request->accepts('application/json')) {
			$this->RequestHandler->renderAs($this, 'json');
		}
        parent::beforeFilter();
        $this->Auth->allow('view');
        $model = $this->modelClass;
        if (!isset($this->request->query['related'])) {
            $this->$model->recursive = -1;
            $this->$model->flatten = true;
        }
    }

    /**
     * Add a meta-resource
     */
    public function add() {
        if (!$this->request->is('post')) throw new MethodNotAllowedException();
        if (!$this->request->data) throw new BadRequestException;
        $model = $this->modelClass;
        //debug($model);
        # if ($model == 'bookmarks' || $model == 'comments' || $model == 'jobs' || $model == 'keywords') {
        $this->$model->permit('user_id');
        $this->request->data['user_id'] = $this->Auth->user('id');
        # } elseif ($model == 'annotations' || $model == 'flags') {
        #   add resource_name, user_name, user_email, user_username, user_id
        # }
        # if ($model == 'flags') {
        # add status
        # }
        if (!$this->$model->add($this->request->data)) 
            throw new InternalErrorException();
        $this->json(201, $this->$model->findById($this->$model->id));
    }

    /**
     * Edit a meta-resource
     *
     * @param string $id
     */
    public function edit($id) {
        if (!($this->request->is('put') || $this->request->is('post'))) 
            throw new MethodNotAllowedException();
            // return $this->json(405);
        $model = $this->modelClass;
        $this->$model->read(null, $id);
        if (!$this->$model->exists()) 
            throw new NotFoundException();
            // return $this->json(404);
        if (!$this->request->data) 
            throw new BadRequestException();
            // return $this->json(400);
        if (!$this->$model->add($this->request->data)) 
            throw new InternalErrorException();
            // return $this->json(500);
        $this->json(200, $this->$model->findById($this->$model->id));
    }

    /**
     * View a meta-resource
     *
     * @param string $id
     */
    public function view($id) {
        //if (!$this->request->is('get')) throw new MethodNotAllowedException();
        $model = $this->modelClass;
        $result = $this->$model->findById($id);
        //if (!$result) throw new NotFoundException(); 
        $this->json(200, $result);
    }

    /**
     * Delete a meta-resource
     *
     * @param string $id
     */
    public function delete($id) {
        if (!$this->request->is('delete')) throw new MethodNotAllowedException();
        $model = $this->modelClass;
        $this->$model->flatten = true;
        $result = $this->$model->findById($id);
        if (!$result) throw new NotFoundException();
        if (!($this->Access->isSrResearcher() || $this->Access->isCreator($result)))
            throw new ForbiddenException();
        if (!$this->$model->delete($id)) throw new InternalErrorException();
        $this->json(204);
    }
}

{'fraction_non_alphanumeric': 0.17829241071428573, 'fraction_numerical': 0.008091517857142858, 'mean_word_length': 2.35941893158388, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 153, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1999738', 'n_tokens_mistral': 2118, 'n_tokens_neox': 2071, 'n_words': 459}