mirror of
https://github.com/processwire/processwire.git
synced 2025-08-14 10:45:54 +02:00
Update PagesEditor::savePageStatus() method to also accept array of page IDs (rather than just 1 page ID). Also accepts Page or PageArray objects now too.
This commit is contained in:
@@ -900,24 +900,53 @@ class PagesEditor extends Wire {
|
|||||||
* While this can be performed with other methods, this is here just to make it fast for internal/non-api use.
|
* While this can be performed with other methods, this is here just to make it fast for internal/non-api use.
|
||||||
* See the trash and restore methods for an example.
|
* See the trash and restore methods for an example.
|
||||||
*
|
*
|
||||||
* @param int $pageID
|
* @param int|array|Page|PageArray $pageID Page ID, Page, array of page IDs, or PageArray
|
||||||
* @param int $status Status per flags in Page::status* constants. Status will be OR'd with existing status, unless $remove option is set.
|
* @param int $status Status per flags in Page::status* constants. Status will be OR'd with existing status, unless $remove option is set.
|
||||||
* @param bool $recursive Should the status descend into the page's children, and grandchildren, etc?
|
* @param bool $recursive Should the status descend into the page's children, and grandchildren, etc?
|
||||||
* @param bool $remove Should the status be removed rather than added?
|
* @param bool $remove Should the status be removed rather than added?
|
||||||
|
* @return int Number of pages updated
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public function savePageStatus($pageID, $status, $recursive = false, $remove = false) {
|
public function savePageStatus($pageID, $status, $recursive = false, $remove = false) {
|
||||||
|
|
||||||
$pageID = (int) $pageID;
|
|
||||||
$status = (int) $status;
|
$status = (int) $status;
|
||||||
$sql = $remove ? "status & ~$status" : $sql = "status|$status";
|
$sql = $remove ? "status & ~$status" : "status|$status";
|
||||||
$database = $this->wire('database');
|
$database = $this->wire('database');
|
||||||
|
$rowCount = 0;
|
||||||
|
$multi = is_array($pageID) || $pageID instanceof PageArray;
|
||||||
|
|
||||||
|
if($multi && $recursive) {
|
||||||
|
// multiple page IDs combined with recursive option, must be handled individually
|
||||||
|
foreach($pageID as $id) {
|
||||||
|
$rowCount += $this->savePageStatus((int) "$id", $status, $recursive, $remove);
|
||||||
|
}
|
||||||
|
// exit early in this case
|
||||||
|
return $rowCount;
|
||||||
|
|
||||||
|
} else if($multi) {
|
||||||
|
// multiple page IDs without recursive option, can be handled in one query
|
||||||
|
$ids = array();
|
||||||
|
foreach($pageID as $id) {
|
||||||
|
$id = (int) "$id";
|
||||||
|
if($id > 0) $ids[$id] = $id;
|
||||||
|
}
|
||||||
|
if(!count($ids)) $ids[] = 0;
|
||||||
|
$query = $database->prepare("UPDATE pages SET status=$sql WHERE id IN(" . implode(',', $ids) . ")");
|
||||||
|
$database->execute($query);
|
||||||
|
return $query->rowCount();
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// single page ID or Page object
|
||||||
|
$pageID = (int) "$pageID";
|
||||||
$query = $database->prepare("UPDATE pages SET status=$sql WHERE id=:page_id");
|
$query = $database->prepare("UPDATE pages SET status=$sql WHERE id=:page_id");
|
||||||
$query->bindValue(":page_id", $pageID, \PDO::PARAM_INT);
|
$query->bindValue(":page_id", $pageID, \PDO::PARAM_INT);
|
||||||
$database->execute($query);
|
$database->execute($query);
|
||||||
|
$rowCount = $query->rowCount();
|
||||||
|
}
|
||||||
|
|
||||||
if($recursive) {
|
if(!$recursive) return $rowCount;
|
||||||
|
|
||||||
|
// recursive mode assumed from this point forward
|
||||||
$parentIDs = array($pageID);
|
$parentIDs = array($pageID);
|
||||||
|
|
||||||
do {
|
do {
|
||||||
@@ -927,6 +956,8 @@ class PagesEditor extends Wire {
|
|||||||
$query = $database->prepare("UPDATE pages SET status=$sql WHERE parent_id=:parent_id");
|
$query = $database->prepare("UPDATE pages SET status=$sql WHERE parent_id=:parent_id");
|
||||||
$query->bindValue(":parent_id", $parentID, \PDO::PARAM_INT);
|
$query->bindValue(":parent_id", $parentID, \PDO::PARAM_INT);
|
||||||
$database->execute($query);
|
$database->execute($query);
|
||||||
|
$rowCount += $query->rowCount();
|
||||||
|
$query->closeCursor();
|
||||||
|
|
||||||
// locate children that themselves have children
|
// locate children that themselves have children
|
||||||
$query = $database->prepare(
|
$query = $database->prepare(
|
||||||
@@ -936,15 +967,20 @@ class PagesEditor extends Wire {
|
|||||||
"GROUP BY pages.id " .
|
"GROUP BY pages.id " .
|
||||||
"ORDER BY pages.sort"
|
"ORDER BY pages.sort"
|
||||||
);
|
);
|
||||||
|
|
||||||
$query->bindValue(':parent_id', $parentID, \PDO::PARAM_INT);
|
$query->bindValue(':parent_id', $parentID, \PDO::PARAM_INT);
|
||||||
$database->execute($query);
|
$database->execute($query);
|
||||||
|
|
||||||
/** @noinspection PhpAssignmentInConditionInspection */
|
/** @noinspection PhpAssignmentInConditionInspection */
|
||||||
while($row = $query->fetch(\PDO::FETCH_ASSOC)) {
|
while($row = $query->fetch(\PDO::FETCH_ASSOC)) {
|
||||||
$parentIDs[] = (int) $row['id'];
|
$parentIDs[] = (int) $row['id'];
|
||||||
}
|
}
|
||||||
|
|
||||||
$query->closeCursor();
|
$query->closeCursor();
|
||||||
|
|
||||||
} while(count($parentIDs));
|
} while(count($parentIDs));
|
||||||
}
|
|
||||||
|
return $rowCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user