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