Example #1
0
 public function testConstruction()
 {
     $kirby = $this->kirbyInstance();
     $site = $this->siteInstance($kirby);
     $page = new Page($site, '1-a');
     $this->assertInstanceOf('Kirby', $page->kirby());
     $this->assertEquals($kirby, $page->kirby());
     $this->assertInstanceOf('Site', $page->site());
     $this->assertEquals($site, $page->site());
     $this->assertInstanceOf('Site', $page->parent());
     $this->assertEquals($site, $page->parent());
     $this->assertEquals('1-a', $page->dirname());
     $this->assertEquals(1, $page->depth());
     $this->assertEquals($kirby->roots()->content() . DS . '1-a', $page->root());
     $this->assertEquals('1', $page->num());
     $this->assertEquals('a', $page->uid());
     $this->assertEquals('a', $page->id());
     $this->assertEquals('1-a', $page->diruri());
     $this->assertEquals('/a', $page->url());
     $this->assertTrue($page->isCachable());
     $this->assertEquals('a', $page->slug());
     $this->assertTrue($page->is($page));
     $this->assertTrue($page->equals($page));
     $this->assertFalse($page->isSite());
     $this->assertFalse($page->isActive());
     $this->assertFalse($page->isOpen());
     $this->assertTrue($page->isVisible());
     $this->assertFalse($page->isInvisible());
     $this->assertFalse($page->isHomePage());
     $this->assertFalse($page->isErrorPage());
     $this->assertEquals($page->id(), (string) $page);
 }
Example #2
0
 /**
  * Is the given page in a state where it can be saved?
  *
  * @param Page $page
  * @param string $reason Text containing the reason why it can't be saved (assuming it's not saveable)
  * @return bool True if saveable, False if not
  *
  */
 public function isSaveable(Page $page, &$reason)
 {
     $saveable = false;
     $outputFormattingReason = "Call \$page->setOutputFormatting(false) before getting/setting values that will be modified and saved. ";
     if ($page instanceof NullPage) {
         $reason = "Pages of type NullPage are not saveable";
     } else {
         if ((!$page->parent || $page->parent instanceof NullPage) && $page->id !== 1) {
             $reason = "It has no parent assigned";
         } else {
             if (!$page->template) {
                 $reason = "It has no template assigned";
             } else {
                 if (!strlen(trim($page->name))) {
                     $reason = "It has an empty 'name' field";
                 } else {
                     if ($page->is(Page::statusCorrupted)) {
                         $reason = $outputFormattingReason . " [Page::statusCorrupted]";
                     } else {
                         if ($page->id == 1 && !$page->template->useRoles) {
                             $reason = "Selected homepage template cannot be used because it does not define access.";
                         } else {
                             if ($page->id == 1 && !$page->template->hasRole('guest')) {
                                 $reason = "Selected homepage template cannot be used because it does not have the required 'guest' role in it's access settings.";
                             } else {
                                 $saveable = true;
                             }
                         }
                     }
                 }
             }
         }
     }
     // check if they could corrupt a field by saving
     if ($saveable && $page->outputFormatting) {
         // iternate through recorded changes to see if any custom fields involved
         foreach ($page->getChanges() as $change) {
             if ($page->template->fieldgroup->getField($change) !== null) {
                 $reason = $outputFormattingReason . " [{$change}]";
                 $saveable = false;
                 break;
             }
         }
         // iterate through already-loaded data to see if any are objects that have changed
         if ($saveable) {
             foreach ($page->getArray() as $key => $value) {
                 if (!$page->template->fieldgroup->getField($key)) {
                     continue;
                 }
                 if (is_object($value) && $value instanceof Wire && $value->isChanged()) {
                     $reason = $outputFormattingReason . " [{$key}]";
                     $saveable = false;
                     break;
                 }
             }
         }
     }
     // check for a parent change
     if ($saveable && $page->parentPrevious && $page->parentPrevious->id != $page->parent->id) {
         // page was moved
         if ($page->template->noMove && ($page->is(Page::statusSystem) || $page->is(Page::statusSystemID) || !$page->isTrash())) {
             // make sure the page's template allows moves. only move laways allowed is to the trash, unless page has system status
             $saveable = false;
             $reason = "Pages using template '{$page->template}' are not moveable (template::noMove)";
         } else {
             if ($page->parent->template->noChildren) {
                 $saveable = false;
                 $reason = "Chosen parent '{$page->parent->path}' uses template that does not allow children.";
             } else {
                 if ($page->parent->id && $page->parent->id != $this->config->trashPageID && count($page->parent->template->childTemplates) && !in_array($page->template->id, $page->parent->template->childTemplates)) {
                     // make sure the new parent's template allows pages with this template
                     $saveable = false;
                     $reason = "Can't move '{$page->name}' because Template '{$page->parent->template}' used by '{$page->parent->path}' doesn't allow children with this template.";
                 } else {
                     if (count($page->template->parentTemplates) && $page->parent->id != $this->config->trashPageID && !in_array($page->parent->template->id, $page->template->parentTemplates)) {
                         $saveable = false;
                         $reason = "Can't move '{$page->name}' because Template '{$page->parent->template}' used by '{$page->parent->path}' is not allowed by template '{$page->template->name}'.";
                     } else {
                         if (count($page->parent->children("name={$page->name},status<" . Page::statusMax))) {
                             $saveable = false;
                             $reason = "Chosen parent '{$page->parent->path}' already has a page named '{$page->name}'";
                         }
                     }
                 }
             }
         }
     }
     return $saveable;
 }
 /**
  * Is the given page in a state where it can be saved?
  *
  * @param Page $page
  * @param string $reason Text containing the reason why it can't be saved (assuming it's not saveable)
  * @return bool True if saveable, False if not
  *
  */
 public function isSaveable(Page $page, &$reason)
 {
     $saveable = false;
     if ($page instanceof NullPage) {
         $reason = "Pages of type NullPage are not saveable";
     } else {
         if ((!$page->parent || $page->parent instanceof NullPage) && $page->id !== 1) {
             $reason = "It has no parent assigned";
         } else {
             if (!$page->template) {
                 $reason = "It has no template assigned";
             } else {
                 if (!strlen(trim($page->name))) {
                     $reason = "It has an empty 'name' field";
                 } else {
                     if ($page->outputFormatting) {
                         $reason = "outputFormatting is on - Call \$page->setOutputFormatting(false) to turn it off";
                     } else {
                         if ($page->is(Page::statusCorrupted)) {
                             $reason = "It was corrupted when you modified a field with outputFormatting - See Page::setOutputFormatting(false)";
                         } else {
                             $saveable = true;
                         }
                     }
                 }
             }
         }
     }
     return $saveable;
 }