cleanup
This commit is contained in:
parent
66df295ec1
commit
8612a07a5a
@ -9,36 +9,40 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
class BulkActionBlockBuildingEvent extends Event {
|
class BulkActionBlockBuildingEvent extends Event
|
||||||
/** @var array */
|
{
|
||||||
public $actions = array();
|
/** @var array */
|
||||||
/**
|
public $actions = array();
|
||||||
* @param string $name
|
|
||||||
*/
|
public function add_action(String $action, String $confirmation_message = "", String $block = "", int $position = 40)
|
||||||
public function add_action(String $action, String $confirmation_message = "", String $block = "", int $position = 40) {
|
{
|
||||||
if($block==null)
|
if ($block == null)
|
||||||
$block = "";
|
$block = "";
|
||||||
|
|
||||||
array_push($this->actions, array(
|
array_push(
|
||||||
"block"=>$block,
|
$this->actions,
|
||||||
"confirmation_message"=>$confirmation_message,
|
array(
|
||||||
"action"=>$action,
|
"block" => $block,
|
||||||
"position"=>$position)
|
"confirmation_message" => $confirmation_message,
|
||||||
|
"action" => $action,
|
||||||
|
"position" => $position
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class BulkActionEvent extends Event {
|
class BulkActionEvent extends Event
|
||||||
|
{
|
||||||
public $action;
|
public $action;
|
||||||
public $items;
|
public $items;
|
||||||
public $page_request;
|
public $page_request;
|
||||||
|
|
||||||
function __construct (String $action, PageRequestEvent $pageRequestEvent, array $items) {
|
function __construct(String $action, PageRequestEvent $pageRequestEvent, array $items)
|
||||||
|
{
|
||||||
$this->action = $action;
|
$this->action = $action;
|
||||||
$this->page_request = $pageRequestEvent;
|
$this->page_request = $pageRequestEvent;
|
||||||
$this->items = $items;
|
$this->items = $items;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class BulkActions extends Extension
|
class BulkActions extends Extension
|
||||||
@ -46,33 +50,32 @@ class BulkActions extends Extension
|
|||||||
public function onPostListBuilding(PostListBuildingEvent $event)
|
public function onPostListBuilding(PostListBuildingEvent $event)
|
||||||
{
|
{
|
||||||
global $config, $page, $user;
|
global $config, $page, $user;
|
||||||
|
|
||||||
$this->theme->display_selector($page, $event, $config, Tag::implode($event->search_terms));
|
$this->theme->display_selector($page, $event, $config, Tag::implode($event->search_terms));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onBulkActionBlockBuilding(BulkActionBlockBuildingEvent $event)
|
public function onBulkActionBlockBuilding(BulkActionBlockBuildingEvent $event)
|
||||||
{
|
{
|
||||||
global $user;
|
global $user;
|
||||||
|
|
||||||
if ($user->can("delete_image")) {
|
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")) {
|
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")) {
|
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)
|
public function onBulkAction(BulkActionEvent $event)
|
||||||
{
|
{
|
||||||
global $user;
|
global $user;
|
||||||
|
|
||||||
switch($event->action) {
|
switch ($event->action) {
|
||||||
case "Delete":
|
case "Delete":
|
||||||
if ($user->can("delete_image")) {
|
if ($user->can("delete_image")) {
|
||||||
$this->delete_items($event->items);
|
$this->delete_items($event->items);
|
||||||
@ -85,10 +88,10 @@ class BulkActions extends Extension
|
|||||||
if ($user->can("bulk_edit_image_tag")) {
|
if ($user->can("bulk_edit_image_tag")) {
|
||||||
$tags = $_POST['bulk_tags'];
|
$tags = $_POST['bulk_tags'];
|
||||||
$replace = false;
|
$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;
|
$replace = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->tag_items($event->items, $tags, $replace);
|
$this->tag_items($event->items, $tags, $replace);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -102,7 +105,6 @@ class BulkActions extends Extension
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onPageRequest(PageRequestEvent $event)
|
public function onPageRequest(PageRequestEvent $event)
|
||||||
@ -116,35 +118,35 @@ class BulkActions extends Extension
|
|||||||
$action = $_POST['bulk_action'];
|
$action = $_POST['bulk_action'];
|
||||||
|
|
||||||
$items = [];
|
$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']);
|
$data = json_decode($_POST['bulk_selected_ids']);
|
||||||
if(is_array($data)) {
|
if (is_array($data)) {
|
||||||
foreach ($data as $id) {
|
foreach ($data as $id) {
|
||||||
if(is_numeric($id)) {
|
if (is_numeric($id)) {
|
||||||
$item = Image::by_id(int_escape($id));
|
$item = Image::by_id(int_escape($id));
|
||||||
array_push($items, $item);
|
array_push($items, $item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(sizeof($items)>0) {
|
if (sizeof($items) > 0) {
|
||||||
reset($items); // rewind to first element in array.
|
reset($items); // rewind to first element in array.
|
||||||
$newEvent = new BulkActionEvent($action, $event, $items);
|
$newEvent = new BulkActionEvent($action, $event, $items);
|
||||||
send_event($newEvent);
|
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'];
|
$query = $_POST['bulk_query'];
|
||||||
if($query!=null&&$query!="") {
|
if ($query != null && $query != "") {
|
||||||
$n = 0;
|
$n = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
$items = Image::find_images($n, 100, Tag::explode($query));
|
$items = Image::find_images($n, 100, Tag::explode($query));
|
||||||
if (count($items) == 0) {
|
if (count($items) == 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
reset($items); // rewind to first element in array.
|
reset($items); // rewind to first element in array.
|
||||||
$newEvent = new BulkActionEvent($action, $event, $items);
|
$newEvent = new BulkActionEvent($action, $event, $items);
|
||||||
send_event($newEvent);
|
send_event($newEvent);
|
||||||
|
|
||||||
$n += 100;
|
$n += 100;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -160,22 +162,23 @@ class BulkActions extends Extension
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function delete_items(array $items) {
|
private function delete_items(array $items)
|
||||||
|
{
|
||||||
$total = 0;
|
$total = 0;
|
||||||
foreach ($items as $item) {
|
foreach ($items as $item) {
|
||||||
try {
|
try {
|
||||||
send_event(new ImageDeletionEvent($item));
|
send_event(new ImageDeletionEvent($item));
|
||||||
$total++;
|
$total++;
|
||||||
} catch(Exception $e) {
|
} catch (Exception $e) {
|
||||||
flash_message("Error while removing $item->id: ".$e->getMessage(), "error");
|
flash_message("Error while removing $item->id: " . $e->getMessage(), "error");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
flash_message("Deleted $total items");
|
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);
|
$tags = Tag::explode($tags);
|
||||||
|
|
||||||
$pos_tag_array = [];
|
$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_merge($pos_tag_array, $item->get_tag_array());
|
||||||
$img_tags = array_diff($img_tags, $neg_tag_array);
|
$img_tags = array_diff($img_tags, $neg_tag_array);
|
||||||
} else {
|
} 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));
|
send_event(new TagSetEvent($item, $img_tags));
|
||||||
$total++;
|
$total++;
|
||||||
@ -211,18 +214,18 @@ class BulkActions extends Extension
|
|||||||
flash_message("Tagged $total items");
|
flash_message("Tagged $total items");
|
||||||
}
|
}
|
||||||
|
|
||||||
private function set_source(array $items, String $source) {
|
private function set_source(array $items, String $source)
|
||||||
|
{
|
||||||
$total = 0;
|
$total = 0;
|
||||||
foreach ($items as $item) {
|
foreach ($items as $item) {
|
||||||
try {
|
try {
|
||||||
send_event(new SourceSetEvent($item, $source));
|
send_event(new SourceSetEvent($item, $source));
|
||||||
$total++;
|
$total++;
|
||||||
} catch(Exception $e) {
|
} catch (Exception $e) {
|
||||||
flash_message("Error while setting source for $item->id: ".$e->getMessage(), "error");
|
flash_message("Error while setting source for $item->id: " . $e->getMessage(), "error");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
flash_message("Set source for $total items");
|
flash_message("Set source for $total items");
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,24 +2,24 @@
|
|||||||
|
|
||||||
class BulkActionsTheme extends Themelet
|
class BulkActionsTheme extends Themelet
|
||||||
{
|
{
|
||||||
private function sort_blocks($a, $b)
|
private function sort_blocks($a, $b)
|
||||||
{
|
{
|
||||||
return $a["position"] - $b["position"];
|
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;
|
global $user;
|
||||||
|
|
||||||
|
|
||||||
if($user->is_logged_in()) {
|
if ($user->is_logged_in()) {
|
||||||
$event = new BulkActionBlockBuildingEvent();
|
$event = new BulkActionBlockBuildingEvent();
|
||||||
send_event($event);
|
send_event($event);
|
||||||
|
|
||||||
if(sizeof($event->actions)==0)
|
if (sizeof($event->actions) == 0)
|
||||||
return;
|
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'/>
|
<input id='bulk_selector_activate' type='button' onclick='activate_bulk_selector();' value='Activate Selector'/>
|
||||||
<div id='bulk_selector_controls' style='display: none;'>
|
<div id='bulk_selector_controls' style='display: none;'>
|
||||||
<input id='bulk_selector_deactivate' type='button' onclick='deactivate_bulk_selector();' value='Deactivate Selector'/>
|
<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>
|
</td></tr></table>
|
||||||
";
|
";
|
||||||
|
|
||||||
$hasQuery = ($query!=null&&$query!="");
|
$hasQuery = ($query != null && $query != "");
|
||||||
|
|
||||||
if($hasQuery) {
|
if ($hasQuery) {
|
||||||
$body .= "</div>";
|
$body .= "</div>";
|
||||||
}
|
}
|
||||||
usort($event->actions, array($this, "sort_blocks"));
|
usort($event->actions, array($this, "sort_blocks"));
|
||||||
|
|
||||||
foreach($event->actions as $action) {
|
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"])."');").
|
$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_query' value='" . html_escape($query) . "'>" .
|
||||||
"<input type='hidden' name='bulk_selected_ids' />".
|
"<input type='hidden' name='bulk_selected_ids' />" .
|
||||||
"<input type='hidden' name='bulk_action' value='".$action["action"]."' />".
|
"<input type='hidden' name='bulk_action' value='" . $action["action"] . "' />" .
|
||||||
$action["block"].
|
$action["block"] .
|
||||||
"<input type='submit' value='".$action["action"]."'/>".
|
"<input type='submit' value='" . $action["action"] . "'/>" .
|
||||||
"</form></div>";
|
"</form></div>";
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!$hasQuery) {
|
if (!$hasQuery) {
|
||||||
$body .= "</div>";
|
$body .= "</div>";
|
||||||
}
|
}
|
||||||
$block = new Block("Bulk Actions", $body, "left", 30);
|
$block = new Block("Bulk Actions", $body, "left", 30);
|
||||||
$page->add_block($block);
|
$page->add_block($block);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function render_tag_input() {
|
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' />";
|
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() {
|
public function render_source_input()
|
||||||
return "<input type='text' name='bulk_source' required='required' placeholder='Enter source here' />";
|
{
|
||||||
}
|
return "<input type='text' name='bulk_source' required='required' placeholder='Enter source here' />";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user