1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-09 08:17:12 +02:00

Update FieldtypePage.getMatchQuery to inherit "include=" and "check_access=0" settings from PageFinder that called upon it. Update PageFinder to support this kind of communication with Fieldtypes.

This commit is contained in:
Ryan Cramer
2021-03-19 14:05:42 -04:00
parent d0b404b752
commit a9b8d37c58
2 changed files with 36 additions and 6 deletions

View File

@@ -15,6 +15,9 @@
* @method string getQueryAllowedTemplatesWhere(DatabaseQuerySelect $query, $where) * @method string getQueryAllowedTemplatesWhere(DatabaseQuerySelect $query, $where)
* @method void getQueryJoinPath(DatabaseQuerySelect $query, $selector) * @method void getQueryJoinPath(DatabaseQuerySelect $query, $selector)
* @method bool|Field getQueryUnknownField($fieldName, array $data); * @method bool|Field getQueryUnknownField($fieldName, array $data);
*
* @property string $includeMode
* @property bool $checkAccess
* *
*/ */
@@ -392,6 +395,12 @@ class PageFinder extends Wire {
// protected $extraSubSelectors = array(); // subselectors that are added in after getQuery() // protected $extraSubSelectors = array(); // subselectors that are added in after getQuery()
// protected $extraJoins = array(); // protected $extraJoins = array();
// protected $nativeWheres = array(); // where statements for native fields, to be reused in subselects where appropriate. // protected $nativeWheres = array(); // where statements for native fields, to be reused in subselects where appropriate.
public function __get($key) {
if($key === 'includeMode') return $this->includeMode;
if($key === 'checkAccess') return $this->checkAccess;
return parent::__get($key);
}
/** /**
* Initialize new find operation and prepare options * Initialize new find operation and prepare options
@@ -1687,6 +1696,7 @@ class PageFinder extends Wire {
$q->set('selector', $selector); // original selector if required by the fieldtype $q->set('selector', $selector); // original selector if required by the fieldtype
$q->set('selectors', $selectors); // original selectors (all) if required by the fieldtype $q->set('selectors', $selectors); // original selectors (all) if required by the fieldtype
$q->set('parentQuery', $query); $q->set('parentQuery', $query);
$q->set('pageFinder', $this);
$q->bindOption('global', true); // ensures bound value key are globally unique $q->bindOption('global', true); // ensures bound value key are globally unique
$q->bindOption('prefix', 'pf'); // pf=PageFinder $q->bindOption('prefix', 'pf'); // pf=PageFinder
@@ -3469,6 +3479,7 @@ class PageFinder extends Wire {
* @property Selector $selector Original Selector object * @property Selector $selector Original Selector object
* @property Selectors $selectors Original Selectors object * @property Selectors $selectors Original Selectors object
* @property DatabaseQuerySelect $parentQuery Parent database query * @property DatabaseQuerySelect $parentQuery Parent database query
* @property PageFinder $pageFinder PageFinder instance that initiated the query
*/ */
abstract class PageFinderDatabaseQuerySelect extends DatabaseQuerySelect { } abstract class PageFinderDatabaseQuerySelect extends DatabaseQuerySelect { }

View File

@@ -1092,7 +1092,7 @@ class FieldtypePage extends FieldtypeMulti implements Module, ConfigurableModule
* *
* Note: $field->findPagesCode is not implemented and thus not supported here. * Note: $field->findPagesCode is not implemented and thus not supported here.
* *
* @param DatabaseQuerySelect $query * @param DatabaseQuerySelect|PageFinderDatabaseQuerySelect $query
* @param string $table * @param string $table
* @param string $subfield * @param string $subfield
* @param string $operator * @param string $operator
@@ -1103,18 +1103,35 @@ class FieldtypePage extends FieldtypeMulti implements Module, ConfigurableModule
*/ */
protected function getMatchQueryCustom($query, $table, $subfield, $operator, $value) { protected function getMatchQueryCustom($query, $table, $subfield, $operator, $value) {
$database = $this->wire('database'); $database = $this->wire()->database;
$field = $query->field; $field = $query->field;
$group = $query->group; $group = $query->group;
$table = $database->escapeTable($table); $table = $database->escapeTable($table);
$selector = 'include=hidden, '; // @todo should $selector include check_access=0 or even include=all?
$pageFinder = $this->wire(new PageFinder()); $pageFinder = $this->wire(new PageFinder());
$pageFinderOptions = array('getTotal' => false); $pageFinderOptions = array('getTotal' => false);
$value = $this->wire('sanitizer')->selectorValue($value); $value = $this->wire()->sanitizer->selectorValue($value);
$findPagesSelector = $field->get('findPagesSelector'); $findPagesSelector = $field->get('findPagesSelector');
if(empty($findPagesSelector)) $findPagesSelector = $field->get('findPagesSelect'); if(empty($findPagesSelector)) $findPagesSelector = $field->get('findPagesSelect');
$parent_id = $field->get('parent_id'); $parent_id = $field->get('parent_id');
$template_ids = self::getTemplateIDs($field, true); $template_ids = self::getTemplateIDs($field, true);
$selector = '';
if($query->pageFinder) {
$pf = $query->pageFinder;
$pfo = $pf->getOptions();
if(empty($pfo['findAll']) === true) {
$selector = "include=all, $selector";
} else if($pf->includeMode) {
$selector = "include=$pf->includeMode, $selector";
} else {
$selector = "include=hidden, $selector";
}
if($pf->checkAccess === false) {
$selector = "check_access=0, $selector";
}
} else {
$selector = "include=hidden, $selector";
}
if(in_array($subfield, $this->nativeNames)) { if(in_array($subfield, $this->nativeNames)) {
// fine then, we can handle that here when needed (like !=) // fine then, we can handle that here when needed (like !=)
@@ -1125,8 +1142,10 @@ class FieldtypePage extends FieldtypeMulti implements Module, ConfigurableModule
} }
if($findPagesSelector) { if($findPagesSelector) {
// remove the existing include=hidden, only if selector specifies a different include=something // remove the existing include=hidden|all|unpublished, only if selector specifies a different include=something
if(strpos($findPagesSelector, 'include=') !== false) $selector = ''; if(strpos($findPagesSelector, 'include=') !== false) {
$selector = preg_replace('/include=[a-z]+,/', '', $selector);
}
$selector .= $findPagesSelector . ", "; $selector .= $findPagesSelector . ", ";
if(strpos($selector, 'page.') !== false) { if(strpos($selector, 'page.') !== false) {
// remove any page.property from selector as it won't be applicable during a getMatchQuery // remove any page.property from selector as it won't be applicable during a getMatchQuery