<commit_before><?php
/*
 * e-additives.server RESTful API
 * Copyright (C) 2013 VEXELON.NET Services
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
namespace Eadditives\Models;

use \Eadditives\MyRequest;

/**
 * CategoriesModel
 *
 * Fetch additives categories data from database.
 *
 * @package Eadditives
 * @author  p.petrov
 */
class CategoriesModel extends Model {

    const CACHE_KEY = 'cats_';
    const CACHE_TTL = 600;  // 10 minutes   

    /**
     * Get a list of categories.
     * @param  array $criteria Filtering criteria.
     * @throws ModelException On any SQL error.
     * @return array 
     */ 
    public function getAll($criteria = array()) {
        $criteria = $this->getDatabaseCriteria($criteria);

        $sql = "SELECT c.id, c.last_update, p.name,
            (SELECT COUNT(id) FROM Additive as a WHERE a.category_id=c.id) as additives
            FROM AdditiveCategory as c
            LEFT JOIN AdditiveCategoryProps as p ON p.category_id = c.id
            WHERE p.locale_id = :locale_id";

        // apply sort criteria
        if (!is_null($criteria[MyRequest::PARAM_SORT])) {
            $this->validateCriteria($criteria, MyRequest::PARAM_SORT, array('id', 'name', 'last_update'));
            $sql .= sprintf(" ORDER BY %s %s", 
                $criteria[MyRequest::PARAM_SORT], 
                $criteria[MyRequest::PARAM_ORDER]);
        }

        try {

            $statement = $this->dbConnection->executeQuery($sql, array(
                'locale_id' => $criteria[MyRequest::PARAM_LOCALE]
            ));
            $result = $statement->fetchAll();

            // $this->validateResult($result);

            // format results
            $items = array();
            foreach ($result as $row) {
                // ISO-8601 datetime format
                $dt = new \DateTime($row['last_update']);
                $row['last_update'] = $dt->format(\DateTime::ISO8601);
                // add resource url
                $row['url'] = BASE_URL . '/categories/' . $row['id'];
                // add updated row
                $items[] = $row;
            }

            return $items;          

        } catch (\Exception $e) {
            throw new ModelException('SQL Error!', $e->getCode(), $e);
        }
    }

    /**
     * Get information about single category.
     * @param  string $id category id
     * @param  array $criteria Filtering criteria.
     * @throws ModelException On any SQL error.
     * @return array 
     */ 
    public function getSingle($id, $criteria = array()) {
        $criteria = $this->getDatabaseCriteria($criteria);

        // get cached result
        $cacheKey = $this->cache->genKey(self::CACHE_KEY, $criteria[MyRequest::PARAM_LOCALE], $id);
        if ($this->cache->exists($cacheKey)) {
            return $this->cache->hget($cacheKey);
        }               

        $sql = "SELECT c.id, p.name, p.description, p.last_update,
            (SELECT COUNT(id) FROM Additive as a WHERE a.category_id=c.id) as additives
            FROM AdditiveCategory as c
            LEFT JOIN AdditiveCategoryProps as p ON p.category_id = c.id
            WHERE c.id = :category_id AND p.locale_id = :locale_id LIMIT 1";

        try {

            $statement = $this->dbConnection->executeQuery($sql, array(
                'locale_id' => $criteria[MyRequest::PARAM_LOCALE],
                'category_id' => $id
            ));
            $result = $statement->fetch();

            $this->validateResult($result); 

            // ISO-8601 datetime format
            $dt = new \DateTime($row['last_update']);
            $result['last_update'] = $dt->format(\DateTime::ISO8601);
            // add resource url
            $result['url'] = BASE_URL . '/categories/' . $result['id'];<commit_msg>Fixed incorrect last_update datetime for single category fetch<commit_after><?php
/*
 * e-additives.server RESTful API
 * Copyright (C) 2013 VEXELON.NET Services
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
namespace Eadditives\Models;

use \Eadditives\MyRequest;

/**
 * CategoriesModel
 *
 * Fetch additives categories data from database.
 *
 * @package Eadditives
 * @author  p.petrov
 */
class CategoriesModel extends Model {

    const CACHE_KEY = 'cats_';
    const CACHE_TTL = 600;  // 10 minutes   

    /**
     * Get a list of categories.
     * @param  array $criteria Filtering criteria.
     * @throws ModelException On any SQL error.
     * @return array 
     */ 
    public function getAll($criteria = array()) {
        $criteria = $this->getDatabaseCriteria($criteria);

        $sql = "SELECT c.id, c.last_update, p.name,
            (SELECT COUNT(id) FROM Additive as a WHERE a.category_id=c.id) as additives
            FROM AdditiveCategory as c
            LEFT JOIN AdditiveCategoryProps as p ON p.category_id = c.id
            WHERE p.locale_id = :locale_id";

        // apply sort criteria
        if (!is_null($criteria[MyRequest::PARAM_SORT])) {
            $this->validateCriteria($criteria, MyRequest::PARAM_SORT, array('id', 'name', 'last_update'));
            $sql .= sprintf(" ORDER BY %s %s", 
                $criteria[MyRequest::PARAM_SORT], 
                $criteria[MyRequest::PARAM_ORDER]);
        }

        try {

            $statement = $this->dbConnection->executeQuery($sql, array(
                'locale_id' => $criteria[MyRequest::PARAM_LOCALE]
            ));
            $result = $statement->fetchAll();

            // $this->validateResult($result);

            // format results
            $items = array();
            foreach ($result as $row) {
                // ISO-8601 datetime format
                $dt = new \DateTime($row['last_update']);
                $row['last_update'] = $dt->format(\DateTime::ISO8601);
                // add resource url
                $row['url'] = BASE_URL . '/categories/' . $row['id'];
                // add updated row
                $items[] = $row;
            }

            return $items;          

        } catch (\Exception $e) {
            throw new ModelException('SQL Error!', $e->getCode(), $e);
        }
    }

    /**
     * Get information about single category.
     * @param  string $id category id
     * @param  array $criteria Filtering criteria.
     * @throws ModelException On any SQL error.
     * @return array 
     */ 
    public function getSingle($id, $criteria = array()) {
        $criteria = $this->getDatabaseCriteria($criteria);

        // get cached result
        $cacheKey = $this->cache->genKey(self::CACHE_KEY, $criteria[MyRequest::PARAM_LOCALE], $id);
        if ($this->cache->exists($cacheKey)) {
            return $this->cache->hget($cacheKey);
        }               

        $sql = "SELECT c.id, p.name, p.description, p.last_update,
            (SELECT COUNT(id) FROM Additive as a WHERE a.category_id=c.id) as additives
            FROM AdditiveCategory as c
            LEFT JOIN AdditiveCategoryProps as p ON p.category_id = c.id
            WHERE c.id = :category_id AND p.locale_id = :locale_id LIMIT 1";

        try {

            $statement = $this->dbConnection->executeQuery($sql, array(
                'locale_id' => $criteria[MyRequest::PARAM_LOCALE],
                'category_id' => $id
            ));
            $result = $statement->fetch();

            $this->validateResult($result); 

            // ISO-8601 datetime format
            $dt = new \DateTime($result['last_update']);
            $result['last_update'] = $dt->format(\DateTime::ISO8601);
            // add resource url
            $result['url'] = BASE_URL . '/categories/' . $result['id'];
{'fraction_non_alphanumeric': 0.11987381703470032, 'fraction_numerical': 0.00608382154123479, 'mean_word_length': 2.1478723404255318, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 51, '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': '3388054', 'n_tokens_mistral': 2550, 'n_tokens_neox': 2403, 'n_words': 728}