This commit is contained in:
Matthew Barbour 2019-06-05 19:37:07 -05:00
parent 66df295ec1
commit 8612a07a5a
2 changed files with 82 additions and 78 deletions

View File

@ -9,36 +9,40 @@
*/
class BulkActionBlockBuildingEvent extends Event {
/** @var array */
public $actions = array();
/**
* @param string $name
*/
public function add_action(String $action, String $confirmation_message = "", String $block = "", int $position = 40) {
if($block==null)
class BulkActionBlockBuildingEvent extends Event
{
/** @var array */
public $actions = array();
public function add_action(String $action, String $confirmation_message = "", String $block = "", int $position = 40)
{
if ($block == null)
$block = "";
array_push($this->actions, array(
"block"=>$block,
"confirmation_message"=>$confirmation_message,
"action"=>$action,
"position"=>$position)
array_push(
$this->actions,
array(
"block" => $block,
"confirmation_message" => $confirmation_message,
"action" => $action,
"position" => $position
)
);
}
}
}
class BulkActionEvent extends Event {
class BulkActionEvent extends Event
{
public $action;
public $items;
public $page_request;
function __construct (String $action, PageRequestEvent $pageRequestEvent, array $items) {
function __construct(String $action, PageRequestEvent $pageRequestEvent, array $items)
{
$this->action = $action;
$this->page_request = $pageRequestEvent;
$this->items = $items;
}
}
class BulkActions extends Extension
@ -46,33 +50,32 @@ class BulkActions extends Extension
public function onPostListBuilding(PostListBuildingEvent $event)
{
global $config, $page, $user;
$this->theme->display_selector($page, $event, $config, Tag::implode($event->search_terms));
}
public function onBulkActionBlockBuilding(BulkActionBlockBuildingEvent $event)
public function onBulkActionBlockBuilding(BulkActionBlockBuildingEvent $event)
{
global $user;
if ($user->can("delete_image")) {
$event->add_action("Delete","Delete selected images?","",10);
$event->add_action("Delete", "Delete selected images?", "", 10);
}
if ($user->can("bulk_edit_image_tag")) {
$event->add_action("Tag","",$this->theme->render_tag_input(),10);
$event->add_action("Tag", "", $this->theme->render_tag_input(), 10);
}
if ($user->can("bulk_edit_image_source")) {
$event->add_action("Set Source","",$this->theme->render_source_input(),10);
$event->add_action("Set Source", "", $this->theme->render_source_input(), 10);
}
}
public function onBulkAction(BulkActionEvent $event)
{
global $user;
switch($event->action) {
switch ($event->action) {
case "Delete":
if ($user->can("delete_image")) {
$this->delete_items($event->items);
@ -85,10 +88,10 @@ class BulkActions extends Extension
if ($user->can("bulk_edit_image_tag")) {
$tags = $_POST['bulk_tags'];
$replace = false;
if(isset($_POST['bulk_tags_replace']) && $_POST['bulk_tags_replace']=="true") {
if (isset($_POST['bulk_tags_replace']) && $_POST['bulk_tags_replace'] == "true") {
$replace = true;
}
$this->tag_items($event->items, $tags, $replace);
}
break;
@ -102,7 +105,6 @@ class BulkActions extends Extension
}
break;
}
}
public function onPageRequest(PageRequestEvent $event)
@ -116,35 +118,35 @@ class BulkActions extends Extension
$action = $_POST['bulk_action'];
$items = [];
if(isset($_POST['bulk_selected_ids'])&&$_POST['bulk_selected_ids']!="") {
if (isset($_POST['bulk_selected_ids']) && $_POST['bulk_selected_ids'] != "") {
$data = json_decode($_POST['bulk_selected_ids']);
if(is_array($data)) {
if (is_array($data)) {
foreach ($data as $id) {
if(is_numeric($id)) {
if (is_numeric($id)) {
$item = Image::by_id(int_escape($id));
array_push($items, $item);
}
}
}
if(sizeof($items)>0) {
if (sizeof($items) > 0) {
reset($items); // rewind to first element in array.
$newEvent = new BulkActionEvent($action, $event, $items);
send_event($newEvent);
}
} else if(isset($_POST['bulk_query'])&&$_POST['bulk_query']!="") {
} else if (isset($_POST['bulk_query']) && $_POST['bulk_query'] != "") {
$query = $_POST['bulk_query'];
if($query!=null&&$query!="") {
if ($query != null && $query != "") {
$n = 0;
while (true) {
$items = Image::find_images($n, 100, Tag::explode($query));
if (count($items) == 0) {
break;
}
reset($items); // rewind to first element in array.
$newEvent = new BulkActionEvent($action, $event, $items);
send_event($newEvent);
$n += 100;
}
}
@ -160,22 +162,23 @@ class BulkActions extends Extension
}
}
private function delete_items(array $items) {
private function delete_items(array $items)
{
$total = 0;
foreach ($items as $item) {
try {
send_event(new ImageDeletionEvent($item));
$total++;
} catch(Exception $e) {
flash_message("Error while removing $item->id: ".$e->getMessage(), "error");
} catch (Exception $e) {
flash_message("Error while removing $item->id: " . $e->getMessage(), "error");
}
}
}
flash_message("Deleted $total items");
}
private function tag_items(array $items, string $tags, bool $replace) {
private function tag_items(array $items, string $tags, bool $replace)
{
$tags = Tag::explode($tags);
$pos_tag_array = [];
@ -201,7 +204,7 @@ class BulkActions extends Extension
$img_tags = array_merge($pos_tag_array, $item->get_tag_array());
$img_tags = array_diff($img_tags, $neg_tag_array);
} else {
$img_tags =array_merge($tags, $item->get_tag_array());
$img_tags = array_merge($tags, $item->get_tag_array());
}
send_event(new TagSetEvent($item, $img_tags));
$total++;
@ -211,18 +214,18 @@ class BulkActions extends Extension
flash_message("Tagged $total items");
}
private function set_source(array $items, String $source) {
private function set_source(array $items, String $source)
{
$total = 0;
foreach ($items as $item) {
try {
send_event(new SourceSetEvent($item, $source));
$total++;
} catch(Exception $e) {
flash_message("Error while setting source for $item->id: ".$e->getMessage(), "error");
} catch (Exception $e) {
flash_message("Error while setting source for $item->id: " . $e->getMessage(), "error");
}
}
flash_message("Set source for $total items");
}
}

View File

@ -2,24 +2,24 @@
class BulkActionsTheme extends Themelet
{
private function sort_blocks($a, $b)
{
return $a["position"] - $b["position"];
}
private function sort_blocks($a, $b)
{
return $a["position"] - $b["position"];
}
public function display_selector(Page $page, Event $event, $config, $query)
{
public function display_selector(Page $page, Event $event, $config, $query)
{
global $user;
if($user->is_logged_in()) {
$event = new BulkActionBlockBuildingEvent();
send_event($event);
if ($user->is_logged_in()) {
$event = new BulkActionBlockBuildingEvent();
send_event($event);
if(sizeof($event->actions)==0)
return;
if (sizeof($event->actions) == 0)
return;
$body ="<input type='hidden' name='bulk_selected_ids' id='bulk_selected_ids' />
$body = "<input type='hidden' name='bulk_selected_ids' id='bulk_selected_ids' />
<input id='bulk_selector_activate' type='button' onclick='activate_bulk_selector();' value='Activate Selector'/>
<div id='bulk_selector_controls' style='display: none;'>
<input id='bulk_selector_deactivate' type='button' onclick='deactivate_bulk_selector();' value='Deactivate Selector'/>
@ -37,38 +37,39 @@ class BulkActionsTheme extends Themelet
</td></tr></table>
";
$hasQuery = ($query!=null&&$query!="");
$hasQuery = ($query != null && $query != "");
if($hasQuery) {
if ($hasQuery) {
$body .= "</div>";
}
usort($event->actions, array($this, "sort_blocks"));
usort($event->actions, array($this, "sort_blocks"));
foreach($event->actions as $action) {
$body .= "<div class='bulk_action'>".make_form(make_link("bulk_action"), "POST", False, "", "return validate_selections(this,'".html_escape($action["confirmation_message"])."');").
"<input type='hidden' name='bulk_query' value='".html_escape($query)."'>".
"<input type='hidden' name='bulk_selected_ids' />".
"<input type='hidden' name='bulk_action' value='".$action["action"]."' />".
$action["block"].
"<input type='submit' value='".$action["action"]."'/>".
"</form></div>";
foreach ($event->actions as $action) {
$body .= "<div class='bulk_action'>" . make_form(make_link("bulk_action"), "POST", False, "", "return validate_selections(this,'" . html_escape($action["confirmation_message"]) . "');") .
"<input type='hidden' name='bulk_query' value='" . html_escape($query) . "'>" .
"<input type='hidden' name='bulk_selected_ids' />" .
"<input type='hidden' name='bulk_action' value='" . $action["action"] . "' />" .
$action["block"] .
"<input type='submit' value='" . $action["action"] . "'/>" .
"</form></div>";
}
if(!$hasQuery) {
if (!$hasQuery) {
$body .= "</div>";
}
$block = new Block("Bulk Actions", $body, "left", 30);
$page->add_block($block);
}
}
public function render_tag_input() {
return "<label><input type='checkbox' style='width:13px;' name='bulk_tags_replace' value='true'/>Replace tags</label>".
"<input type='text' name='bulk_tags' required='required' placeholder='Enter tags here' />";
public function render_tag_input()
{
return "<label><input type='checkbox' style='width:13px;' name='bulk_tags_replace' value='true'/>Replace tags</label>" .
"<input type='text' name='bulk_tags' required='required' placeholder='Enter tags here' />";
}
public function render_source_input() {
return "<input type='text' name='bulk_source' required='required' placeholder='Enter source here' />";
}
public function render_source_input()
{
return "<input type='text' name='bulk_source' required='required' placeholder='Enter source here' />";
}
}