1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-22 06:13:57 +02:00

Update FieldtypeComments, FieldtypeOptions and FieldtypePage to take advantage of new PageFinder and Database class improvements

This commit is contained in:
Ryan Cramer
2020-05-22 13:35:58 -04:00
parent 9d7d8f66af
commit 7223ff2356
4 changed files with 54 additions and 16 deletions

View File

@@ -1265,13 +1265,16 @@ class FieldtypeComments extends FieldtypeMulti {
} }
// COUNT // COUNT
$query = $database->prepare($countQuery->getQuery()); $sql = $countQuery->getQuery();
$query = $database->prepare($sql);
foreach($binds as $key => $value) { foreach($binds as $key => $value) {
$query->bindValue(":$key", $value); $query->bindValue(":$key", $value);
} }
$countQuery->copyBindValuesTo($query); // populate from $countQuery to $query
$query->execute(); $query->execute();
list($total) = $query->fetch(\PDO::FETCH_NUM); list($total) = $query->fetch(\PDO::FETCH_NUM);
$total = (int) $total; $total = (int) $total;
$query->closeCursor();
if($options['count']) return $total; if($options['count']) return $total;
// SELECT // SELECT
@@ -1290,6 +1293,7 @@ class FieldtypeComments extends FieldtypeMulti {
foreach($binds as $key => $value) { foreach($binds as $key => $value) {
$query->bindValue(":$key", $value); $query->bindValue(":$key", $value);
} }
$selectQuery->copyBindValuesTo($query); // populate from $selectQuery to $query
$query->execute(); $query->execute();
$commentPages = array(); $commentPages = array();

View File

@@ -345,9 +345,13 @@ class FieldtypeOptions extends FieldtypeMulti implements Module {
$database = $this->wire('database'); $database = $this->wire('database');
$t = $database->escapeTable($query->field->getTable()); $t = $database->escapeTable($query->field->getTable());
$s = $database->escapeCol($subfield); $s = $database->escapeCol($subfield);
$v = $database->escapeStr($value);
$query->where("(SELECT COUNT(*) FROM $t WHERE $t.pages_id=pages.id AND $t.$s='$v')=0"); $bindKey = $query->bindValueGetKey($value);
$query->parentQuery->where("($table.data IS NULL OR $table.$s!='$v')"); $query->where("(SELECT COUNT(*) FROM $t WHERE $t.pages_id=pages.id AND $t.$s=$bindKey)=0");
$bindKey = $query->parentQuery->bindValueGetKey($value);
$query->parentQuery->where("($table.data IS NULL OR $table.$s!=$bindKey)");
return $query; return $query;
} }

View File

@@ -27,6 +27,14 @@ class SelectableOptionManager extends Wire {
*/ */
protected $useLanguages = false; protected $useLanguages = false;
/**
* Cache of loaded options
*
* @var array
*
*/
protected $optionsCache = array();
/** /**
* Options removed that have not yet been deleted * Options removed that have not yet been deleted
* *
@@ -93,6 +101,12 @@ class SelectableOptionManager extends Wire {
*/ */
public function getOptions(Field $field, array $filters = array()) { public function getOptions(Field $field, array $filters = array()) {
$hasFilters = count($filters) > 0;
if(isset($this->optionsCache[$field->id]) && !$hasFilters) {
return $this->optionsCache[$field->id];
}
$defaults = array( $defaults = array(
'id' => array(), 'id' => array(),
'title' => array(), 'title' => array(),
@@ -176,6 +190,8 @@ class SelectableOptionManager extends Wire {
$options->resetTrackChanges(); $options->resetTrackChanges();
if(!$hasFilters) $this->optionsCache[$field->id] = $options;
return $options; return $options;
} }
@@ -544,6 +560,8 @@ class SelectableOptionManager extends Wire {
*/ */
public function updateOptions(Field $field, $options) { public function updateOptions(Field $field, $options) {
unset($this->optionsCache[$field->id]);
$database = $this->wire('database'); $database = $this->wire('database');
$sql = "UPDATE " . self::optionsTable . " SET sort=:sort, title=:title, `value`=:value "; $sql = "UPDATE " . self::optionsTable . " SET sort=:sort, title=:title, `value`=:value ";
$bindCols = array(); $bindCols = array();
@@ -596,6 +614,7 @@ class SelectableOptionManager extends Wire {
* *
*/ */
public function deleteOptions(Field $field, $options) { public function deleteOptions(Field $field, $options) {
unset($this->optionsCache[$field->id]);
$ids = array(); $ids = array();
foreach($options as $option) { foreach($options as $option) {
if(!$option instanceof SelectableOption) continue; if(!$option instanceof SelectableOption) continue;
@@ -616,6 +635,7 @@ class SelectableOptionManager extends Wire {
*/ */
public function deleteOptionsByID(Field $field, array $ids) { public function deleteOptionsByID(Field $field, array $ids) {
unset($this->optionsCache[$field->id]);
$database = $this->wire('database'); $database = $this->wire('database');
$table = $database->escapeTable($field->getTable()); $table = $database->escapeTable($field->getTable());
$cleanIDs = array(); $cleanIDs = array();
@@ -664,6 +684,8 @@ class SelectableOptionManager extends Wire {
// options that have pre-assigned IDs // options that have pre-assigned IDs
$optionsByID = array(); $optionsByID = array();
unset($this->optionsCache[$field->id]);
// determine if any added options already have IDs // determine if any added options already have IDs
foreach($options as $option) { foreach($options as $option) {
if(!$option instanceof SelectableOption || !strlen($option->title)) continue; if(!$option instanceof SelectableOption || !strlen($option->title)) continue;

View File

@@ -43,7 +43,7 @@ class FieldtypePage extends FieldtypeMulti implements Module, ConfigurableModule
'created', 'created',
'modified', 'modified',
'published', 'published',
); );
/** /**
@@ -758,7 +758,7 @@ class FieldtypePage extends FieldtypeMulti implements Module, ConfigurableModule
'path', 'path',
'url', 'url',
'sort', 'sort',
); );
$database = $this->wire('database'); $database = $this->wire('database');
@@ -813,20 +813,29 @@ class FieldtypePage extends FieldtypeMulti implements Module, ConfigurableModule
} }
} }
$value = $database->escapeStr($value);
$subfield = $database->escapeCol($subfield); $subfield = $database->escapeCol($subfield);
if($operator == '!=') { if($operator == '!=') {
$t = $database->escapeTable($query->field->getTable()); $t = $database->escapeTable($query->field->getTable());
$where = $where = "SELECT COUNT(*) FROM $t WHERE $t.pages_id=pages.id ";
"SELECT COUNT(*) FROM $t " . if($idstr) {
"WHERE $t.pages_id=pages.id " . $value = explode(',', $value);
"AND " . ($idstr ? "$t.$subfield IN($value)" : "$t.$subfield='$value'"); $value = array_map('intval', $value);
$value = implode(',', $value);
$where .= "AND $t.$subfield IN($value)";
} else {
$bindKey = $query->bindValueGetKey($value);
$where .= "AND $t.$subfield=$bindKey";
}
$query->where("($where)=0"); $query->where("($where)=0");
} else if($operator == '=' && $idstr) { } else if($operator == '=' && $idstr) {
$value = explode(',', $value);
$value = array_map('intval', $value);
$value = implode(',', $value);
$query->where("($table.{$subfield} IN($value))"); $query->where("($table.{$subfield} IN($value))");
} else { } else {
$query->where("($table.{$subfield}{$operator}'$value')"); $bindKey = $query->bindValueGetKey($value);
$query->where("($table.{$subfield}{$operator}$bindKey)");
} }
} else if($this->getMatchQueryNative($query, $table, $subfield, $operator, $value)) { } else if($this->getMatchQueryNative($query, $table, $subfield, $operator, $value)) {
@@ -894,9 +903,8 @@ class FieldtypePage extends FieldtypeMulti implements Module, ConfigurableModule
$table = $database->escapeTable($table); $table = $database->escapeTable($table);
$table2 = "_fieldtypepage" . (++$n); $table2 = "_fieldtypepage" . (++$n);
$subfield = $database->escapeCol($subfield); $subfield = $database->escapeCol($subfield);
$bindKey = $query->bindValueGetKey($value);
$value = $database->escapeStr($value); $query->join("pages AS $table2 ON $table2.$subfield$operator$bindKey");
$query->join("pages AS $table2 ON $table2.$subfield$operator'$value'");
$query->where("($table.data=$table2.id)"); $query->where("($table.data=$table2.id)");
return true; return true;