/** * Update items to make sure they are in same order specified in GET var InputfieldPageTableSort * * @param Page $page * @param Field $field * @param string $sort CSV string * */ protected function sortItems(Page $page, Field $field, $sort) { // if this field has it's own sort settings, then we have nothing to do here. if ($field->sortfields && $field->sortfields != 'sort') { return; } $value = $page->getUnformatted($field->name); if (!$value instanceof PageArray || !$value->count()) { return; } $sortedIDs = explode(',', $sort); $test = implode('|', $sortedIDs); if ($test == (string) $value) { return; } // already in right order? foreach ($value as $item) { $sort = array_search($item->id, $sortedIDs); if ($sort === false) { $sort = count($value); } $item->set('_pagetable_sort', $sort); } $value->sort('_pagetable_sort'); }
/** * 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; }
/** * Handler for the InputfieldPageTableAdd ajax action * * @param Page $page * @param Field $field * @param Page $item * @return bool * */ protected function addItem(Page $page, Field $field, Page $item) { // add an item and save the field if (!$item->id || $item->createdUser->id != $this->wire('user')->id) { return false; } $value = $page->getUnformatted($field->name); if ($value instanceof PageArray && !$value->has($item)) { $of = $page->of(); $page->of(false); $value->add($item); $page->set($field->name, $value); $page->save($field->name); $this->notes = $this->_('Added item') . ' - ' . $item->name; $page->of($of); return true; } return false; }