File: view.inc.php

package info (click to toggle)
zoph 1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,632 kB
  • sloc: php: 28,044; javascript: 10,435; sql: 527; sh: 153; makefile: 4
file content (264 lines) | stat: -rw-r--r-- 8,289 bytes parent folder | download
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
/**
 * Common parts for photo view
 *
 * This file is part of Zoph.
 *
 * Zoph 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.
 *
 * Zoph 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 Zoph; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @package Zoph
 * @author Jeroen Roos
 */

namespace photo\view;

use web\view\viewInterface;
use album;
use conf\conf;
use photo;
use selection;
use template\actionlink;
use template\block;
use user;
use web\request;
use web\url;
use web\view\view as webView;

use photoNoSelectionException;

/**
 * This is a base view for the photo page, that contains common parts for all views
 */
abstract class view extends webView implements viewInterface {

    /** * @var array request variables */
    protected $vars;

    /** @var int total number of photos return by the query */
    protected $photocount = 0;
    /** @var int offset in the query for the current photo */
    protected $offset = 0;

    /** @var string Contains url of next image */
    protected $nextURL = "";
    /** @var string Contains url of prev image */
    protected $prevURL = "";
    /** @var string Contains url for 'up' link  */
    protected $upURL = "";

    protected $title = "photo";

    /**
     * Create view
     * @param request web request
     * @param photo the photo that this page is dealing with
     */
    public function __construct(protected request $request, protected photo $photo) {
        $this->vars=$request->getRequestVars();
    }

    /**
     * Create "next" "previous" and "up" links for the photo page
     */
    public function setLinks() : void {
        if (isset($this->request["_off"])) {
            $user=user::getCurrent();
            $ignore=array("_off");

            $cols = (int) $this->request["_cols"];
            $rows = (int) $this->request["_rows"];
            $offset  = (int) $this->request["_off"];

            $cols = $cols ? $cols : $user->prefs->get("num_cols");
            $rows = $rows ? $rows : $user->prefs->get("num_rows");

            $cells = $cols * $rows;

            $upQs = $this->request->getUpdatedVars(null, null, $ignore);

            if ($cells) {
                $off = $cells * floor($offset / ($cells));
                $upQs["_off"] = $off;
            }
            $this->upURL=$this->request->getBasePath() . "photos?" . http_build_query($upQs);

            $url = $this->request->getBasePath() . "photo";

            if ($this->request->getAction() == "edit") {
                $url .= "/edit?";
            } else {
                $url .= "?";
            }

            if ($offset > 0) {
                $newoffset = $offset - 1;
                $this->prevURL= $url .
                    htmlentities(
                        str_replace("_off=$offset", "_off=$newoffset", $this->request->getReturnQueryString())
                    );
            }
            if ($offset + 1 < $this->photocount) {
                $newoffset = $offset + 1;
                $this->nextURL = $url .
                    htmlentities(
                        str_replace("_off=$offset", "_off=$newoffset", $this->request->getReturnQueryString())
                    );
            }

        }
    }


    /**
     * Get selection block for current photo
     * @return selection
     */
    protected function getSelection() : ?selection {
        try {
            $photoId = array(
                "photo_id_2" => $this->photo->getId(),
            );
            $selection=new selection(
                array(new actionlink("relate", "relation/new", $photoId)),
                "photo?_qs=" . $this->request->getEncodedQueryString(),
                $this->photo,
                field: "photo_id_1"
            );
        } catch (photoNoSelectionException $e) {
            $selection=null;
        }
        return $selection;
    }

    /**
     * Get 'share' block for current photo
     * @return block template block
     */
    protected function getShare() : ?block {
        if (conf::get("share.enable") && (user::getCurrent()->isAdmin() || user::getCurrent()->get("allow_share"))) {
            $hash=$this->photo->getHash();
            $fullHash=sha1(conf::get("share.salt.full") . $hash);
            $midHash=sha1(conf::get("share.salt.mid") . $hash);
            $fullLink=url::get() . "image?hash=" . $fullHash;
            $midLink=url::get() . "image?hash=" . $midHash;

            $share=new block("photoShare", array(
                "hash" => $hash,
                "full_link" => $fullLink,
                "mid_link" => $midLink
            ));
        }
        return $share ?? null;
    }

    public function getTitle() : string {
        return $this->title;
    }

    public function setOffset(int $offset, int $count) : void {
        $this->offset = $offset;
        $this->photocount = $count;

        if ($this->photo instanceof photo && !empty($this->photo->get("name"))) {
            $this->title = $this->photo->get("name");
        }

        if ($this->photocount > 0) {
            if (!empty($this->title)) {
                $this->title .= " - ";
            }
            $this->title .= sprintf(translate("photo %s of %s"), ($offset + 1), $this->photocount);
        }

        if (empty($this->title)) {
            $this->title = translate("Photo");
        }
    }


    /**
     * Create the actionlinks for this page
     */
    protected function getActionlinks() : array {
        $user=user::getCurrent();
        $actionlinks=array();

        $photoId = array("photo_id" => $this->photo->getId());

        if (conf::get("feature.mail")) {
            $actionlinks[] = new actionlink("email", "mail/compose", $photoId);
        }

        $writable = $user->getPhotoPermissions($this->photo)->get("writable");
        if ($writable) {
            $url = "photo/edit";
            $qs = $this->request->getReturnQueryString();
            if (!empty($qs)) {
                $url .= "?" . $qs;
            }
            $actionlinks["edit"] = new actionlink("edit", $url);
        }

        if ($user->canDeletePhotos() && $writable) {
            $actionlinks[] = new actionlink("delete", "photo/delete", array_merge($photoId, array(
                "_qs" => $this->request->getEncodedQueryString())));
        }

        if ($user->get("lightbox_id")) {
            $lightbox = new album($user->get("lightbox_id"));
            $lbPhotos = array_map(
                function ($ph) {
                    return $ph->getId();
                }, $lightbox->getPhotos()
            );

            if (array_search($this->photo->getId(), $lbPhotos) === false) {
                $action = "lightbox";
            } else {
                $action = "unlightbox";
            }

            $actionlinks[] = new actionlink($action, "photo/" . $action, array(
                "photo_id"  => $this->photo->getId(),
                "_return"   => "photo",
                "_qs"       => $this->request->getEncodedQueryString()
            ));
        }

        if (conf::get("feature.comments") && $user->canLeaveComments()) {
            $actionlinks[] = new actionlink("add comment", "comment/new", $photoId);
        }

        if ($user->isAdmin()) {
            $url = "photo/select";
            $qs = $this->request->getReturnQueryString();
            if (!empty($qs)) {
                $url .= "?" . $qs;
            }
            $actionlinks[] = new actionlink("select", $url);
        }
        return $actionlinks;
    }

    public function getScripts() : array {
        $scripts = parent::getScripts();
        $scripts[]="js/photoPeople.js";
        $scripts[]="js/locationLookup.js";
        $scripts[]="js/rating.js";
        $scripts[]="js/json.js";

        return $scripts;
    }

}