You are here

class HierarchyOutlineStorage in Entity Reference Hierarchy 8

Defines a storage class for hierarchies outline.

Hierarchy

Expanded class hierarchy of HierarchyOutlineStorage

1 string reference to 'HierarchyOutlineStorage'
entity_hierarchy.services.yml in ./entity_hierarchy.services.yml
entity_hierarchy.services.yml
1 service uses HierarchyOutlineStorage
entity_hierarchy.outline_storage in ./entity_hierarchy.services.yml
Drupal\entity_hierarchy\HierarchyOutlineStorage

File

src/HierarchyOutlineStorage.php, line 15
Definition of Drupal\entity_hierarchy\HierarchyOutlineStorage.

Namespace

Drupal\entity_hierarchy
View source
class HierarchyOutlineStorage implements HierarchyOutlineStorageInterface {

  /**
   * Database Service Object.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * Constructs a HierarchyOutlineStorage object.
   */
  public function __construct(Connection $connection) {
    $this->connection = $connection;
  }
  public function getHierarchies() {
    return $this->connection
      ->query("SELECT DISTINCT(hid) FROM {entity_hierarchy}")
      ->fetchCol();
  }

  /**
   * Get all the parents for the given node.
   */
  public function hierarchyGetParents($node, $limit = NULL) {
    $cnid = $node;

    // If a node object was passed, then the parents may already have been loaded.
    if (is_object($node)) {
      if (isset($node->entity_hierarchy_parents)) {
        return $node->entity_hierarchy_parents;
      }
      $cnid = $node->nid;
    }
    $out = array();
    $query = db_select('entity_hierarchy', 'nh')
      ->fields('nh')
      ->where('cnid = :cnid', array(
      ':cnid' => $cnid,
    ))
      ->orderBy('pweight', 'ASC');
    if ($limit) {
      $query
        ->range(0, $limit);
    }
    $result = $query
      ->execute()
      ->fetchAll();
    foreach ($result as $item) {
      $out[] = $item;
    }
    return $out;
  }

  /**
   * Get the immediate parent for the given node.
   */
  public function hierarchyGetParent($cnid) {
    $query = db_select('entity_hierarchy', 'nh')
      ->fields('nh')
      ->where('cnid = :cnid', array(
      ':cnid' => $cnid,
    ))
      ->orderBy('pweight', 'ASC');
    $result = $query
      ->execute()
      ->fetch();
    return $result;
  }

  /**
   * {@inheritdoc}
   */
  public function delete($nid) {

    //    return $this->connection->delete('hierarchy')
    //      ->condition('nid', $nid)
    //      ->execute();
  }

  /**
   * Get the next child weight for a given pnid.
   */
  public function hierarchyLoadParentNextChildWeight($pnid) {
    $out = db_query('SELECT MAX(cweight) FROM {entity_hierarchy} WHERE pnid = :pnid', array(
      ':pnid' => $pnid,
    ))
      ->fetchField();
    if ($out !== NULL) {
      $out += 1;
    }
    else {
      $out = 0;
    }
    return $out;
  }

  /**
   * Save a entity_hierarchy record.
   */
  public function hierarchyRecordLoad($hid) {
    $result = db_select('entity_hierarchy', 'nh')
      ->fields('nh')
      ->where('hid = :hid', array(
      ':hid' => $hid,
    ))
      ->execute();
    return $result
      ->fetch();
  }

  /**
   * Save a entity_hierarchy record.
   */
  public function hierarchyRecordDelete($hid) {
    db_delete('entity_hierarchy')
      ->condition('hid', $hid)
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function insert($item) {
    return $this->connection
      ->insert('entity_hierarchy')
      ->fields(array(
      'pnid' => $item->pnid,
      'cnid' => $item->cnid,
      'cweight' => $item->cweight,
      'pweight' => $item->pweight,
    ))
      ->execute();
  }

  /**
   * Count the children of the given node.
   */
  function hierarchyGetNodeChildrenCount($node) {

    //    dsm($node);
    $pnid = $node;
    if (is_object($node)) {
      $pnid = $node
        ->id();
    }
    return (int) db_query("SELECT count(*) FROM {entity_hierarchy} WHERE pnid = :pnid", array(
      ':pnid' => $pnid,
    ))
      ->fetchField();
  }

  /**
   * Get the children of the given node.
   */
  public function hierarchyGetNodeChildren($node, $limit = FALSE) {
    $pnid = $node;
    if (is_object($node)) {
      $pnid = $node
        ->id();
    }
    $query = db_select('entity_hierarchy', 'nh')
      ->fields('nh')
      ->fields('nfd', array(
      'title',
    ))
      ->where('pnid = :pnid', array(
      ':pnid' => $pnid,
    ))
      ->orderBy('cweight', 'ASC');
    $query
      ->leftJoin('node', 'n', 'nh.cnid = n.nid');
    $query
      ->leftJoin('node_field_data', 'nfd', 'nfd.nid = n.nid');
    if ($limit) {
      $query
        ->range(0, $limit);
    }
    $result = $query
      ->execute()
      ->fetchAll();
    $children = array();
    foreach ($result as $item) {
      $children[] = $item;
    }
    return $children;
  }

  /**
  * Get a tree of nodes of the given type.
  */
  public function hierarchyNodesByType($types) {
    $out = array();
    if ($types) {
      $query = db_select('node', 'n')
        ->fields('n', array(
        'nid',
        'type',
      ))
        ->fields('nh', array(
        'cweight',
        'pnid',
      ))
        ->fields('nfd', array(
        'title',
        'uid',
        'status',
      ))
        ->condition('n.type', $types, 'IN')
        ->orderBy('nh.cweight', 'ASC');
      $query
        ->leftJoin('entity_hierarchy', 'nh', 'nh.cnid = n.nid');
      $query
        ->leftJoin('node_field_data', 'nfd', 'nfd.nid = n.nid');
      $result = $query
        ->execute();
      foreach ($result as $item) {
        $out[$item->nid] = $item;
      }
    }
    return $out;
  }

  /**
   * {@inheritdoc}
   */
  public function update($hid, $fields) {
    return $this->connection
      ->update('entity_hierarchy')
      ->fields($fields)
      ->condition('hid', $hid)
      ->execute();
  }
  public function loadHierarchies($nids) {
    $results = db_select('entity_hierarchy', 'h')
      ->fields('h', array(
      'pnid',
    ))
      ->condition('h.cnid', $nids, 'IN')
      ->execute()
      ->fetchAllAssoc('pnid', \PDO::FETCH_ASSOC);
    $pnids = array();
    foreach ($results as $result) {
      $pnids[] = $result['pnid'];
    }
    return $pnids;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
HierarchyOutlineStorage::$connection protected property Database Service Object.
HierarchyOutlineStorage::delete public function Deletes a hierarchy entry. Overrides HierarchyOutlineStorageInterface::delete
HierarchyOutlineStorage::getHierarchies public function
HierarchyOutlineStorage::hierarchyGetNodeChildren public function Get the children of the given node.
HierarchyOutlineStorage::hierarchyGetNodeChildrenCount function Count the children of the given node.
HierarchyOutlineStorage::hierarchyGetParent public function Get the immediate parent for the given node.
HierarchyOutlineStorage::hierarchyGetParents public function Get all the parents for the given node.
HierarchyOutlineStorage::hierarchyLoadParentNextChildWeight public function Get the next child weight for a given pnid. Overrides HierarchyOutlineStorageInterface::hierarchyLoadParentNextChildWeight
HierarchyOutlineStorage::hierarchyNodesByType public function Get a tree of nodes of the given type. Overrides HierarchyOutlineStorageInterface::hierarchyNodesByType
HierarchyOutlineStorage::hierarchyRecordDelete public function Save a entity_hierarchy record. Overrides HierarchyOutlineStorageInterface::hierarchyRecordDelete
HierarchyOutlineStorage::hierarchyRecordLoad public function Save a entity_hierarchy record. Overrides HierarchyOutlineStorageInterface::hierarchyRecordLoad
HierarchyOutlineStorage::insert public function Inserts a hierarchy link. Overrides HierarchyOutlineStorageInterface::insert
HierarchyOutlineStorage::loadHierarchies public function Overrides HierarchyOutlineStorageInterface::loadHierarchies
HierarchyOutlineStorage::update public function Updates hierarchy reference for links that were moved between hierarchies. Overrides HierarchyOutlineStorageInterface::update
HierarchyOutlineStorage::__construct public function Constructs a HierarchyOutlineStorage object.