Esempio n. 1
0
    /**
     * Retrieve a list of Documents
     *
     * @param String $name
     * @param Request $request
     * @return Response
     */
    public function listDocument($name, Request $request)
    {
        if (!$this->appKeyAvailable($request)) {
            return $this->notAuthorized($request);
        }
        $this->setSessionUser($request);
        if (!$this->isCollection($name)) {
            $this->setResultError("Collection '{$name}' doesn't exist", 404);
        } else {
            $perPage = intval(Input::get('perPage', 10));
            $start = intval((Input::get('page', 1) - 1) * $perPage);
            $query = urldecode(Input::get('query', ''));
            $table = "";
            if (!empty($query)) {
                parse_str($query, $where);
                $table = ", `data`";
                $condition = array();
                foreach ($where as $key => $value) {
                    $condition[] = "(`data`.`key` = '" . $key . "' and `data`.`value` LIKE '%" . $value . "%')";
                }
                $query = " and `data`.`document_id` = `documents`.`id` and (" . implode(' OR ', $condition) . ") ";
            }
            $data = DB::select('SELECT `documents`.`id` FROM `permissions`, `users`, `documents`, `collections`' . $table . '
                where `collections`.`name` = :name and `documents`.`collection_id` = `collections`.`id`
                and (
                    `documents`.`public` = 1 OR
                    (users.id = ' . $this->user->id . ' and users.group_id < 3 and documents.public = 0) OR
                    (
                        `permissions`.`access` = \'read\' and
                        `permissions`.`document_id` = `documents`.`id` and
                        (
                            `permissions`.`user_id` = ' . $this->user->id . ' OR `permissions`.`group_id` = 3
		                )
		            )
                ) ' . $query . '
                group by documents.id ORDER BY documents.created_at DESC LIMIT :start, :limit', ['name' => $name, 'start' => $start, 'limit' => $perPage]);
            $this->setResultOk();
            $documents = array();
            foreach ($data as $document) {
                $documents[] = Document::whereId($document->id)->first();
            }
            $this->setDocumentListData($documents);
        }
        return $this->setResponse();
    }
Esempio n. 2
0
 /**
  * Check if the Document is available
  *
  * @param Int $id
  * @return Bool
  */
 protected function isDocument($id)
 {
     return Document::whereId($id)->first() !== NULL;
 }