File: zophTreeTable.inc.php

package info (click to toggle)
zoph 1.0.1-3
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 16,480 kB
  • sloc: php: 27,195; javascript: 10,374; sql: 416; sh: 152; makefile: 4
file content (357 lines) | stat: -rw-r--r-- 10,960 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
/**
 * zophTreeTable represents a hierarchical table.
 *
 * 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 Jason Geiger
 * @author Jeroen Roos
 */

/**
 * zophTreeTable represents a hierarchical table.  Since the album
 * and category tables are identical in structure, some of the methods
 * those classes share are abstracted and placed here.
 *
 * @package Zoph
 * @author Jason Geiger
 * @author Jeroen Roos
 */
abstract class zophTreeTable extends zophTable {

    protected $children;
    protected $ancestors;

    abstract public function getChildren($order = null);

    /**
     * Insert a new record in the database
     */
    public function insert() {
        $this->set("createdby", (int) user::getCurrent()->getId());
        return parent::insert();
    }

    /**
     * Deletes a record along with all of its descendants.
     * @param array Names of tables from which entries also should be deleted.
     */
    public function delete() {
        // simulate overloading
        if (func_num_args()>=1) {
            $extra_tables = func_get_arg(0);
        } else {
            $extra_tables = null;
        }
        if ($this->getId()==0) {
            return;
        }
        $this->getChildren();
        if ($this->children) {
            foreach ($this->children as $child) {
                $child->delete();
            }
        }

        parent::delete($extra_tables);
    }

    /**
     * Updates a record
     * first check if there are no circular references created
     */
    public function update() {
        reset($this->fields);
        foreach ($this->fields as $name => $value) {
            if (substr($name, 0, 7) == "parent_") {
                $children=array();
                $this->getBranchIdArray($children);
                if (in_array($value, $children)) {
                    throw new circularReferenceException("You cannot set the parent to a child of the current selection!");
                }
            }
        }
        parent::update();
    }

    /**
     * Check whether this organizer is the root of the tree
     * At this moment the root always has id 1 but this may
     * change in the future, so to be safe we'll make a function for
     * this
     * @return bool
     */
    public function isRoot() {
        $root=static::getRoot();
        return ($this->getId() == $root->getId());
    }

    /**
     * Get the parent node for this node
     * @return zophTreeTable parent node
     */
    public function getParent() {
        if ($this->isRoot()) {
            return null;
        }
        $key = static::$primaryKeys[0];
        $pid = $this->get("parent_" . $key);

        if (!$pid) {
            $this->lookup();
            $pid = $this->get("parent_" . $key);
        }

        $parent = new static($pid);
        $parent->lookup();

        return $parent;
    }

    /**
     * Gets the ancestors of this record.
     * @param array ancestors
     * @return array ancestors
     */
    public function getAncestors($anc = array()) {
        $parent=$this->getParent();

        if ($parent) {
            array_push($anc, $parent);
            return $parent->getAncestors($anc);
        } else {
            return $anc;
        }
    }

    /**
     * Gets an array of links to ancestors
     * @return array ancestor name => url
     */
    public function getAncestorLinks() {
        $links=array();
        foreach (array_reverse($this->getAncestors()) as $ancestor) {
            $links[$ancestor->getName()] = $ancestor->getURL();
        }
        return $links;
    }


    /**
     * Get all ancestors of this a list of records, in order to get
     * all viewable records
     *
     * We now have a list of records this person can see, (that is, albums,
     * categories or places that contain photos this user can see). However,
     * sometimes it may be neededi to have access to a category, album or
     * place with no viewable photos, in order to reach a viewable
     * album, category or place. Therefore, we are going to backtrack up to
     * the root for each.
     */
    public static function getAllAncestors(array $records) {
        $ids=array();
        foreach ($records as $record) {
            $ids[$record->getId()]=$record->getId();

            $parents=$record->getAncestors();

            foreach ($parents as $parent) {
                $ids[$parent->getId()]=$parent->getId();
            }
        }
        return $ids;
    }
    /*
     * Gets a list of the id of this record along with the ids of
     * all of its descendants.
     * @param array id_array add values to this array
     * @todo refactor the pass by reference out
     */
    public function getBranchIdArray(array &$id_array=null) {
        if (!is_array($id_array)) {
            $id_array=array();
        }
        $id_array[] = (int) $this->getId();
        $this->getChildren();

        if ($this->children) {
            foreach ($this->children as $c) {
                $c->getBranchIdArray($id_array);
            }
        }
        return $id_array;
    }

    /*
     * Gets a comma separated string of this record's id along with
     * all of its descendant's ids.  Useful to make "record_id in
     * (id_list)" clauses.
     */
    public function getBranchIds() {
        return implode(",", $this->getBranchIdArray());
    }

    /**
     * Create an XML tree from this object
     * @param DOMDocument XML document to insert the new node in
     * @param Only include nodes that begin with this string
     */
    private function getXMLtree(DOMDocument $xml, $search) {
        $rootname=static::XMLROOT;
        $nodename=static::XMLNODE;
        $idname=static::$primaryKeys[0];

        $newchild=$xml->createElement($nodename);

        $title=$this->getName();
        $titleshort=$search ? strtolower(substr($title, 0, strlen($search))) : null;
        if ($titleshort == ($search ? strtolower($search) : null)) {
            $key=$this->get($idname);

            $newchildkey=$xml->createElement("key");
            $newchildkey->appendChild($xml->createTextNode($key));
            $newchildtitle=$xml->createElement("title");
            $newchildtitle->appendChild($xml->createTextNode($title));

            $newchild->appendChild($newchildkey);
            $newchild->appendChild($newchildtitle);
        }
        $order = user::getCurrent()->prefs->get("child_sortorder");
        $children=$this->getChildren($order);
        if ($children) {
            $childset=$xml->createElement($rootname);
            foreach ($children as $child) {
                $newnode=$child->getXMLtree($xml, $search);
                if (isset($newnode)) {
                    $childset->appendChild($newnode);
                }
            }
            $newchild->appendChild($childset);

        }
        return $newchild;
    }

    /**
     * Turn the array from @see getDetails() into XML
     * @param array Don't fetch details, but use the given array
     */
    public function getDetailsXML(array $details = array()) {
        if (empty($details)) {
            $details=$this->getDetails();
        }
        $children=$this->getChildren();
        if (is_array($children)) {
            $details["children"]=count($children);
        }
        return parent::getDetailsXML($details);
    }

    /**
     * Return the root of the tree
     * @return album|category|place
     */
    public static function getRoot() {
        return new static(1);
    }

    /**
     * Search for an object by hierarchical name
     * @example If you have an album "Vacation" with subalbums "2010"
     *          and "2012", both with a subalbum named "France"
     *          album::getByNameHierarchical("Vacation/2010/France");
     *          will match the "France" album in 2010, but not in 2012, even
     *          if they are both called "France"
     * @param string name to search for
     * @return zophTreeTable found object
     */
    public static function getByNameHierarchical($name) {
        if (strpos($name, "/") === false) {
            return static::getByName($name, true);
        }

        $found=0;

        $searchString=explode("/", $name);
        $depth=sizeof($searchString);
        foreach ($searchString as $namePart) {
            $objs = static::getByName($namePart, true);
            foreach ($objs as $obj) {
                $obj->lookup();
                if (!isset($parentObj)) {
                    $found++;
                    $parentObj=$obj;
                } else {
                    $nextObjId=$obj->getId();
                    $children=$parentObj->getChildren();
                    foreach ($children as $child) {
                        $child->lookup();
                        if ($child->getId()==$nextObjId) {
                            $parentObj=$obj;
                            $found++;
                            break;
                        }
                    }
                }
            }
        }

        // Only report success if we have traversed the full depth of the search.
        if ($depth == $found) {
            return $obj;
        } else {
            return false;
        }

    }


    public static function getXMLdata($search, DOMDocument $xml, DOMElement $rootnode) {
        $obj = static::getRoot();
        $obj->lookup();
        $tree=$obj->getXMLtree($xml, $search);
        $rootnode->appendChild($tree);
        $xml->appendChild($rootnode);
        return $xml;
    }

    public static function getTreeSelectArray($rec = null, $select_array = null, $depth=0) {
        $user=user::getCurrent();
        $user->lookupPrefs();
        $order = $user->prefs->get("child_sortorder");

        if (!$rec) {
            $rec = static::getRoot();
            $rec->lookup();
            $select_array[""] = "";
        }

        $select_array[$rec->getId()] = str_repeat("&nbsp;", $depth * 3) . e($rec->getName());

        $children = $rec->getChildren($order);
        if ($children) {
            $depth++;
            foreach ($children as $child) {
                $select_array = static::getTreeSelectArray($child, $select_array, $depth);
            }
        }
        return $select_array;
    }
}


?>