summaryrefslogtreecommitdiff
path: root/modules/organize/controllers/organize.php
blob: 08c80de3f13b8bc892c69c7908dd60ddc6ce9c0a (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php defined("SYSPATH") or die("No direct script access.");
/**
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2009 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 Organize_Controller extends Controller {
  function dialog($album_id) {
    $album = ORM::factory("item", $album_id);
    access::required("view", $album);
    access::required("edit", $album);

    $v = new View("organize_dialog.html");
    $v->album = $album;
    $v->album_tree = self::_expanded_tree(ORM::factory("item", 1), $album);
    $v->micro_thumb_grid = self::_get_micro_thumb_grid($album, 0);
    print $v;
  }

  function album($album_id, $offset) {
    $album = ORM::factory("item", $album_id);
    access::required("view", $album);
    access::required("edit", $album);

    print json_encode(
      array("grid" => self::_get_micro_thumb_grid($album, $offset)->__toString(),
            "sort_column" => $album->sort_column,
            "sort_order" => $album->sort_order));
  }

  function move_to($target_album_id) {
    access::verify_csrf();

    $target_album = ORM::factory("item", $target_album_id);
    access::required("view", $target_album);
    access::required("add", $target_album);

    foreach ($this->input->post("source_ids") as $source_id) {
      $source = ORM::factory("item", $source_id);
      if (!$source->contains($target_album)) {
        access::required("edit", $source);
        item::move($source, $target_album);
      }
    }

    print json_encode(
      array("tree" => self::_expanded_tree(ORM::factory("item", 1), $target_album)->__toString(),
            "grid" => self::_get_micro_thumb_grid($target_album, 0)->__toString()));
  }

  function rearrange($target_id, $before_or_after) {
    access::verify_csrf();

    $target = ORM::factory("item", $target_id);
    $album = $target->parent();
    access::required("view", $album);
    access::required("edit", $album);

    $source_ids = $this->input->post("source_ids", array());

    if ($album->sort_column != "weight") {
      $i = 0;
      foreach ($album->children() as $child) {
        // Do this directly in the database to avoid sending notifications
        Database::Instance()->update("items", array("weight" => ++$i), array("id" => $child->id));
      }
      $album->sort_column = "weight";
      $album->sort_order = "ASC";
      $album->save();
      $target->reload();
    }

    // Find the insertion point
    $target_weight = $target->weight;
    if ($before_or_after == "after") {
      $target_weight++;
    }

    // Make a hole
    $count = count($source_ids);
    Database::Instance()->query(
      "UPDATE {items} " .
      "SET `weight` = `weight` + $count " .
      "WHERE `weight` >= $target_weight AND `parent_id` = {$album->id}");

    // Insert source items into the hole
    foreach ($source_ids as $source_id) {
      Database::Instance()->update(
        "items", array("weight" => $target_weight++), array("id" => $source_id));
    }

    module::event("album_rearrange", $album);

    print json_encode(
      array("grid" => self::_get_micro_thumb_grid($album, 0)->__toString(),
            "sort_column" => $album->sort_column,
            "sort_order" => $album->sort_order));
  }

  function sort_order($album_id, $col, $dir) {
    access::verify_csrf();

    $album = ORM::factory("item", $album_id);
    access::required("view", $album);
    access::required("edit", $album);

    $options = album::get_sort_order_options();
    if (!isset($options[$col])) {
      return;
    }

    $album->sort_column = $col;
    $album->sort_order = $dir;
    $album->save();

    print json_encode(
      array("grid" => self::_get_micro_thumb_grid($album, 0)->__toString(),
            "sort_column" => $album->sort_column,
            "sort_order" => $album->sort_order));
  }

  private static function _get_micro_thumb_grid($album, $offset) {
    $v = new View("organize_thumb_grid.html");
    $v->album = $album;
    $v->offset = $offset;
    return $v;
  }

  public function tree($album_id) {
    $album = ORM::factory("item", $album_id);
    access::required("view", $album);

    print self::_expanded_tree($album);
  }

  /**
   * Create an HTML representation of the tree from the root down to the selected album.  We only
   * include albums along the descendant hierarchy that includes the selected album, and the
   * immediate child albums.
   */
  private static function _expanded_tree($root, $selected_album=null) {
    $v = new View("organize_tree.html");
    $v->album = $root;
    $v->selected = $selected_album;
    return $v;
  }
}