API /lib/hierarchical.php

Language PHP Lines 524
MD5 Hash f5cfba141816b718cae406eadd275bf2
Repository https://github.com/dreamhackcrew/API.git View Raw File
  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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
<?php

class hierarchical {

    const version = '1.0.0';
	private $info = array();

    function __construct($table,$id=NULL,$child=NULL) {
		$this->table = $table;
		$this->id = $id;
		$this->child = $child;
	}

	function init( $do = NULL, $basic = false ) {

		if ( $do == NULL )
			$do = $this->table;

		if ( isset( $this->info[$do]['basic'] ) && $basic )
			return true;

		if ( isset( $this->info[$do]['ready'] ) )
			return true;
        
		if ( trim($do) == '' )
			return !trigger_error('No table is defined, failed hierarchical initiation',E_USER_ERROR);

        if ( !$tables = db()->fetchAll("DESCRIBE `$do`"))
            return !trigger_error('Table "'.$do.'" don´t exists!',E_USER_ERROR);

		$this->info[$do] = array();

        foreach($tables as $line) {
            if ($line['Key'] == 'PRI')
                $this->info[$do]['id'] = $line['Field'];
            if ($line['Field'] == 'lft')
                $lft_found = true;
            if ($line['Field'] == 'rgt')
                $rgt_found = true;
            if ($line['Field'] == 'prio')
                $this->info[$do]['prio'] = true;
        }

		if( !isset($this->info[$do]['id']) || trim($this->info[$do]['id']) == '' )
			return !trigger_error('Table "'.$do.'" don´t have a primary key!',E_USER_ERROR);

        if ( !isset($lft_found) || !isset($rgt_found) ) {
            db()->installTables(  array(
                $do => array(
                    array(
                        'Field'   => 'lft',
                        'Type'    => 'int(11)'
                    ),
                    array(
                        'Field'   => 'rgt',
                        'Type'    => 'int(11)'
                    ),
                    array(
                        'Field'   => 'parent',
                        'Type'    => 'int(11)'
                    )
                )
            ));
            db()->query("ALTER TABLE `%s` ADD INDEX ( `lft` , `rgt` )",$do);

			if ( !isset($this->id) || trim($this->id) == '' )
				$this->id = $this->info[$do]['id'];

			$this->$this->info[$do]['basic'] = true;

            if ( !$this->rebuild() )
				return false;
        }

		$this->info[$do]['basic'] = true;

		if ( $do == $this->table ) {
			if ( !isset($this->id) || trim($this->id) == '' )
				$this->id = $this->info[$do]['id'];

			if ( ( !isset($this->prio) || trim($this->prio) == '' ) && isset($this->info[$do]['prio']) )
				$this->prio = $this->info[$do]['prio'];

			// integrity check... only on the main table
				if (!$integrity = db()->fetchSingle("SELECT count(*) as rows, max(rgt) as max FROM `{$do}`") )
					return !trigger_error('Table "'.$do.'" failed integrity check',E_USER_ERROR);

				if ( ($integrity['rows'] * 2) != $integrity['max'] && !$basic ) {
					trigger_error('Table "'.$do.'" failed integrity check, trying to repair tree!',E_USER_ERROR);
					if ( !$this->rebuild() )
						return false;
				}
		} else {
			if ( !isset($this->childId) || trim($this->childId) == '' )
				$this->childId = $this->info[$do]['id'];
		}


		
		$this->info[$do]['ready'] = true;

		if ( isset($this->child) && $this->child )
			return $this->init($this->child);

		return true;

    }

    function setId( $id ) {
        if ( is_numeric($id) || strlen($id) < 2 )
            return !trigger_error('Id field must be a string and at least 2 chars long');

        $this->id = $id;
    }

    function setChild( $table ) {
		if ( trim($table) > '' )
	        $this->child = $table;
    }

    function setPrio( $prio ) {
        if ( is_numeric($prio) || strlen($prio) < 2 )
            return !trigger_error('Priority field must be a string and at least 2 chars long');

        $this->info[$this->table]['prio'] = $prio;
    }

	function tree($root,$where='',$what='*') {
		$data = $this->do_tree($root,$where,$what);

        return $data['childs'];
    }

    function get_tree($root,$where = '',$what='*') {

        if (!$this->init())
			return !trigger_error('Hierarchical isn´t initiated!',E_USER_WARNING);

        $where = trim($where);
        if ( $where != '' )
            $where = '('.$where.') AND ';


        $what = explode( ',', $what );
        if ( !in_array('*',$what) ) {
            $what[] = "`{$this->id}`";
            $what[] = "`lft`";
            $what[] = "`rgt`";
        }

        $what = array_unique($what);
        $what = implode($what, ', ');


        if (is_array($root)) {
            if (!$row = db()->fetchSingle("SELECT min(lft) as lft, max(rgt) as rgt FROM {$this->table} WHERE $where parent IN (".implode($root,',').')'))
                return false;
        }
        else {
			if ( $root ) {
	           if (!$row = db()->fetchSingle("SELECT min(lft) as lft, max(rgt) as rgt FROM {$this->table} WHERE $where parent=$root"))
		            return false;
			} else {
				if (!$row = db()->fetchSingle("SELECT min(lft) as lft, max(rgt) as rgt FROM {$this->table} WHERE $where 1=1"))
					return false;
			}
        }
        if (!($row['lft'] > 0 && $row['rgt'] > 0))
            return false;
    
        return db()->fetchAll("SELECT $what FROM {$this->table} WHERE $where lft BETWEEN %d AND %d ORDER BY lft ASC;",$row['lft'],$row['rgt']);
    }

    function do_tree($root,$where='',$what='*') {
        if(!$result = $this->get_tree($root,$where,$what))
            return false;

        $right = array();
        $ret = array();
        $level = array();

        foreach ($result as $row) {
            while (count($right)>0 && ($right[count($right)-1] < $row['rgt'])) {
                array_pop($right);
                array_pop($level);
            }

            $l = '';
            foreach($level as $line)
                $l .= "['childs'][$line]";

            eval('$ret'.$l.'[\'childs\']['.$row[$this->id].'] = $row;');

            $right[] = $row['rgt'];
            $level[] = $row[$this->id];
        }
        return $ret;
    }

    function selectOptions($root,$where='',$head = 'head') {
            if(!$result = $this->get_tree($root,$where))
                return array();

            $right = array();
            $options = array();

            foreach ($result as $row) {
                while (count($right)>0 && ($right[count($right)-1] < $row['rgt'])) {
                    array_pop($right);
                }

                $options[] = array(
                    'text'  => str_repeat(' -',count($right)) . ' ' . $row[$head],
                    'val' => $row[$this->id]
                );

                $right[] = $row['rgt'];
            }
            return $options;
    }

    function do_level($root,$where = '') {
        if(!$result = $this->get_tree($root,$where))
            return false;

        foreach ($result as $row) {
            if ($row['parent'] == $root)
                $ret[] = $row;
        }

        return $ret;
    }

    function do_list( $root,$fields = '' ) {
        if( !$result = $this->get_tree($root) ){
            //@trigger_error('No childs to "'.$root.'" was found!');
            return  array();
        }
        $ret = array();

        foreach ( $result as $row )
            if ( $fields != '' )
                if( is_array($fields) )
                    $ret[] = array_intersect_key($row,$fields);
                else
                    if ( isset($row[$fields]) )
                        $ret[] = $row[$fields];
                    else
                        trigger_error('Field "'.$fields.'" not found in source',E_USER_NOTICE);
            else
                $ret[] = $row;

        return $ret;
    }

    function do_path($id,$field = '',$separator = '') {

        if (!$this->init())
            return !trigger_error('Hierarchical isn´t initiated!',E_USER_WARNING);

        if ( !$data = db()->fetchSingle("SELECT lft,rgt FROM `{$this->table}` WHERE `{$this->id}`='$id'"))
            return !trigger_error('Main post not found in '.$this->table.'!',E_USER_WARNING);

        if( trim($field) != '' ) {
            if( $path = db()->fetchAllOne("SELECT $field FROM {$this->table} WHERE lft <= {$data['lft']} AND rgt >= {$data['rgt']} ORDER BY lft ASC"))
                if (trim($separator) != '')
                    return implode($path,$separator);
                else
                    return $path;
        } else {
            if( $path = db()->fetchAll("SELECT * FROM {$this->table} WHERE lft <= {$data['lft']} AND rgt >= {$data['rgt']} ORDER BY lft ASC"))
                return $path;
        }

        return false;
    }

    function descendants($lft,$rgt) {
        return ($rgt - $lft - 1) / 2;
    }

    function rebuild($parent = 0, $left = 0) {
        db()->query("UPDATE {$this->table} SET lft=-1, rgt=-1");

        if ( !$this->do_rebuild($parent, $left) )
            return !trigger_error('rebuild tree failed',E_USER_ERROR);

        return true;
    }

    function do_rebuild($parent, $left) {

        if ( !$this->init( NULL, true ) )
            return !trigger_error('Hierarchical isn´t initiated, can not save!',E_USER_WARNING);

        if (!isset($this->table))
            return !trigger_error('Table isn´t defined',E_USER_WARNING);

        if (!is_numeric($parent))
            return !trigger_error('$parent isn´t a numeric value',E_USER_WARNING);

        $right = $left+1;

        if ( isset($this->prio) ) {
            if($result = db()->fetchAllOne("SELECT {$this->id} FROM {$this->table} WHERE parent=$parent ORDER BY prio;"))
                foreach($result as $row)
                    $right = self::do_rebuild($row, $right);
        } else {
            if($result = db()->fetchAllOne("SELECT {$this->id} FROM {$this->table} WHERE parent=$parent;"))
                foreach($result as $row)
                    $right = self::do_rebuild($row, $right);
       }

        db()->query("UPDATE {$this->table} SET lft=$left, rgt=$right WHERE {$this->id}=$parent;");
        if ( isset($this->child) && $this->child )
            db()->query("UPDATE {$this->child} SET lft=$left, rgt=$right WHERE {$this->id}=$parent;");

        return $right+1;
    }

    function add( $data, $parent = -1 ) {

        if (!$this->init())
            return !trigger_error('Hierarchical isn´t initiated, can not save!',E_USER_WARNING);
       
        if ( $parent != -1 )
            $data['parent'] = $parent;

        if ( !isset($data['parent']) )
            return !trigger_error('Input parent id isn´t set, can not save!',E_USER_WARNING);

        if ( !is_numeric($data['parent']) )
            return !trigger_error('Input parent isn´t a numeric value, can not save!',E_USER_WARNING);

        if ( isset($this->prio) && isset($data['prio']) && is_numeric($data['prio']) ) {
            if ( ($rgt = db()->fetchOne("SELECT rgt FROM {$this->table} WHERE parent={$data['parent']} AND prio<={$data['prio']} ORDER BY prio DESC,lft DESC LIMIT 1")) === false)
                @trigger_error('Failed to select the insert point of the post, trying parent!');
        } else {
            if( ($rgt = db()->fetchOne("SELECT rgt FROM {$this->table} WHERE parent={$data['parent']} ORDER BY lft DESC LIMIT 1")) === false )
                @trigger_error('Failed to select the insert point of the post, trying parent!');
        }

        if ( !isset($rgt) || isset($rgt) && !is_numeric($rgt) )
            if ( ($rgt = db()->fetchOne("SELECT lft FROM {$this->table} WHERE {$this->id}={$data['parent']}")) === false )
                if ( db()->fetchAll("SELECT * FROM {$this->table}") === false )
                    $rgt = 0;
                else
                    if ( ($rgt = db()->fetchOne("SELECT lft FROM {$this->table} WHERE parent={$data['parent']} ORDER BY prio LIMIT 1")) === false )
                        return !trigger_error('Failed to select the insert point of the post!',E_USER_ERROR);

        $data['lft'] = $rgt + 1;
        $data['rgt'] = $rgt + 2;
		$time = microtime(true);
		
		// Skapa ett hål
        if ( db()->query("UPDATE {$this->table} SET rgt=rgt+2 WHERE lft < %d AND rgt >= %d ",$data['lft'],$data['lft']) &&
             db()->query("UPDATE {$this->table} SET rgt=rgt+2,lft=lft+2 WHERE lft >= %d",$data['rgt']) ) {

			// Uppdatera i spegel trädet
            if ( isset($this->child) && $this->child ) {
                db()->query("UPDATE {$this->child} SET rgt=rgt+2 WHERE lft < %d AND rgt >= %d ",$data['lft'],$data['lft']);
                db()->query("UPDATE {$this->child} SET rgt=rgt+2,lft=lft+2 WHERE lft >= %d",$data['rgt']);
            }

			// Lägg till datan
			if ( !db()->insert( $data,$this->table ) )
				return !trigger_error('Failed to save data in tree!',E_USER_ERROR);

			$insert = mysql_insert_id();
			
			// Kontrollera så att det inte blivit något fel
			/*
            if ( $failed = db()->fetchAll("SELECT * FROM {$this->table} AS a, {$this->table} AS b 
					WHERE (a.lft = b.lft OR a.lft = b.rgt OR a.rgt = b.rgt) AND a.{$this->id} <> b.{$this->id}") ) {
				
				trigger_error('Faild to do a cleean insert, rebuilding tree!',E_USER_ERROR);
                $this->rebuild();
            }
			*/

			// Returnera idt datan fick
            return $insert;
        }

        return !trigger_error('Failed to save data in tree!',E_USER_ERROR);
    }

    function remove ( $id, $force = false ) {

        if (!$this->init())
            return !trigger_error('Hierarchical isn´t initiated!',E_USER_WARNING);

        if ( !is_numeric($id) )
            return !trigger_error('Row identifiaction isn´t a numeric value!');

        if ( !$post = db()->fetchSingle("SELECT lft,rgt FROM {$this->table} WHERE {$this->id}=%d",$id) )
            return !trigger_error('The selected row could not be found in the database!',E_USER_ERROR);

        $posts = ($post['rgt']-$post['lft']+1 * 2)-1;

        if ( $posts > 2 )
            if ( $force )
                $childs = $this->do_list( $id, $this->id );
            else
                return !trigger_error('The selected post have childs! Remove aborted');

        if ( !db()->query("DELETE FROM {$this->table} WHERE lft BETWEEN {$post['lft']} AND {$post['rgt']}") )
            return !trigger_error("Failed to remove the selected post and its childs!");

        if ( db()->query("UPDATE {$this->table} SET rgt=rgt-$posts,lft=lft-$posts WHERE lft>{$post['rgt']}") &&
             db()->query("UPDATE {$this->table} SET rgt=rgt-$posts WHERE  rgt > {$post['rgt']} AND lft < {$post['lft']} ")
            ) {
            if ( isset($this->child) && $this->child ) {
                db()->query("UPDATE {$this->child} SET rgt=rgt-$posts,lft=lft-$posts WHERE lft>{$post['rgt']}");
                db()->query("UPDATE {$this->child} SET rgt=rgt-$posts WHERE  rgt > {$post['rgt']} AND lft < {$post['lft']} ");
            }
            return true;
        }
        $this->rebuild();
        return !trigger_error('Failed to remove data in tree, reversing and rebuilding tree!',E_USER_ERROR);
    }

    function move ( $id, $parent, $prio = 0 ) {

        if (!$this->init())
            return !trigger_error('Hierarchical isn´t initiated!',E_USER_WARNING);

        if ( !is_numeric($id) )
            return !trigger_error('Row identifiaction isn´t a numeric value!');

        if ( !is_numeric($parent) )
            return !trigger_error('Row parent identifiaction isn´t a numeric value!',E_USER_WARNING);


        $row = db()->fetchSingle("SELECT * FROM {$this->table} WHERE {$this->id}=%d",$id);
        $this->remove( $id );
        $row['parent'] = $parent;
        return $this->add($row);
        /*
        // ---------------------------------------------------------------------------------------------------------
        if ( !$post = db()->fetchSingle("SELECT lft,rgt FROM {$this->table} WHERE {$this->id}=%d",$id) )
            return !trigger_error('The selected post could not be found in the database!',E_USER_ERROR);

        if ( isset($this->prio) && $prio && is_numeric($prio) ) {
            if ( ($rgt = db()->fetchOne("SELECT rgt FROM {$this->table} WHERE parent=$parent AND prio<=$prio ORDER BY prio DESC,lft DESC LIMIT 1")) === false)
                @trigger_error('Failed to select the insert point of the post, trying parent!');
        } else {
            if( ($rgt = db()->fetchOne("SELECT rgt FROM {$this->table} WHERE parent=$parent ORDER BY lft DESC,{$this->id} LIMIT 1")) === false )
                @trigger_error('Failed to select the insert point of the post, trying parent!');
        }

        if ( !isset($rgt) || isset($rgt) && !is_numeric($rgt) )
            if ( ($rgt = db()->fetchOne("SELECT lft FROM {$this->table} WHERE {$this->id}=$parent")) === false )
                if ( db()->fetchAll("SELECT * FROM {$this->table}") === false )
                    $rgt = 0;
                else
                    if ( ($rgt = db()->fetchOne("SELECT lft FROM {$this->table} WHERE parent=$parent ORDER BY prio LIMIT 1")) === false )
                        return !trigger_error('Failed to select the insert point of the post!',E_USER_ERROR);

        $shift = $post['lft'] - ($rgt + 1);
        $posts = $post['rgt'] - $post['lft'] + 1;

        $move = db()->fetchOne("SELECT count(*) FROM {$this->table}") * 2;

        //echo $move;


        // flytta posten till slutet..
        db()->query("UPDATE {$this->table} SET lft=lft+$move, rgt=rgt+$move, parent=$parent WHERE {$this->id}=$id");

        if ( $shift > 0 ) { // flytta upp en post
            // shifta alla poster emellan
            db()->query("UPDATE {$this->table} SET rgt=rgt-$posts, lft=lft-$posts WHERE rgt BETWEEN {$post['rgt']} AND $rgt ");


        } else { // flytta ner
            // shifta alla poster emellan
            db()->query("UPDATE {$this->table} SET rgt=rgt-$posts, lft=lft-$posts WHERE rgt > {$post['rgt']} AND lft < $rgt AND lft > {$post['rgt']} ");
            $shift += $posts;
        }

        // minska alla parents med antalet poster som flyttat
        db()->query("UPDATE {$this->table} SET rgt=rgt-$posts WHERE lft < {$post['lft']} AND rgt > {$post['rgt']}");

        // öka alla parents med antalet poster som flyttat till
        db()->query("UPDATE {$this->table} SET rgt=rgt+$posts WHERE lft < $rgt AND rgt > $rgt");

        // flytta in på rätt pos
        db()->query("UPDATE {$this->table} SET lft=lft-$move-$shift, rgt=rgt-$move-$shift WHERE lft >$move");


        // innan


        //db()->query("UPDATE {$this->table} SET rgt=rgt+$shift+1 WHERE rgt > $rgt AND lft < $rgt");

        // efter


        */
    }

	function _check( $parent = 0 ) {

        if (!$this->init())
            return !trigger_error('Hierarchical isn´t initiated!',E_USER_WARNING);

		if ( $childs = db()->fetchAll("SELECT {$this->id},name,lft,rgt FROM {$this->table} WHERE parent=%d",$parent) )
		foreach ( $childs as $line ) {
			$count = db()->fetchOne("SELECT count(*) FROM {$this->table} WHERE lft BETWEEN %d AND %d",$line['lft'],$line['rgt']);

			if ( ($count*2) != ($line['rgt'] - $line['lft']+1) ) {
				if ( $this->_check( $line[$this->id] ) )
					return !trigger_error('Failed integrity check count in ID#'.$line[$this->id].' count is <b>'.($count*2).'</b> and shuld be <b>'.($line['rgt'] - $line['lft']+1).'</b>');
			}
		}

		return true;

	}
}

?>
Back to Top