Пример #1
0
 public function testCopyAndDeleteDocument()
 {
     $documentList = new Document_List();
     $documentList->setCondition("`key` like '%_data%' and `type` = 'page'");
     $documents = $documentList->load();
     $parent = $documents[0];
     $this->assertTrue($parent instanceof Document_Page);
     //remove childs if there are some
     if ($parent->hasChilds()) {
         foreach ($parent->getChilds() as $child) {
             $child->delete();
         }
     }
     $this->assertFalse($parent->hasChilds());
     $service = new Document_Service(User::getById(1));
     //copy as child
     $service->copyAsChild($parent, $parent);
     $this->assertTrue($parent->hasChilds());
     $this->assertTrue(count($parent->getChilds()) == 1);
     //copy as child no. 2
     $service->copyAsChild($parent, $parent);
     $this->assertTrue($parent->hasChilds());
     $this->assertTrue(count($parent->getChilds()) == 2);
     $childs = $parent->getChilds();
     $this->assertTrue(Test_Tool::documentsAreEqual($parent, $childs[0], true));
     $this->assertTrue(Test_Tool::documentsAreEqual($parent, $childs[1], true));
     //copy recursivley
     $rootNode = Document::getById(1);
     $copy = $service->copyRecursive($rootNode, $parent);
     $this->assertTrue($copy->hasChilds());
     $this->assertTrue(count($copy->getChilds()) == 2);
     $this->assertTrue(Test_Tool::documentsAreEqual($parent, $copy, true));
     //create empty document
     $emptyDoc = Document_Page::create(1, array("userOwner" => 1, "key" => uniqid() . rand(10, 99)));
     $this->assertFalse(Test_Tool::documentsAreEqual($emptyDoc, $copy, true));
     //copy contents
     $emptyDoc = $service->copyContents($emptyDoc, $copy);
     $this->assertTrue(Test_Tool::documentsAreEqual($emptyDoc, $copy, true));
     //todo copy contents must fail if types differ
     //delete recusively
     $shouldBeDeleted[] = $copy->getId();
     $childs = $copy->getChilds();
     foreach ($childs as $child) {
         $shouldBeDeleted[] = $child->getId();
     }
     $copy->delete();
     foreach ($shouldBeDeleted as $id) {
         $o = Document::getById($id);
         $this->assertFalse($o instanceof Document);
     }
 }
 public function seopanelTreeAction()
 {
     $document = Document::getById($this->_getParam("node"));
     $documents = array();
     if ($document->hasChilds()) {
         $list = new Document_List();
         $list->setCondition("parentId = ?", $document->getId());
         $list->setOrderKey("index");
         $list->setOrder("asc");
         $childsList = $list->load();
         foreach ($childsList as $childDocument) {
             // only display document if listing is allowed for the current user
             if ($childDocument->isAllowed("list")) {
                 $list = new Document_List();
                 $list->setCondition("path LIKE ? and type = ?", array($childDocument->getFullPath() . "/%", "page"));
                 if ($childDocument instanceof Document_Page || $list->getTotalCount() > 0) {
                     $nodeConfig = $this->getTreeNodeConfig($childDocument);
                     if (method_exists($childDocument, "getTitle") && method_exists($childDocument, "getDescription")) {
                         $nodeConfig["title"] = $childDocument->getTitle();
                         $nodeConfig["description"] = $childDocument->getDescription();
                         $nodeConfig["title_length"] = strlen($childDocument->getTitle());
                         $nodeConfig["description_length"] = strlen($childDocument->getDescription());
                         // anaylze content
                         $nodeConfig["links"] = 0;
                         $nodeConfig["externallinks"] = 0;
                         $nodeConfig["h1"] = 0;
                         $nodeConfig["h1_text"] = "";
                         $nodeConfig["hx"] = 0;
                         $nodeConfig["imgwithalt"] = 0;
                         $nodeConfig["imgwithoutalt"] = 0;
                         try {
                             // cannot use the rendering service from Document_Service::render() because of singleton's ...
                             // $content = Document_Service::render($childDocument, array("pimcore_admin" => true, "pimcore_preview" => true), true);
                             $request = $this->getRequest();
                             $contentUrl = $request->getScheme() . "://" . $request->getHttpHost() . $childDocument->getFullPath();
                             $content = Pimcore_Tool::getHttpData($contentUrl, array("pimcore_preview" => true, "pimcore_admin" => true, "_dc" => time()));
                             if ($content) {
                                 $html = str_get_html($content);
                                 if ($html) {
                                     $nodeConfig["links"] = count($html->find("a"));
                                     $nodeConfig["externallinks"] = count($html->find("a[href^=http]"));
                                     $nodeConfig["h1"] = count($html->find("h1"));
                                     $h1 = $html->find("h1", 0);
                                     if ($h1) {
                                         $nodeConfig["h1_text"] = strip_tags($h1->innertext);
                                     }
                                     $nodeConfig["hx"] = count($html->find("h2,h2,h4,h5"));
                                     $images = $html->find("img");
                                     if ($images) {
                                         foreach ($images as $image) {
                                             $alt = $image->alt;
                                             if (empty($alt)) {
                                                 $nodeConfig["imgwithoutalt"]++;
                                             } else {
                                                 $nodeConfig["imgwithalt"]++;
                                             }
                                         }
                                     }
                                 }
                             }
                         } catch (Exception $e) {
                             Logger::debug($e);
                         }
                         if (strlen($childDocument->getTitle()) > 80 || strlen($childDocument->getTitle()) < 5 || strlen($childDocument->getDescription()) > 180 || strlen($childDocument->getDescription()) < 20 || $nodeConfig["h1"] != 1 || $nodeConfig["hx"] < 1) {
                             $nodeConfig["cls"] = "pimcore_document_seo_warning";
                         }
                     }
                     $documents[] = $nodeConfig;
                 }
             }
         }
     }
     $this->_helper->json($documents);
 }
Пример #3
0
 /**
  * Get a list of the Childs (not recursivly)
  *
  * @return array
  */
 public function getChilds()
 {
     if ($this->childs === null) {
         $list = new Document_List();
         $list->setCondition("parentId = ?", $this->getId());
         $list->setOrderKey("index");
         $list->setOrder("asc");
         $this->childs = $list->load();
     }
     return $this->childs;
 }
Пример #4
0
 public function updateAction()
 {
     $success = false;
     $allowUpdate = true;
     $document = Document::getById($this->_getParam("id"));
     // check for permissions
     $document->getPermissionsForUser($this->getUser());
     if ($document->isAllowed("settings")) {
         // if the position is changed the path must be changed || also from the childs
         if ($this->_getParam("parentId")) {
             $parentDocument = Document::getById($this->_getParam("parentId"));
             //check if parent is changed
             if ($document->getParentId() != $parentDocument->getId()) {
                 if (!$parentDocument->isAllowed("create")) {
                     throw new Exception("Prevented moving document - no create permission on new parent ");
                 }
                 $intendedPath = $parentDocument->getPath();
                 $pKey = $parentDocument->getKey();
                 if (!empty($pKey)) {
                     $intendedPath .= $parentDocument->getKey() . "/";
                 }
                 $documentWithSamePath = Document::getByPath($intendedPath . $document->getKey());
                 if ($documentWithSamePath != null) {
                     $allowUpdate = false;
                 }
             }
         }
         if ($allowUpdate) {
             if ($this->_getParam("key") || $this->_getParam("parentId")) {
                 $oldPath = $document->getPath() . $document->getKey();
             }
             $blockedVars = array("controller", "action", "module");
             if (!$document->isAllowed("rename") && $this->_getParam("key")) {
                 $blockedVars[] = "key";
                 Logger::debug("prevented renaming document because of missing permissions ");
             }
             foreach ($this->_getAllParams() as $key => $value) {
                 if (!in_array($key, $blockedVars)) {
                     $document->setValue($key, $value);
                 }
             }
             // if changed the index change also all documents on the same level
             if ($this->_getParam("index") !== null) {
                 $list = new Document_List();
                 $list->setCondition("parentId = ? AND id != ?", array($this->_getParam("parentId"), $document->getId()));
                 $list->setOrderKey("index");
                 $list->setOrder("asc");
                 $childsList = $list->load();
                 $count = 0;
                 foreach ($childsList as $child) {
                     if ($count == intval($this->_getParam("index"))) {
                         $count++;
                     }
                     $child->setIndex($count);
                     $child->save();
                     $count++;
                 }
             }
             $document->setUserModification($this->getUser()->getId());
             try {
                 $document->save();
                 $success = true;
             } catch (Exception $e) {
                 $this->_helper->json(array("success" => false, "message" => $e->getMessage()));
             }
         } else {
             Logger::debug("Prevented moving document, because document with same path+key already exists.");
         }
     } else {
         if ($document->isAllowed("rename") && $this->_getParam("key")) {
             //just rename
             try {
                 $document->setKey($this->_getParam("key"));
                 $document->save();
                 $success = true;
             } catch (Exception $e) {
                 $this->_helper->json(array("success" => false, "message" => $e->getMessage()));
             }
         } else {
             Logger::debug("Prevented update document, because of missing permissions.");
         }
     }
     $this->_helper->json(array("success" => $success));
 }