File: model.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 (206 lines) | stat: -rw-r--r-- 6,610 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
<?php
/**
 * This class stores relations between 2 photos.
 * This could be used store an original and a changed copy
 *
 * 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
 *
 * @author Jeroen Roos
 * @package Zoph
 */
namespace photo\relation;

use db\select;
use db\param;
use db\clause;
use PDO;
use photo;
use zophTable;


/**
 * This class stores relations between 2 photos.
 * This could be used store an original and a changed copy
 *
 * @author Jeroen Roos
 * @package Zoph
 */
class model extends zophTable {

    /** @var string The name of the database table */
    protected static $tableName="photo_relations";
    /** @var array List of primary keys */
    protected static $primaryKeys=array("photo_id_1", "photo_id_2");
    /** @var array Fields that may not be empty */
    protected static $notNull=array();
    /** @var bool keep keys with insert. In most cases the keys are set by the
                  db with auto_increment */
    protected static $keepKeys = true;
    /** @var string URL for this class */
    protected static $url="photo?photo_id=";

    /**
     * Create a new relation between two photos.
     * order of the photos is not important
     * @param photo first photo
     * @param photo second photo
     * @return phote\relation\model newly created object
     */
    public function __construct(photo $photo1, photo $photo2) {
        $this->set("photo_id_1", $photo1->getId());
        $this->set("photo_id_2", $photo2->getId());
    }

    /**
     * Get id
     * @return array ids
     */
    public function getId() {
        return array(
            "photo_id_1" => (int) $this->get("photo_id_1"),
            "photo_id_2" => (int) $this->get("photo_id_2")
        );
    }

    /**
     * Lookup in database.
     * Tries to look up in the database, first (photo_1, photo_2), then (photo_2, photo_1)
     * @return bool success or not
     */
    public function lookup() {
        if (!parent::lookup()) {
            $photoId1=$this->get("photo_id_1");
            $photoId2=$this->get("photo_id_2");

            $this->set("photo_id_1", $photoId2);
            $this->set("photo_id_2", $photoId1);

            return parent::lookup();
        } else {
            return true;
        }
    }

    /**
     * Get description.
     * Get description of the photo in the first param for the current relation
     * @param photo Photo to get description for
     * @throws photo\relation\exception if you try to lookup a photo that is not part of this relation
     * @return string description
     */
    public function getDesc(photo $photo) {
        if ($photo->getId() == $this->get("photo_id_1")) {
            return $this->get("desc_1");
        } else if ($photo->getId() == $this->get("photo_id_2")) {
            return $this->get("desc_2");
        } else {
            throw new exception("photo not in relation");
        }
    }

    /**
     * Set description.
     * Set description of the photo in the first param for the current relation
     * @param photo Photo to set description for
     * @param string description
     * @throws photo\relation\exception if you try to lookup a photo that is not part of this relation
     */
    public function setDesc(photo $photo, $desc) {
        if ($photo->getId() == $this->get("photo_id_1")) {
            $this->set("desc_1", $desc);
        } else if ($photo->getId() == $this->get("photo_id_2")) {
            $this->set("desc_2", $desc);
        } else {
            throw new exception("photo not in relation");
        }
    }

    /**
     * Define a relation between two photos, with descriptions.
     * Automatically creates new or updates existing relation
     * @param photo first photo
     * @param photo second photo
     * @param string description for first photo
     * @param string description for second photo
     */
    public static function defineRelation(photo $photo1, photo $photo2, $desc1, $desc2) {
        $rel=new self($photo1, $photo2);

        $exists=$rel->lookup();
        $rel->setDesc($photo1, $desc1);
        $rel->setDesc($photo2, $desc2);

        if ($exists===true) {
            $rel->update();
        } else {
            $rel->insert();
        }
    }

    /**
     * Get related photos
     * @param photo photo to get relations for
     * @return array of photos
     */
    public static function getRelated(photo $photo) {
        $qry=new select(array("pr" => "photo_relations"));
        $qry->addFunction(array("photo_id" => "photo_id_1"));
        $where=new clause("photo_id_2=:photoid2");
        $qry->addParam(new param(":photoid2", (int) $photo->getId(), PDO::PARAM_INT));
        $qry->where($where);

        $qry2=new select(array("pr" => "photo_relations"));
        $qry2->addFunction(array("photo_id" => "photo_id_2"));
        $where2=new clause("photo_id_1=:photoid1");
        $qry2->addParam(new param(":photoid1", (int) $photo->getId(), PDO::PARAM_INT));
        $qry2->where($where2);

        $qry->union($qry2);

        return photo::getRecordsFromQuery($qry);
    }

    /**
     * Get relation for 2 specific photos.
     * Order of photos is not important
     * @param photo first photo
     * @param photo second photo
     * @returns photo\relation\model|bool relation, if found or false
     */
    public static function getRelationForPhotos(photo $photo1, photo $photo2) {
        $rel=new self($photo1, $photo2);

        if (!$rel->lookup()) { return false; }

        return $rel;
    }

    /**
     * Get relation for 2 specific photos.
     * Returns description for SECOND photo.
     * @param photo first photo
     * @param photo second photo
     * @returns string description
     */
    public static function getDescForPhotos(photo $photo1, photo $photo2) {
        $rel=self::getRelationForPhotos($photo1, $photo2);
        if ($rel instanceof self) {
            return $rel->getDesc($photo2);
        }
    }
}

?>