/** * Convert any value to a string * * @param mixed $value * @return string * */ protected function valueToString($value) { if (is_object($value) && ($value instanceof Pagefiles || $value instanceof Pagefile)) { return $this->objectToString($value); } else { if (WireArray::iterable($value)) { return $this->arrayToString($value); } else { if (is_object($value)) { return $this->objectToString($value); } else { return $this->wire('sanitizer')->entities1($value); } } } }
/** * Add one or more of parents that this PagesType represents * * @param array|int|string|Page $parents Single or array of Page objects, IDs, or paths * */ public function addParents($parents) { if (!WireArray::iterable($parents)) { $parents = array($parents); } foreach ($parents as $parent) { if (is_int($parent)) { $id = $parent; } else { if (is_string($parent) && ctype_digit($parent)) { $id = (int) $parent; } else { if (is_string($parent)) { $parent = $this->wire('pages')->findOne($parent, array('loadOptions' => array('autojoin' => false))); $id = $parent->id; } else { if (is_object($parent) && $parent instanceof Page) { $id = $parent->id; } } } } if ($id) { $this->parents[$id] = $id; } } if (empty($this->parent_id)) { $this->parent_id = reset($this->parents); } // legacy deprecated }
/** * Set the roles that are allowed to view or edit this field on pages * * Applicable only if the flagAccess is set to this field's flags. * * @param string $type Must be either "view" or "edit" * @param PageArray|array|null $roles May be a PageArray of Role objects or an array of Role IDs * * @throws WireException if given invalid argument * */ public function setRoles($type, $roles) { if (empty($roles)) { $roles = array(); } if (!WireArray::iterable($roles)) { throw new WireException("setRoles expects PageArray or array of Role IDs"); } $ids = array(); foreach ($roles as $role) { if (is_int($role) || is_string($role) && ctype_digit("{$role}")) { $ids[] = (int) $role; } else { if ($role instanceof Role) { $ids[] = (int) $role->id; } } } if ($type == 'view') { $guestID = $this->wire('config')->guestUserRolePageID; // if guest is present, then that's inclusive of all, no need to store others in viewRoles if (in_array($guestID, $ids)) { $ids = array($guestID); } if ($this->viewRoles != $ids) { $this->viewRoles = $ids; $this->trackChange('viewRoles'); } } else { if ($type == 'edit') { if ($this->editRoles != $ids) { $this->editRoles = $ids; $this->trackChange('editRoles'); } } else { throw new WireException("setRoles expects either 'view' or 'edit' (arg 0)"); } } }
/** * Given an array or CSV string of Page IDs, return a PageArray * * Optionally specify an $options array rather than a template for argument 2. When present, the 'template' and 'parent_id' arguments may be provided * in the given $options array. These options may be specified: * * LOAD OPTIONS (argument 2 array): * - cache: boolean, default=true. place loaded pages in memory cache? * - getFromCache: boolean, default=true. Allow use of previously cached pages in memory (rather than re-loading it from DB)? * - template: instance of Template (see $template argument) * - parent_id: integer (see $parent_id argument) * - getNumChildren: boolean, default=true. Specify false to disable retrieval and population of 'numChildren' Page property. * - getOne: boolean, default=false. Specify true to return just one Page object, rather than a PageArray. * - autojoin: boolean, default=true. Allow use of autojoin option? * - joinFields: array, default=empty. Autojoin the field names specified in this array, regardless of field settings (requires autojoin=true). * - joinSortfield: boolean, default=true. Whether the 'sortfield' property will be joined to the page. * - findTemplates: boolean, default=true. Determine which templates will be used (when no template specified) for more specific autojoins. * - pageClass: string, default=auto-detect. Class to instantiate Page objects with. Leave blank to determine from template. * - pageArrayClass: string, default=PageArray. PageArray-derived class to store pages in (when 'getOne' is false). * * Use the $options array for potential speed optimizations: * - Specify a 'template' with your call, when possible, so that this method doesn't have to determine it separately. * - Specify false for 'getNumChildren' for potential speed optimization when you know for certain pages will not have children. * - Specify false for 'autojoin' for potential speed optimization in certain scenarios (can also be a bottleneck, so be sure to test). * - Specify false for 'joinSortfield' for potential speed optimization when you know the Page will not have children or won't need to know the order. * - Specify false for 'findTemplates' so this method doesn't have to look them up. Potential speed optimization if you have few autojoin fields globally. * - Note that if you specify false for 'findTemplates' the pageClass is assumed to be 'Page' unless you specify something different for the 'pageClass' option. * * @param array|WireArray|string $_ids Array of IDs or CSV string of IDs * @param Template|array|null $template Specify a template to make the load faster, because it won't have to attempt to join all possible fields... just those used by the template. * Optionally specify an $options array instead, see the method notes above. * @param int|null $parent_id Specify a parent to make the load faster, as it reduces the possibility for full table scans. * This argument is ignored when an options array is supplied for the $template. * @return PageArray|Page Returns Page only if the 'getOne' option is specified, otherwise always returns a PageArray. * @throws WireException * */ public function getById($_ids, $template = null, $parent_id = null) { static $instanceID = 0; $options = array('cache' => true, 'getFromCache' => true, 'template' => null, 'parent_id' => null, 'getNumChildren' => true, 'getOne' => false, 'autojoin' => true, 'findTemplates' => true, 'joinSortfield' => true, 'joinFields' => array(), 'pageClass' => '', 'pageArrayClass' => 'PageArray'); if (is_array($template)) { // $template property specifies an array of options $options = array_merge($options, $template); $template = $options['template']; $parent_id = $options['parent_id']; } else { if (!is_null($template) && !$template instanceof Template) { throw new WireException('getById argument 2 must be Template or $options array'); } } $pageArrayClass = $options['pageArrayClass']; if (!is_null($parent_id) && !is_int($parent_id)) { // convert Page object or string to integer id $parent_id = (int) (string) $parent_id; } if (!is_null($template) && !is_object($template)) { // convert template string or id to Template object $template = $this->wire('templates')->get($template); } if (is_string($_ids)) { // convert string of IDs to array if (strpos($_ids, '|') !== false) { $_ids = explode('|', $_ids); } else { $_ids = explode(",", $_ids); } } else { if (is_int($_ids)) { $_ids = array($_ids); } } if (!WireArray::iterable($_ids) || !count($_ids)) { // return blank if $_ids isn't iterable or is empty return $options['getOne'] ? new NullPage() : new $pageArrayClass(); } if (is_object($_ids)) { $_ids = $_ids->getArray(); } // ArrayObject or the like $loaded = array(); // array of id => Page objects that have been loaded $ids = array(); // sanitized version of $_ids // sanitize ids and determine which pages we can pull from cache foreach ($_ids as $key => $id) { $id = (int) $id; if ($id < 1) { continue; } if ($options['getFromCache'] && ($page = $this->getCache($id))) { // page is already available in the cache $loaded[$id] = $page; } else { if (isset(Page::$loadingStack[$id])) { // if the page is already in the process of being loaded, point to it rather than attempting to load again. // the point of this is to avoid a possible infinite loop with autojoin fields referencing each other. $loaded[$id] = Page::$loadingStack[$id]; // cache the pre-loaded version so that other pages referencing it point to this instance rather than loading again $this->cache($loaded[$id]); } else { $loaded[$id] = ''; // reserve the spot, in this order $ids[(int) $key] = $id; // queue id to be loaded } } } $idCnt = count($ids); // idCnt contains quantity of remaining page ids to load if (!$idCnt) { // if there are no more pages left to load, we can return what we've got if ($options['getOne']) { return count($loaded) ? reset($loaded) : new NullPage(); } $pages = new $pageArrayClass(); $pages->import($loaded); return $pages; } $database = $this->wire('database'); $idsByTemplate = array(); if (is_null($template) && $options['findTemplates']) { // template was not defined with the function call, so we determine // which templates are used by each of the pages we have to load $sql = "SELECT id, templates_id FROM pages WHERE "; if ($idCnt == 1) { $sql .= "id=" . (int) reset($ids); } else { $sql .= "id IN(" . implode(",", $ids) . ")"; } $query = $database->prepare($sql); $result = $this->executeQuery($query); if ($result) { while ($row = $query->fetch(PDO::FETCH_NUM)) { list($id, $templates_id) = $row; $id = (int) $id; $templates_id = (int) $templates_id; if (!isset($idsByTemplate[$templates_id])) { $idsByTemplate[$templates_id] = array(); } $idsByTemplate[$templates_id][] = $id; } } $query->closeCursor(); } else { if (is_null($template)) { // no template provided, and autojoin not needed (so we don't need to know template) $idsByTemplate = array(0 => $ids); } else { // template was provided $idsByTemplate = array($template->id => $ids); } } foreach ($idsByTemplate as $templates_id => $ids) { if ($templates_id && (!$template || $template->id != $templates_id)) { $template = $this->wire('templates')->get($templates_id); } if ($template) { $fields = $template->fieldgroup; } else { $fields = $this->wire('fields'); } $query = new DatabaseQuerySelect(); $sortfield = $template ? $template->sortfield : ''; $joinSortfield = empty($sortfield) && $options['joinSortfield']; $query->select("false AS isLoaded, pages.templates_id AS templates_id, pages.*, " . ($joinSortfield ? 'pages_sortfields.sortfield, ' : '') . ($options['getNumChildren'] ? '(SELECT COUNT(*) FROM pages AS children WHERE children.parent_id=pages.id) AS numChildren' : '')); if ($joinSortfield) { $query->leftjoin('pages_sortfields ON pages_sortfields.pages_id=pages.id'); } $query->groupby('pages.id'); if ($options['autojoin'] && $this->autojoin) { foreach ($fields as $field) { if (!empty($options['joinFields']) && in_array($field->name, $options['joinFields'])) { // joinFields option specified to force autojoin this field } else { if (!($field->flags & Field::flagAutojoin)) { continue; } // autojoin not enabled for field if ($fields instanceof Fields && !($field->flags & Field::flagGlobal)) { continue; } // non-fieldgroup, autojoin only if global flag is set } $table = $database->escapeTable($field->table); if (!$field->type || !$field->type->getLoadQueryAutojoin($field, $query)) { continue; } // autojoin not allowed $query->leftjoin("{$table} ON {$table}.pages_id=pages.id"); // QA } } if (!is_null($parent_id)) { $query->where("pages.parent_id=" . (int) $parent_id); } if ($template) { $query->where("pages.templates_id=" . (int) $template->id); } // QA $query->where("pages.id IN(" . implode(',', $ids) . ") "); // QA $query->from("pages"); $stmt = $query->prepare(); $this->executeQuery($stmt); $class = $options['pageClass']; if (empty($class)) { if ($template) { $class = $template->pageClass && class_exists($template->pageClass) ? $template->pageClass : 'Page'; } else { $class = 'Page'; } } if ($class != 'Page' && !class_exists($class)) { $this->error("Class '{$class}' for Pages::getById() does not exist", Notice::log); $class = 'Page'; } try { while ($page = $stmt->fetchObject($class, array($template))) { $page->instanceID = ++$instanceID; $page->setIsLoaded(true); $page->setIsNew(false); $page->setTrackChanges(true); $page->setOutputFormatting($this->outputFormatting); $loaded[$page->id] = $page; if ($options['cache']) { $this->cache($page); } } } catch (Exception $e) { $error = $e->getMessage() . " [pageClass={$class}, template={$template}]"; if ($this->wire('user')->isSuperuser()) { $this->error($error); } $this->wire('log')->error($error); $this->trackException($e, false); } $stmt->closeCursor(); $template = null; } if ($options['getOne']) { return count($loaded) ? reset($loaded) : new NullPage(); } $pages = new $pageArrayClass(); return $pages->import($loaded); }
/** * Given an array or CSV string of Page IDs, return a PageArray * * @param array|WireArray|string $ids Array of IDs or CSV string of IDs * @param Template $template Specify a template to make the load faster, because it won't have to attempt to join all possible fields... just those used by the template. * @param int $parent_id Specify a parent to make the load faster, as it reduces the possibility for full table scans * @return PageArray * */ public function getById($ids, Template $template = null, $parent_id = null) { static $instanceID = 0; $pages = new PageArray(); if (is_string($ids)) { $ids = explode(",", $ids); } if (!WireArray::iterable($ids) || !count($ids)) { return $pages; } if (is_object($ids)) { $ids = $ids->getArray(); } $loaded = array(); foreach ($ids as $key => $id) { $id = (int) $id; $ids[$key] = $id; if ($page = $this->getCache($id)) { $loaded[$id] = $page; unset($ids[$key]); } else { if (isset(Page::$loadingStack[$id])) { // if the page is already in the process of being loaded, point to it rather than attempting to load again. // the point of this is to avoid a possible infinite loop with autojoin fields referencing each other. $loaded[$id] = Page::$loadingStack[$id]; // cache the pre-loaded version so that other pages referencing it point to this instance rather than loading again $this->cache($loaded[$id]); unset($ids[$key]); } else { $loaded[$id] = ''; // reserve the spot, in this order } } } $idCnt = count($ids); if (!$idCnt) { return $pages->import($loaded); } $idsByTemplate = array(); if (is_null($template)) { $sql = "SELECT id, templates_id FROM pages WHERE "; if ($idCnt == 1) { $sql .= "id=" . (int) reset($ids); } else { $sql .= "id IN(" . implode(",", $ids) . ")"; } $result = $this->db->query($sql); if ($result && $result->num_rows) { while ($row = $result->fetch_row()) { list($id, $templates_id) = $row; if (!isset($idsByTemplate[$templates_id])) { $idsByTemplate[$templates_id] = array(); } $idsByTemplate[$templates_id][] = $id; } } $result->free(); } else { $idsByTemplate = array($template->id => $ids); } foreach ($idsByTemplate as $templates_id => $ids) { if (!$template || $template->id != $templates_id) { $template = $this->fuel('templates')->get($templates_id); } $fields = $template->fieldgroup; $query = new DatabaseQuerySelect(); $query->select("false AS isLoaded, pages.templates_id AS templates_id, pages.*, pages_sortfields.sortfield, " . "(SELECT COUNT(*) FROM pages AS children WHERE children.parent_id=pages.id) AS numChildren"); $query->leftjoin("pages_sortfields ON pages_sortfields.pages_id=pages.id"); $query->groupby("pages.id"); foreach ($fields as $field) { if (!($field->flags & Field::flagAutojoin)) { continue; } $table = $field->table; if (!$field->type->getLoadQueryAutojoin($field, $query)) { continue; } // autojoin not allowed $query->leftjoin("{$table} ON {$table}.pages_id=pages.id"); } if (!is_null($parent_id)) { $query->where("pages.parent_id=" . (int) $parent_id); } $query->where("pages.templates_id={$template->id}"); $query->where("pages.id IN(" . implode(',', $ids) . ") "); $query->from("pages"); if (!($result = $query->execute())) { throw new WireException($this->db->error); } $class = $template->pageClass && class_exists($template->pageClass) ? $template->pageClass : 'Page'; while ($page = $result->fetch_object($class, array($template))) { $page->instanceID = ++$instanceID; $page->setIsLoaded(true); $page->setIsNew(false); $page->setTrackChanges(true); $page->setOutputFormatting($this->outputFormatting); $loaded[$page->id] = $page; $this->cache($page); } $template = null; $result->free(); } return $pages->import($loaded); }
/** * Given an array of page IDs or a PageArray, sets the roles for this template * * @param array|PageArray $value * @param string Default is 'view', but you may also specify 'edit', 'create', or 'add' to set that type * */ public function setRoles($value, $type = 'view') { if (strpos($type, 'page-') === 0) { $type = str_replace('page-', '', $type); } if ($type == 'view') { if (is_array($value) || $value instanceof PageArray) { $this->_roles = $value; } } else { if (WireArray::iterable($value)) { $roleIDs = array(); foreach ($value as $v) { if (is_int($v)) { $id = $v; } else { if (is_string($v) && ctype_digit($v)) { $id = (int) $v; } else { if ($v instanceof Page) { $id = $v->id; } else { continue; } } } $roleIDs[] = $id; } if ($type == 'edit') { $this->set('editRoles', $roleIDs); } else { if ($type == 'create') { $this->set('createRoles', $roleIDs); } else { if ($type == 'add') { $this->set('addRoles', $roleIDs); } } } } } }
/** * Given an array or CSV string of Page IDs, return a PageArray * * Optionally specify an $options array rather than a template for argument 2. When present, the 'template' and 'parent_id' arguments may be provided * in the given $options array. These options may be specified: * * - template: instance of Template (see $template argument) * - parent_id: integer (see $parent_id argument) * - getNumChildren: boolean, default=true. Specify false to disable retrieval and population of 'numChildren' Page property. * * @param array|WireArray|string $ids Array of IDs or CSV string of IDs * @param Template|array|null $template Specify a template to make the load faster, because it won't have to attempt to join all possible fields... just those used by the template. * Optionally specify an $options array instead, see the method notes above. * @param int|null $parent_id Specify a parent to make the load faster, as it reduces the possibility for full table scans. * This argument is ignored when an options array is supplied for the $template. * @return PageArray * @throws WireException * */ public function getById($ids, $template = null, $parent_id = null) { $options = array('template' => null, 'parent_id' => null, 'getNumChildren' => true); if (is_array($template)) { // $template property specifies an array of options $options = array_merge($options, $template); $template = $options['template']; $parent_id = $options['parent_id']; } else { if (!is_null($template) && !$template instanceof Template) { throw new WireException('getById argument 2 must be Template or $options array'); } } static $instanceID = 0; $database = $this->wire('database'); $pages = new PageArray(); if (is_string($ids)) { $ids = explode(",", $ids); } if (!WireArray::iterable($ids) || !count($ids)) { return $pages; } if (is_object($ids)) { $ids = $ids->getArray(); } $loaded = array(); foreach ($ids as $key => $id) { $id = (int) $id; $ids[$key] = $id; if ($page = $this->getCache($id)) { $loaded[$id] = $page; unset($ids[$key]); } else { if (isset(Page::$loadingStack[$id])) { // if the page is already in the process of being loaded, point to it rather than attempting to load again. // the point of this is to avoid a possible infinite loop with autojoin fields referencing each other. $loaded[$id] = Page::$loadingStack[$id]; // cache the pre-loaded version so that other pages referencing it point to this instance rather than loading again $this->cache($loaded[$id]); unset($ids[$key]); } else { $loaded[$id] = ''; // reserve the spot, in this order } } } $idCnt = count($ids); if (!$idCnt) { return $pages->import($loaded); } $idsByTemplate = array(); if (is_null($template)) { $sql = "SELECT id, templates_id FROM pages WHERE "; if ($idCnt == 1) { $sql .= "id=" . (int) reset($ids); } else { $sql .= "id IN(" . implode(",", $ids) . ")"; } $query = $database->prepare($sql); $result = $query->execute(); if ($result) { while ($row = $query->fetch(PDO::FETCH_NUM)) { list($id, $templates_id) = $row; if (!isset($idsByTemplate[$templates_id])) { $idsByTemplate[$templates_id] = array(); } $idsByTemplate[$templates_id][] = $id; } } $query->closeCursor(); } else { $idsByTemplate = array($template->id => $ids); } foreach ($idsByTemplate as $templates_id => $ids) { if (!$template || $template->id != $templates_id) { $template = $this->wire('templates')->get($templates_id); } $fields = $template->fieldgroup; $query = new DatabaseQuerySelect(); $joinSortfield = empty($template->sortfield); $query->select("false AS isLoaded, pages.templates_id AS templates_id, pages.*, " . ($joinSortfield ? 'pages_sortfields.sortfield, ' : '') . ($options['getNumChildren'] ? '(SELECT COUNT(*) FROM pages AS children WHERE children.parent_id=pages.id) AS numChildren' : '')); if ($joinSortfield) { $query->leftjoin('pages_sortfields ON pages_sortfields.pages_id=pages.id'); } $query->groupby('pages.id'); foreach ($fields as $field) { if (!($field->flags & Field::flagAutojoin)) { continue; } $table = $database->escapeTable($field->table); if (!$field->type || !$field->type->getLoadQueryAutojoin($field, $query)) { continue; } // autojoin not allowed $query->leftjoin("{$table} ON {$table}.pages_id=pages.id"); // QA } if (!is_null($parent_id)) { $query->where("pages.parent_id=" . (int) $parent_id); } $query->where("pages.templates_id=" . (int) $template->id); // QA $query->where("pages.id IN(" . implode(',', $ids) . ") "); // QA $query->from("pages"); $stmt = $query->execute(); if ($stmt->errorCode() > 0) { $errorInfo = $result->errorInfo(); throw new WireException($errorInfo[2]); } $class = $template->pageClass && class_exists($template->pageClass) ? $template->pageClass : 'Page'; while ($page = $stmt->fetchObject($class, array($template))) { $page->instanceID = ++$instanceID; $page->setIsLoaded(true); $page->setIsNew(false); $page->setTrackChanges(true); $page->setOutputFormatting($this->outputFormatting); $loaded[$page->id] = $page; $this->cache($page); } $stmt->closeCursor(); $template = null; } return $pages->import($loaded); }
/** * Given an array or CSV string of Page IDs, return a PageArray * * @param array|WireArray|string $ids Array of IDs or CSV string of IDs * @param Template $template Specify a template to make the load faster, because it won't have to attempt to join all possible fields... just those used by the template. * @param int $parent_id Specify a parent to make the load faster, as it reduces the possibility for full table scans * @return PageArray * */ public function getById($ids, Template $template = null, $parent_id = null) { static $instanceID = 0; $pages = new PageArray(); if (is_string($ids)) { $ids = explode(",", $ids); } if (!WireArray::iterable($ids) || !count($ids)) { return $pages; } if (is_object($ids)) { $ids = $ids->getArray(); } $loaded = array(); foreach ($ids as $key => $id) { $id = (int) $id; $ids[$key] = $id; if ($page = $this->getCache($id)) { $loaded[$id] = $page; unset($ids[$key]); } else { $loaded[$id] = ''; // reserve the spot, in this order } } $idCnt = count($ids); $fields = is_null($template) ? $this->fuel->fields : $template->fieldgroup; if ($idCnt) { // Optimization to only load the fields specific to the page, if just one page. // Without it, all autojoin fields have to be attempted, whether they are applicable to the pages loaded or not. // Even though this increases queries, it does result in a slight overall speed and memory improvement. if (is_null($template) && $idCnt == 1) { $result = $this->db->query("SELECT templates_id FROM pages WHERE id=" . (int) reset($ids)); if ($result) { list($tpl_id) = $result->fetch_row(); $template = $this->fuel('templates')->get($tpl_id); if ($template) { $fields = $template->fieldgroup; } $result->free(); } } $query = new DatabaseQuerySelect(); $query->select("false AS isLoaded, pages.templates_id AS templates_id, pages.*, pages_sortfields.sortfield, " . "(SELECT COUNT(*) FROM pages AS children WHERE children.parent_id=pages.id) AS numChildren"); $query->leftjoin("pages_sortfields ON pages_sortfields.pages_id=pages.id"); $query->groupby("pages.id"); foreach ($fields as $field) { if (!($field->flags & Field::flagAutojoin)) { continue; } //if($field->type instanceof FieldtypeMulti) continue; $table = $field->table; // if($field->type instanceof FieldtypeMulti) { // $sel .= "(SELECT COUNT(*) FROM $table WHERE $table.pages_id=pages.id) AS {$field->name}, "; // } else { if (!$field->type->getLoadQueryAutojoin($field, $query)) { continue; } // autojoin not allowed // $query->select("$table.data AS {$field->name}"); $query->leftjoin("{$table} ON {$table}.pages_id=pages.id"); } if (!is_null($parent_id)) { $query->where("pages.parent_id=" . (int) $parent_id); } if (!is_null($template)) { $query->where("pages.templates_id={$template->id}"); } $query->where("pages.id IN(" . implode(',', $ids) . ") "); $query->from("pages"); if (!($result = $query->execute())) { throw new WireException($this->db->error); } while ($page = $result->fetch_object('Page', array($template))) { $page->instanceID = ++$instanceID; $page->setIsLoaded(true); $page->setIsNew(false); $page->setTrackChanges(true); $page->setOutputFormatting($this->outputFormatting); $loaded[$page->id] = $page; $this->cache($page); } $result->free(); } $pages->import($loaded); return $pages; }