diff --git a/ext/bulk_actions/main.php b/ext/bulk_actions/main.php index 66ce1d6b..bae4353f 100644 --- a/ext/bulk_actions/main.php +++ b/ext/bulk_actions/main.php @@ -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"); - } } diff --git a/ext/bulk_actions/theme.php b/ext/bulk_actions/theme.php index 80675509..a6c3ad3c 100644 --- a/ext/bulk_actions/theme.php +++ b/ext/bulk_actions/theme.php @@ -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 =" + $body = " "; } - usort($event->actions, array($this, "sort_blocks")); + usort($event->actions, array($this, "sort_blocks")); - foreach($event->actions as $action) { - $body .= "
".make_form(make_link("bulk_action"), "POST", False, "", "return validate_selections(this,'".html_escape($action["confirmation_message"])."');"). - "". - "". - "". - $action["block"]. - "". - "
"; + foreach ($event->actions as $action) { + $body .= "
" . make_form(make_link("bulk_action"), "POST", False, "", "return validate_selections(this,'" . html_escape($action["confirmation_message"]) . "');") . + "" . + "" . + "" . + $action["block"] . + "" . + "
"; } - if(!$hasQuery) { + if (!$hasQuery) { $body .= ""; } $block = new Block("Bulk Actions", $body, "left", 30); $page->add_block($block); } } - - public function render_tag_input() { - return "". - ""; + + public function render_tag_input() + { + return "" . + ""; } - public function render_source_input() { - return ""; - } - + public function render_source_input() + { + return ""; + } }