public function execute($id)
 {
     $app = \App::getInstance();
     $docs = new \Archive\Port\Adaptor\Data\Archive\Documents();
     $conn = $app->DB_CONNECT;
     $params = array();
     $params[":id"] = $id;
     $ref = new \Archive\Port\Adaptor\Data\Archive\Refs\Ref();
     $ref->setRel("source");
     $ref->setHref($id);
     $docs->setRef($ref);
     $query = "SELECT `r`.`xmlview` AS `rxmlview`,`l`.`xmlview` AS `lxmlview` FROM `links` AS `l` \n\t\t            LEFT JOIN `resources` AS `r` ON `l`.`destination`=`r`.`id`\n\t\t            LEFT JOIN `documents_keys` AS `dk` ON `l`.`destination`=`dk`.`documentId`\n\t\t            WHERE `l`.`source`=:id AND `r`.`type`='document'\n\t\t            ORDER BY `dk`.`year`;";
     $sth = $conn->prepare($query);
     $sth->execute($params);
     while ($row = $sth->fetch()) {
         $doc = new \Archive\Port\Adaptor\Data\Archive\Documents\Document();
         $doc->fromXmlStr($row["rxmlview"]);
         $link = new \Archive\Port\Adaptor\Data\Archive\Links\Link();
         $link->fromXmlStr($row["lxmlview"]);
         $doc->setLink($link);
         $docs->setDocument($doc);
     }
     return $docs;
 }
 public function execute()
 {
     $app = \App::getInstance();
     $docs = new \Archive\Port\Adaptor\Data\Archive\Documents();
     $conn = $app->DB_CONNECT;
     $params = array();
     $start = isset($app->QUERY['start']) ? intval($app->QUERY['start']) : 0;
     $count = isset($app->QUERY['count']) ? intval($app->QUERY['count']) : (isset($app->QUERY['search']) ? 100 : 100);
     $where = "";
     if (isset($app->QUERY['id']) && $app->QUERY['id'] != "") {
         $where = " AND `r`.`id`= :id";
         $id = trim($app->QUERY['id']);
         $params[":id"] = $id;
         $ref = new \Archive\Port\Adaptor\Data\Archive\Refs\Ref();
         $ref->setRel("id");
         $ref->setHref($id);
         $docs->setRef($ref);
     }
     if (isset($app->QUERY['path']) && $app->QUERY['path'] != "") {
         $where = " AND `dk`.`path`= :path";
         $path = trim($app->QUERY['path']);
         $params[":path"] = $path;
         $ref = new \Archive\Port\Adaptor\Data\Archive\Refs\Ref();
         $ref->setRel("path");
         $ref->setHref($path);
         $docs->setRef($ref);
     }
     if (isset($app->QUERY['snumber']) && $app->QUERY['snumber'] != "") {
         $start = intval(trim($app->QUERY['snumber']));
         $count = 1;
     }
     $query = "SELECT `r`.`autoid`,`r`.`xmlview` FROM `resources` AS `r` \n\t\t            LEFT JOIN `documents_keys` AS `dk` ON `dk`.`documentId`=`r`.`id`\n\t\t            WHERE `r`.`type`='document' {$where}\n\t\t            ORDER BY `dk`.`path` LIMIT {$start},{$count};";
     $sth = $conn->prepare($query);
     $sth->execute($params);
     while ($row = $sth->fetch()) {
         $autoid = $row["autoid"];
         $doc = new \Archive\Port\Adaptor\Data\Archive\Documents\Document();
         $doc->fromXmlStr($row["xmlview"]);
         $docs->setDocument($doc);
     }
     if (count($docs->getDocument()) == 1) {
         $params = [$autoid];
         $query = "SELECT COUNT(`autoid`) AS `total` FROM `resources` WHERE `type`='document' AND `autoid` < ?;";
         $sth = $conn->prepare($query);
         $sth->execute($params);
         while ($row = $sth->fetch()) {
             $ref = new \Archive\Port\Adaptor\Data\Archive\Refs\Ref();
             $ref->setRel("snumber");
             $ref->setHref($row["total"]);
             $docs->setRef($ref);
         }
         $query = "SELECT COUNT(`autoid`) AS `total` FROM `resources` WHERE `type`='document';";
         $sth = $conn->prepare($query);
         $sth->execute($params);
         while ($row = $sth->fetch()) {
             $ref = new \Archive\Port\Adaptor\Data\Archive\Refs\Ref();
             $ref->setRel("max");
             $ref->setHref($row["total"] - 1);
             $docs->setRef($ref);
         }
     }
     return $docs;
 }