summaryrefslogtreecommitdiff
path: root/modules/batchtag/controllers/batchtag.php
blob: c5437b656e8b22a9791d6d5bed556550003e5049 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php defined("SYSPATH") or die("No direct script access.");
/**
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2013 Bharat Mediratta
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 */
class BatchTag_Controller extends Controller {
  public function tagitems() {
    // Tag all non-album items in the current album with the specified tags.

    // Prevent Cross Site Request Forgery
    access::verify_csrf();

    $input = Input::instance();
    url::redirect(url::abs_site("batchtag/tagitems2?name={$input->post('name')}&item_id={$input->post('item_id')}&tag_subitems={$input->post('tag_subitems')}&csrf={$input->post('csrf')}"));

  }
  
  public function tagitems2() {
    // Tag all non-album items in the current album with the specified tags.

    // Prevent Cross Site Request Forgery
    access::verify_csrf();

    $input = Input::instance();

    // Variables
    if (($input->get("batchtag_max") == false) || ($input->get("batchtag_max") == "0")) {
      $batchtag_max = "50";
    } else {
      $batchtag_max = $input->get("batchtag_max");      
    }
    if ($input->get("batchtag_items_processed") == false) {
      $batchtag_items_processed = "0";
    } else {
      $batchtag_items_processed = $input->get("batchtag_items_processed");
    }
    
    // Figure out if the contents of sub-albums should also be tagged
    $str_tag_subitems = $input->get("tag_subitems");

    $children = "";
    if ($str_tag_subitems == false) {
      // Generate an array of all non-album items in the current album.
      $children = ORM::factory("item")
        ->where("parent_id", "=", $input->get("item_id"))
        ->where("type", "!=", "album")
        ->find_all();
    } else {
      // Generate an array of all non-album items in the current album
      //   and any sub albums.
      $item = ORM::factory("item", $input->get("item_id"));
      $children = $item->descendants();
    }
    
    // Loop through each item in the album and make sure the user has
    //   access to view and edit it.
    $children_count = "0";
    $tag_count = "0";

    //echo Kohana::debug($children);

    echo '<style>.continue { margin: 5em auto; text-align: center; }</style>';

    foreach ($children as $child) {
      
      if ($tag_count < $batchtag_max) {
      
        if ($children_count >= $batchtag_items_processed) {
          if (access::can("view", $child) && access::can("edit", $child) && !$child->is_album()) {

            // Assuming the user can view/edit the current item, loop
            //   through each tag that was submitted and apply it to
            //   the current item.
            foreach (explode(",", $input->get("name")) as $tag_name) {
              $tag_name = trim($tag_name);
              if ($tag_name) {
                tag::add($child, $tag_name);
              }
              // $tag_count should be inside the foreach loop as it is depending on the number of time tag:add is run
              $tag_count++;
            }
          }
          echo '<style>.c' . $children_count . ' { display:none; }</style>' . "\n";
          $children_count++;
          $batchtag_max_new = $tag_count;
          echo '<div class="continue c' . $children_count . '"><a href="' . url::abs_site("batchtag/tagitems2?name={$input->get('name')}&item_id={$input->get('item_id')}&tag_subitems={$input->get('tag_subitems')}&batchtag_items_processed=$children_count&batchtag_max=$batchtag_max_new&csrf={$input->get('csrf')}") . '">Continue</a></div>';
        } else { $children_count++; }
        
      } else { break; }

    }

    if ($tag_count < $batchtag_max) {
      // Redirect back to the album.
      $item = ORM::factory("item", $input->get("item_id"));
      url::redirect(url::abs_site("{$item->type}s/{$item->id}"));
      //echo url::abs_site("{$item->type}s/{$item->id}");
    } else {
      url::redirect(url::abs_site("batchtag/tagitems2?name={$input->get('name')}&item_id={$input->get('item_id')}&tag_subitems={$input->get('tag_subitems')}&batchtag_items_processed=$children_count&batchtag_max=$batchtag_max&csrf={$input->get('csrf')}"));
      //echo url::abs_site("batchtag/tagitems2?name={$input->get('name')}&item_id={$input->get('item_id')}&tag_subitems={$input->get('tag_subitems')}&batchtag_items_processed=$children_count&batchtag_max=$batchtag_max&csrf={$input->get('csrf')}");
    }
  }
}