/**
  * Find the first active document for a given folder
  *
  * @param $folder_id
  * @return mixed
  */
 protected function findFirstDocumentByParentId($folder_id)
 {
     $list = new Document\Listing();
     $list->setCondition("parentId = ?", (int) $folder_id);
     $list->setOrderKey("index");
     $list->setOrder("asc");
     $list->setLimit(1);
     $childsList = $list->load();
     return reset($childsList);
 }
 /**
  * Original function
  * @see Admin_DocumentController::treeGetChildsByIdAction()
  */
 public function treeGetChildsByIdAction()
 {
     $languages = Tool::getValidLanguages();
     $language = $this->_getParam("language", reset($languages));
     $document = Document::getById($this->getParam("node"));
     $documents = array();
     if ($document->hasChilds()) {
         $limit = intval($this->getParam("limit"));
         if (!$this->getParam("limit")) {
             $limit = 100000000;
         }
         $offset = intval($this->getParam("start"));
         $list = new Document\Listing();
         if ($this->getUser()->isAdmin()) {
             $list->setCondition("parentId = ? ", $document->getId());
         } else {
             $userIds = $this->getUser()->getRoles();
             $userIds[] = $this->getUser()->getId();
             $list->setCondition("parentId = ? and\r\n                                        (\r\n                                        (select list from users_workspaces_document where userId in (" . implode(',', $userIds) . ") and LOCATE(CONCAT(path,`key`),cpath)=1  ORDER BY LENGTH(cpath) DESC LIMIT 1)=1\r\n                                        or\r\n                                        (select list from users_workspaces_document where userId in (" . implode(',', $userIds) . ") and LOCATE(cpath,CONCAT(path,`key`))=1  ORDER BY LENGTH(cpath) DESC LIMIT 1)=1\r\n                                        )", $document->getId());
         }
         $list->setOrderKey("index");
         $list->setOrder("asc");
         $list->setLimit($limit);
         $list->setOffset($offset);
         $childsList = $list->load();
         foreach ($childsList as $childDocument) {
             // only display document if listing is allowed for the current user
             if ($childDocument->isAllowed("list")) {
                 if ($childDocument instanceof Document\Page && $childDocument->hasProperty('isLanguageRoot') && $childDocument->getProperty('isLanguageRoot') == 1) {
                     if ($childDocument->getKey() == $language) {
                         //                            $documents[] = $this->getTreeNodeConfig($childDocument);
                         $config = $this->getTreeNodeConfig($childDocument);
                         $config['expanded'] = true;
                         $documents[] = $config;
                     }
                 } else {
                     $documents[] = $this->getTreeNodeConfig($childDocument);
                 }
             }
         }
     }
     if ($this->getParam("limit")) {
         $this->_helper->json(array("offset" => $offset, "limit" => $limit, "total" => $document->getChildAmount($this->getUser()), "nodes" => $documents));
     } else {
         $this->_helper->json($documents);
     }
     $this->_helper->json(false);
 }
 public function deleteAction()
 {
     if ($this->getParam("type") == "childs") {
         $parentDocument = Document::getById($this->getParam("id"));
         $list = new Document\Listing();
         $list->setCondition("path LIKE '" . $parentDocument->getFullPath() . "/%'");
         $list->setLimit(intval($this->getParam("amount")));
         $list->setOrderKey("LENGTH(path)", false);
         $list->setOrder("DESC");
         $documents = $list->load();
         $deletedItems = array();
         foreach ($documents as $document) {
             $deletedItems[] = $document->getFullPath();
             if ($document->isAllowed("delete")) {
                 $document->delete();
             }
         }
         $this->_helper->json(array("success" => true, "deleted" => $deletedItems));
     } else {
         if ($this->getParam("id")) {
             $document = Document::getById($this->getParam("id"));
             if ($document->isAllowed("delete")) {
                 try {
                     $document->delete();
                     $this->_helper->json(array("success" => true));
                 } catch (\Exception $e) {
                     \Logger::err($e);
                     $this->_helper->json(array("success" => false, "message" => $e->getMessage()));
                 }
             }
         }
     }
     $this->_helper->json(array("success" => false, "message" => "missing_permission"));
 }