Exemplo n.º 1
0
 public function redirectsAction()
 {
     if ($this->_getParam("data")) {
         if ($this->getUser()->isAllowed("redirects")) {
             if ($this->_getParam("xaction") == "destroy") {
                 $id = Zend_Json::decode($this->_getParam("data"));
                 $redirect = Redirect::getById($id);
                 $redirect->delete();
                 $this->_helper->json(array("success" => true, "data" => array()));
             } else {
                 if ($this->_getParam("xaction") == "update") {
                     $data = Zend_Json::decode($this->_getParam("data"));
                     // save redirect
                     $redirect = Redirect::getById($data["id"]);
                     if ($data["target"]) {
                         if ($doc = Document::getByPath($data["target"])) {
                             $data["target"] = $doc->getId();
                         }
                     }
                     $redirect->setValues($data);
                     $redirect->save();
                     $redirectTarget = $redirect->getTarget();
                     if (is_numeric($redirectTarget)) {
                         if ($doc = Document::getById(intval($redirectTarget))) {
                             $redirect->setTarget($doc->getFullPath());
                         }
                     }
                     $this->_helper->json(array("data" => $redirect, "success" => true));
                 } else {
                     if ($this->_getParam("xaction") == "create") {
                         $data = Zend_Json::decode($this->_getParam("data"));
                         unset($data["id"]);
                         // save route
                         $redirect = new Redirect();
                         if ($data["target"]) {
                             if ($doc = Document::getByPath($data["target"])) {
                                 $data["target"] = $doc->getId();
                             }
                         }
                         $redirect->setValues($data);
                         $redirect->save();
                         $redirectTarget = $redirect->getTarget();
                         if (is_numeric($redirectTarget)) {
                             if ($doc = Document::getById(intval($redirectTarget))) {
                                 $redirect->setTarget($doc->getFullPath());
                             }
                         }
                         $this->_helper->json(array("data" => $redirect, "success" => true));
                     }
                 }
             }
         } else {
             Logger::err("user [" . $this->getUser()->getId() . "] attempted to modify static routes, but has no permission to do so.");
         }
     } else {
         // get list of routes
         $list = new Redirect_List();
         $list->setLimit($this->_getParam("limit"));
         $list->setOffset($this->_getParam("start"));
         if ($this->_getParam("sort")) {
             $list->setOrderKey($this->_getParam("sort"));
             $list->setOrder($this->_getParam("dir"));
         }
         if ($this->_getParam("filter")) {
             $list->setCondition("`source` LIKE " . $list->quote("%" . $this->_getParam("filter") . "%") . " OR `target` LIKE " . $list->quote("%" . $this->_getParam("filter") . "%"));
         }
         $list->load();
         $redirects = array();
         foreach ($list->getRedirects() as $redirect) {
             if ($link = $redirect->getTarget()) {
                 if (is_numeric($link)) {
                     if ($doc = Document::getById(intval($link))) {
                         $redirect->setTarget($doc->getFullPath());
                     }
                 }
             }
             $redirects[] = $redirect;
         }
         $this->_helper->json(array("data" => $redirects, "success" => true, "total" => $list->getTotalCount()));
     }
     $this->_helper->json(false);
 }