Version 1
[yaffs-website] / web / modules / contrib / draggableviews / src / DraggableViews.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\draggableviews\DraggableViews class.
6  */
7
8 namespace Drupal\draggableviews;
9
10 use Drupal\views\ViewExecutable;
11 use Drupal\Component\Utility\Html;
12
13 /**
14  * Class DraggableViews.
15  */
16 class DraggableViews {
17
18   /**
19    * The view.
20    *
21    * @var \Drupal\views\ViewExecutable $view
22    */
23   public $view;
24
25   /**
26    * Constructs DraggableViewsRows object.
27    *
28    * @param \Drupal\views\ViewExecutable $view
29    *   Views object.
30    */
31   public function __construct(ViewExecutable $view) {
32     $this->view = $view;
33   }
34
35   /**
36    * Get index by name and id.
37    */
38   public function getIndex($name, $id) {
39     foreach ($this->view->result as $item) {
40       if ($item->$name == $id) {
41         return $item->index;
42       }
43     }
44     return FALSE;
45   }
46
47   /**
48    * Get depth by index.
49    */
50   public function getDepth($index) {
51     if (!isset($this->view->result[$index])) {
52       return FALSE;
53     }
54     $row = $this->view->result[$index];
55     // If parent is available, set parent's depth +1.
56     return (!empty($row->draggableviews_structure_parent)) ? $this->getDepth($this->getIndex('nid', $row->draggableviews_structure_parent)) + 1 : 0;
57   }
58
59   /**
60    * Get parent by index.
61    */
62   public function getParent($index) {
63     return isset($this->view->result[$index]->draggableviews_structure_parent) ? $this->view->result[$index]->draggableviews_structure_parent : 0;
64   }
65
66   /**
67    * Get ancestor by index.
68    */
69   public function getAncestor($index) {
70     $row = $this->view->result[$index];
71     return !empty($row->draggableviews_structure_parent) ? $this->getAncestor($this->getIndex('nid', $row->draggableviews_structure_parent)) : $index;
72   }
73
74   /**
75    * Return value by it's name and index.
76    */
77   public function getValue($name, $index) {
78     return $this->view->result[$index]->$name;
79   }
80
81   /**
82    * Get HTML id for draggableviews table.
83    */
84   public function getHtmlId() {
85     return Html::getId('draggableviews-table-' . $this->view->id() . '-' . $this->view->current_display);
86   }
87
88 }