Beispiel #1
0
 /**
  * Get the value of a requested Page property
  *
  * @param string $key
  * @return mixed
  * @see __get
  *
  */
 public function get($key)
 {
     if (is_array($key)) {
         $key = implode('|', $key);
     }
     $value = null;
     switch ($key) {
         case 'parent_id':
         case 'parentID':
             $value = $this->parent ? $this->parent->id : 0;
             break;
         case 'child':
             $value = $this->child();
             break;
         case 'children':
         case 'subpages':
             // PW1
             $value = $this->children();
             break;
         case 'has_parent':
         case 'hasParent':
             $value = $this->parents();
             break;
         case 'parent':
         case 'parents':
         case 'rootParent':
         case 'siblings':
         case 'next':
         case 'prev':
         case 'url':
         case 'path':
         case 'outputFormatting':
         case 'isTrash':
             $value = $this->{$key}();
             break;
         case 'httpUrl':
         case 'httpURL':
             $value = $this->httpUrl();
             break;
         case 'fieldgroup':
         case 'fields':
             $value = $this->template->fieldgroup;
             break;
         case 'template_id':
         case 'templates_id':
         case 'templateID':
         case 'templatesID':
             $value = $this->template ? $this->template->id : 0;
             break;
         case 'template':
         case 'templatePrevious':
         case 'parentPrevious':
         case 'namePrevious':
         case 'statusPrevious':
         case 'isLoaded':
         case 'isNew':
         case 'pageNum':
         case 'instanceID':
             $value = $this->{$key};
             break;
         case 'out':
         case 'output':
             $value = $this->output();
             break;
         case 'filesManager':
             $value = $this->filesManager();
             break;
         case 'name':
             $value = $this->settings['name'];
             break;
         case 'modified_users_id':
         case 'modifiedUsersID':
         case 'modifiedUserID':
             $value = (int) $this->settings['modified_users_id'];
             break;
         case 'created_users_id':
         case 'createdUsersID':
         case 'createdUserID':
             $value = (int) $this->settings['created_users_id'];
             break;
         case 'modifiedUser':
             if (!$this->modifiedUser) {
                 if ($this->settings['modified_users_id'] == $this->wire('user')->id) {
                     $this->modifiedUser = $this->wire('user');
                 } else {
                     $this->modifiedUser = $this->wire('users')->get((int) $this->settings['modified_users_id']);
                 }
             }
             $this->modifiedUser->of($this->of());
             $value = $this->modifiedUser;
             break;
         case 'createdUser':
             if (!$this->createdUser) {
                 if ($this->settings['created_users_id'] == $this->wire('user')->id) {
                     $this->createdUser = $this->wire('user');
                 } else {
                     $this->createdUser = $this->wire('users')->get((int) $this->settings['created_users_id']);
                 }
             }
             $this->createdUser->of($this->of());
             $value = $this->createdUser;
             break;
         case 'urlSegment':
             $value = $this->wire('input')->urlSegment1;
             // deprecated, but kept for backwards compatibility
             break;
         case 'accessTemplate':
             $value = $this->getAccessTemplate();
             break;
         case 'num_children':
         case 'numChildren':
             $value = $this->settings['numChildren'];
             break;
         case 'numChildrenVisible':
         case 'numVisibleChildren':
         case 'hasChildren':
             $value = $this->numChildren(true);
             break;
         case 'editUrl':
         case 'editURL':
             $value = $this->wire('config')->urls->admin . "page/edit/?id={$this->id}";
             break;
         default:
             if ($key && isset($this->settings[(string) $key])) {
                 return $this->settings[$key];
             }
             if (($value = $this->getFieldFirstValue($key)) !== null) {
                 return $value;
             }
             if (($value = $this->getFieldValue($key)) !== null) {
                 return $value;
             }
             // if there is a selector, we'll assume they are using the get() method to get a child
             if (Selectors::stringHasOperator($key)) {
                 return $this->child($key);
             }
             // check if it's a field.subfield property, but only if output formatting is off
             if (!$this->outputFormatting() && strpos($key, '.') !== false && ($value = $this->getDot($key)) !== null) {
                 return $value;
             }
             // optionally let a hook look at it
             if (self::isHooked('Page::getUnknown()')) {
                 return $this->getUnknown($key);
             }
     }
     return $value;
 }
 /**
  * Does this WireArray have the given index or match the given selector?
  *
  * If the WireArray uses numeric keys, then this will also match a wire's "name" field.
  * 
  * @param int|string $key Key of item to check or selector.
  * @return bool True if the item exists, false if not. 
  */
 public function has($key)
 {
     if (is_object($key)) {
         $key = $this->getItemKey($key);
     }
     if (array_key_exists($key, $this->data)) {
         return true;
     }
     if (is_string($key)) {
         if (Selectors::stringHasOperator($key)) {
             $match = $this->findOne($key);
         } else {
             if ($this->usesNumericKeys()) {
                 $match = $this->getItemThatMatches('name', $key);
             }
         }
     } else {
         $match = null;
     }
     return $match ? true : false;
 }
 /**
  * Given a Selectors object or a selector string, return whether this Page matches it
  *
  * @param Page $page
  * @param string|Selectors $s
  * @return bool
  *
  */
 public function matches(Page $page, $s)
 {
     if (is_string($s) || is_int($s)) {
         if (ctype_digit("{$s}")) {
             $s = (int) $s;
         }
         if (is_string($s)) {
             // exit early for simple path comparison
             if (substr($s, 0, 1) == '/' && $page->path() == rtrim($s, '/') . '/') {
                 return true;
             }
             if (!Selectors::stringHasOperator($s)) {
                 return false;
             }
             $selectors = new Selectors($s);
         } else {
             if (is_int($s)) {
                 // exit early for simple ID comparison
                 return $page->id == $s;
             }
         }
     } else {
         if ($s instanceof Selectors) {
             $selectors = $s;
         } else {
             return false;
         }
     }
     $matches = false;
     foreach ($selectors as $selector) {
         $name = $selector->field;
         if (in_array($name, array('limit', 'start', 'sort', 'include'))) {
             continue;
         }
         $matches = true;
         $value = $page->getUnformatted($name);
         if (is_object($value)) {
             // if the current page value resolves to an object
             if ($value instanceof Page) {
                 // if it's a Page, get both the ID and path as allowed comparison values
                 $value = array($value->id, $value->path);
             } else {
                 if ($value instanceof PageArray) {
                     // if it's a PageArray, then get the ID and path of all of them
                     // @todo add support for @ selectors
                     $_value = array();
                     foreach ($value as $v) {
                         $_value[] = $v->id;
                         $_value[] = $v->path;
                     }
                     $value = $_value;
                 } else {
                     if ($value instanceof Template) {
                         $value = array($value->id, $value->name);
                     } else {
                         // otherwise just get the string value of the object
                         $value = "{$value}";
                     }
                 }
             }
         } else {
             if (is_array($value)) {
                 // ok: selector matches will accept an array
             } else {
                 // convert to a string value, whatever it may be
                 $value = "{$value}";
             }
         }
         if (!$selector->matches($value)) {
             $matches = false;
             break;
         }
     }
     return $matches;
 }
Beispiel #4
0
 /**
  * Given a Selectors object or a selector string, return whether this Page matches it
  *
  * @param string|Selectors $s
  * @return bool
  *
  */
 public function matches($s)
 {
     if (is_string($s)) {
         if (!Selectors::stringHasOperator($s)) {
             return false;
         }
         $selectors = new Selectors($s);
     } else {
         if ($s instanceof Selectors) {
             $selectors = $s;
         } else {
             return false;
         }
     }
     $matches = false;
     foreach ($selectors as $selector) {
         $name = $selector->field;
         if (in_array($name, array('limit', 'start', 'sort', 'include'))) {
             continue;
         }
         $matches = true;
         $value = $this->get($name);
         if (!$selector->matches("{$value}")) {
             $matches = false;
             break;
         }
     }
     return $matches;
 }
Beispiel #5
0
 /**
  * Get the value of a requested Page property
  *
  * @param string $key
  * @return mixed
  * @see __get
  *
  */
 public function get($key)
 {
     if (is_array($key)) {
         $key = implode('|', $key);
     }
     $value = null;
     switch ($key) {
         case 'parent_id':
         case 'parentID':
             $value = $this->parent ? $this->parent->id : 0;
             break;
         case 'child':
             $value = $this->child();
             break;
         case 'children':
         case 'subpages':
             // PW1
             $value = $this->children();
             break;
         case 'has_parent':
         case 'hasParent':
             $value = $this->parents();
             break;
         case 'parent':
         case 'parents':
         case 'rootParent':
         case 'siblings':
         case 'next':
         case 'prev':
         case 'url':
         case 'path':
         case 'outputFormatting':
         case 'isTrash':
             $value = $this->{$key}();
             break;
         case 'httpUrl':
         case 'httpURL':
             $value = $this->httpUrl();
             break;
         case 'fieldgroup':
         case 'fields':
             $value = $this->template->fieldgroup;
             break;
         case 'template_id':
         case 'templates_id':
         case 'templateID':
         case 'templatesID':
             $value = $this->template ? $this->template->id : 0;
             break;
         case 'template':
         case 'templatePrevious':
         case 'parentPrevious':
         case 'namePrevious':
         case 'statusPrevious':
         case 'isLoaded':
         case 'isNew':
         case 'pageNum':
         case 'instanceID':
             $value = $this->{$key};
             break;
         case 'out':
         case 'output':
             $value = $this->output();
             break;
         case 'filesManager':
             $value = $this->filesManager();
             break;
         case 'name':
             $value = $this->settings['name'];
             break;
         case 'modified_users_id':
         case 'modifiedUsersID':
         case 'modifiedUserID':
             $value = (int) $this->settings['modified_users_id'];
             break;
         case 'created_users_id':
         case 'createdUsersID':
         case 'createdUserID':
             $value = (int) $this->settings['created_users_id'];
             break;
         case 'modifiedUser':
         case 'createdUser':
             if (!$this->{$key}) {
                 $_key = str_replace('User', '', $key) . '_users_id';
                 $u = $this->wire('user');
                 if ($this->settings[$_key] == $u->id) {
                     $this->set($key, $u);
                     // prevent possible recursion loop
                 } else {
                     $u = $this->wire('users')->get((int) $this->settings[$_key]);
                     $this->set($key, $u);
                 }
             }
             $value = $this->{$key};
             if ($value) {
                 $value->of($this->of());
             }
             break;
         case 'urlSegment':
             $value = $this->wire('input')->urlSegment1;
             // deprecated, but kept for backwards compatibility
             break;
         case 'accessTemplate':
             $value = $this->getAccessTemplate();
             break;
         case 'num_children':
         case 'numChildren':
             $value = $this->settings['numChildren'];
             break;
         case 'numChildrenVisible':
         case 'numVisibleChildren':
         case 'hasChildren':
             $value = $this->numChildren(true);
             break;
         case 'editUrl':
         case 'editURL':
             $value = $this->editUrl();
             break;
         case 'statusStr':
             $value = implode(' ', $this->status(true));
             break;
         case 'modifiedStr':
         case 'createdStr':
         case 'publishedStr':
             $value = $this->settings[str_replace('Str', '', $key)];
             $value = $value ? wireDate($this->wire('config')->dateFormat, $value) : '';
             break;
         default:
             if ($key && isset($this->settings[(string) $key])) {
                 return $this->settings[$key];
             }
             // populate a formatted string with {tag} vars
             if (strpos($key, '{') !== false && strpos($key, '}')) {
                 return $this->getMarkup($key);
             }
             if (($value = $this->getFieldFirstValue($key)) !== null) {
                 return $value;
             }
             if (($value = $this->getFieldValue($key)) !== null) {
                 return $value;
             }
             // if there is a selector, we'll assume they are using the get() method to get a child
             if (Selectors::stringHasOperator($key)) {
                 return $this->child($key);
             }
             // check if it's a field.subfield property
             if (strpos($key, '.') && ($value = $this->getFieldSubfieldValue($key)) !== null) {
                 return $value;
             }
             // optionally let a hook look at it
             if (self::isHooked('Page::getUnknown()')) {
                 $value = $this->getUnknown($key);
             }
     }
     return $value;
 }
 /** 
  * Does this page have the specified status number or template name? 
  *
  * See status flag constants at top of Page class
  *
  * @param int|string $status Status number or Template name
  * @return bool
  *
  */
 public function is($status)
 {
     if (is_int($status)) {
         return $this->status & $status;
     } else {
         if (is_string($status) && $this->sanitizer->name($status) == $status) {
             // valid template name
             if ($this->template->name == $status) {
                 return true;
             }
         } else {
             if (Selectors::stringHasOperator($status)) {
                 $matches = false;
                 $selectors = new Selectors($status);
                 foreach ($selectors as $selector) {
                     $matches = true;
                     $value = $this->get($selector->field);
                     if (!$selector->matches("{$value}")) {
                         $matches = false;
                         break;
                     }
                 }
                 return $matches;
             }
         }
     }
     return false;
 }