Ejemplo n.º 1
0
        }
    }
    /**
     * Возвращет массив новостей
     * @return array
     */
    public function getNews()
    {
        $pager = $this->getPager();
        $offset = ($pager->current_page - 1) * $this->per_page;
        $params = [];
        $sql = 'SELECT A.id, A.name, A.alias, A.image,
                       A.intro, A.text,
                       A.created_at, A.updated_at,
                       A.category_id, B.name AS category_name
                FROM articles AS A
                  LEFT JOIN categories AS B ON B.id=A.category_id
                WHERE A.deleted_at IS NULL';
        if ($this->category !== 0) {
            $sql .= ' AND A.category_id=:category';
            $params[':category'] = $this->category;
        }
        $sql .= ' ORDER BY ' . $this->order_by . ' LIMIT ' . (int) $this->per_page . ' OFFSET ' . (int) $offset;
        $result = $this->db->select($sql, $params);
        return ['pager' => $pager, 'items' => $result];
    }
}
$api = new ApiCategory();
$news = $api->getNews();
header('Content-Type:application/json');
echo json_encode($news);
Ejemplo n.º 2
0
 private static function categoryFilter($object, \ApiParam $params)
 {
     if (!self::$fields) {
         $allowFields = array('name', 'englishName', 'parentEnglishName', 'level', 'shortname', 'redirect', 'children', 'viewType', 'meta');
         if ($params->fields) {
             self::$fields = array_intersect($allowFields, explode(',', (string) $params->fields));
         } else {
             self::$fields = array_slice($allowFields, 0, 9);
         }
     }
     $newObject = array();
     foreach (self::$fields as $field) {
         if (isset($object[$field]) && !empty($object[$field])) {
             $newObject[$field] = $object[$field];
         }
     }
     if (isset($object['appviewtype']) && !empty($object['appviewtype'])) {
         $newObject['viewType'] = $object['appviewtype'];
     }
     if (isset($object['appname']) && !empty($object['appname'])) {
         $newObject['name'] = $object['appname'];
     }
     return $newObject;
 }