You are here

class CmisElement in CMIS API 3.0.x

Same name and namespace in other branches
  1. 8.2 src/CmisElement.php \Drupal\cmis\CmisElement
  2. 8 src/CmisElement.php \Drupal\cmis\CmisElement

Description of CmisElement.

Hierarchy

Expanded class hierarchy of CmisElement

1 file declares its use of CmisElement
CmisQueryForm.php in src/Form/CmisQueryForm.php

File

src/CmisElement.php, line 17

Namespace

Drupal\cmis
View source
class CmisElement {
  use StringTranslationTrait;

  /**
   * Cmis element.
   *
   * @var object
   */
  private $element;

  /**
   * Config.
   *
   * @var string
   */
  private $config;

  /**
   * Popup flag.
   *
   * @var string
   */
  private $popup;

  /**
   * Element parent id.
   *
   * @var string
   */
  private $parent;

  /**
   * The query string from cmis query form.
   *
   * @var string
   */
  private $queryString;

  /**
   * Render array of element.
   *
   * @var array
   */
  private $data = [];

  /**
   * The type of caller.
   *
   * @var string
   */
  private $type;

  /**
   * Root folder id.
   *
   * @var object
   */
  private $rootId;

  /**
   * {@inheritdoc}
   *
   * @param string $config
   *   Entity label.
   * @param bool $popup
   *   Get true if is a popup.
   * @param \Dkd\PhpCmis\DataObjects\AbstractFileableCmisObject $parent
   *   Get AbstractFileableCmisObject class.
   * @param string $query
   *   CMIS Query.
   * @param string $root_id
   *   CMIS root id.
   */
  public function __construct($config, $popup = FALSE, AbstractFileableCmisObject $parent = NULL, $query = '', $root_id = '') {
    $this->config = $config;
    $this->popup = $popup;
    $this->parent = $parent;
    $this->queryString = $query;
    $this->rootId = $root_id;
  }

  /**
   * Set element.
   *
   * @param string $type
   *   Controller type.
   * @param \Dkd\PhpCmis\DataObjects\AbstractFileableCmisObject $element
   *   Get AbstractFileableCmisObject class.
   */
  public function setElement($type, AbstractFileableCmisObject $element) {
    $this->type = $type;
    $this->element = $element;
    $this
      ->prepare();
  }

  /**
   * Prepare element data.
   */
  protected function prepare() {
    $type_id = $this->element
      ->getBaseTypeId()
      ->__toString();
    $name = $this->element
      ->getName();
    $id = $this->element
      ->getId();
    switch ($type_id) {
      case 'cmis:folder':
        $this
          ->prepareElement('cmis_browser_folder_item', $name, $id);
        break;
      case 'cmis:document':
        $this
          ->prepareElement('cmis_browser_document_item', $name, $id);
        break;
      default:
        $element = [
          '#theme' => 'cmis_browser_other_item',
          '#element' => $name,
        ];
        $this->data = [
          render($element),
        ];
    }
  }

  /**
   * Get element data.
   *
   * @return array
   *   Return data object.
   */
  public function getData() {
    return $this->data;
  }

  /**
   * Set element to render array.
   *
   * @param string $theme
   *   Render theme.
   * @param string $data
   *   Data.
   * @param string $id
   *   CMIS Object id.
   */
  protected function prepareElement($theme, $data, $id = '') {
    $author = $this->element
      ->getCreatedBy();
    $created = $this->element
      ->getCreationDate()
      ->format('Y-m-d H:i:s');
    $description = $this->element
      ->getDescription();
    $title = '';
    if ($title_property = $this->element
      ->getProperty('cm:title')) {
      $title = $title_property
        ->getFirstValue();
    }
    $size = 0;
    if ($size_property = $this->element
      ->getProperty('cmis:contentStreamLength')) {
      $size = $size_property
        ->getFirstValue();
    }
    $mime_type = '';
    $operations = '';
    if ($theme == 'cmis_browser_folder_item') {
      $this
        ->prepareFolderElement($data, $operations, $id);
    }
    if ($theme == 'cmis_browser_document_item') {
      $this
        ->prepareDocumentElement($data, $operations, $id);
    }
    if (!$this->popup) {
      $this
        ->preparePropertiesLink($operations);
    }
    $element = [
      '#theme' => $theme,
      '#element' => $data,
    ];
    $details = [
      '#theme' => 'cmis_browser_document_details',
      '#title' => $title,
      '#mime_type' => $mime_type,
      '#size' => number_format($size, 0, '', ' '),
    ];
    $this->data = [
      render($element),
      render($details),
      $author,
      $created,
      $description,
      $operations,
    ];
  }

  /**
   * Prepare document element.
   *
   * @param string $data
   *   Get data.
   * @param string $operations
   *   Get render.
   * @param string $id
   *   CMIS object id.
   */
  private function prepareDocumentElement(&$data, &$operations, $id) {
    if ($this->popup) {
      $url = Url::fromUserInput('/');
      $link_options = [
        'attributes' => [
          'class' => [
            'cmis-field-insert',
          ],
          'id' => $this->element
            ->getProperty('cmis:objectId')
            ->getFirstValue(),
          'name' => $data,
        ],
      ];
      $url
        ->setOptions($link_options);
      $path = Link::fromTextAndUrl($this
        ->t('Choose'), $url)
        ->toRenderable();
      $operations = render($path);
    }
    $url = Url::fromUserInput('/cmis/document/' . $this->config . '/' . $id);
    $path = Link::fromTextAndUrl($data, $url)
      ->toRenderable();
    $data = [
      '#markup' => render($path),
    ];
  }

  /**
   * Prepare folder element.
   *
   * @param string $data
   *   Get data.
   * @param string $operations
   *   Get render.
   * @param string $id
   *   CMIS object id.
   */
  private function prepareFolderElement(&$data, &$operations, $id) {
    switch ($this->type) {
      case 'browser':
        $url = Url::fromUserInput('/cmis/browser/nojs/' . $this->config . '/' . $id);
        $link_options = [
          'attributes' => [
            'class' => [
              'use-ajax',
            ],
          ],
        ];
        break;
      case 'query':
        $url = Url::fromUserInput('/cmis/browser/' . $this->config . '/' . $id);
        break;
    }
    if ($this->popup) {
      $url_operation = Url::fromUserInput('/');
      $link_options_operations = [
        'attributes' => [
          'class' => [
            'cmis-field-insert',
          ],
          'id' => $this->element
            ->getProperty('cmis:objectId')
            ->getFirstValue(),
          'name' => $data,
        ],
      ];
      $url_operation
        ->setOptions($link_options_operations);
      $path = Link::fromTextAndUrl($this
        ->t('Choose folder'), $url_operation)
        ->toRenderable();
      $operations = render($path);
      $link_options['query'] = [
        'type' => 'popup',
      ];
    }
    if (!empty($link_options)) {
      $url
        ->setOptions($link_options);
    }
    $data = Link::fromTextAndUrl($data, $url)
      ->toRenderable();
  }

  /**
   * Prepare properties link.
   *
   * @param string $operations
   *   Get render.
   */
  private function preparePropertiesLink(&$operations) {
    $url = Url::fromUserInput('/cmis/object-properties/' . $this->config . '/' . $this->element
      ->getId());
    $link_options = [
      'attributes' => [
        'class' => [
          'use-ajax',
        ],
        'data-dialog-type' => 'modal',
        'data-dialog-options' => Json::encode([
          'height' => 400,
          'width' => 700,
        ]),
      ],
    ];
    $url
      ->setOptions($link_options);
    $path = Link::fromTextAndUrl($this
      ->t('Properties'), $url)
      ->toRenderable();
    $links[] = [
      '#markup' => render($path),
      '#wrapper_attributes' => [
        'class' => [
          'object-properties',
        ],
      ],
    ];
    $cmis_connection_api = new CmisConnectionApi($this->config);
    $cmis_connection_api
      ->setDefaultParameters();
    $element_id = $this->element
      ->getId();
    if ($this->rootId != $element_id && $cmis_connection_api
      ->hasAllowableActionById($element_id, Action::CAN_DELETE_OBJECT)) {
      $url = Url::fromUserInput('/cmis/object-delete-verify/' . $this->config . '/' . $element_id);
      $link_options = [
        'attributes' => [
          'class' => [
            'use-ajax',
          ],
          'data-dialog-type' => 'modal',
          'data-dialog-options' => Json::encode([
            'height' => 120,
            'width' => 600,
          ]),
        ],
        'query' => [
          'type' => $this->type,
        ],
      ];
      switch ($this->type) {
        case 'browser':
          $link_options['query']['parent'] = $this->parent
            ->getId();
          break;
        case 'query':
          $link_options['query']['query_string'] = $this->queryString;
          break;
      }
      $url
        ->setOptions($link_options);
      $path = Link::fromTextAndUrl($this
        ->t('Delete'), $url)
        ->toRenderable();
      $links[] = [
        '#markup' => render($path),
        '#wrapper_attributes' => [
          'class' => [
            'object-delete',
          ],
        ],
      ];
    }
    $list = [
      '#theme' => 'item_list',
      '#items' => $links,
      '#type' => 'ul',
    ];
    $operations = render($list);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CmisElement::$config private property Config.
CmisElement::$data private property Render array of element.
CmisElement::$element private property Cmis element.
CmisElement::$parent private property Element parent id.
CmisElement::$popup private property Popup flag.
CmisElement::$queryString private property The query string from cmis query form.
CmisElement::$rootId private property Root folder id.
CmisElement::$type private property The type of caller.
CmisElement::getData public function Get element data.
CmisElement::prepare protected function Prepare element data.
CmisElement::prepareDocumentElement private function Prepare document element.
CmisElement::prepareElement protected function Set element to render array.
CmisElement::prepareFolderElement private function Prepare folder element.
CmisElement::preparePropertiesLink private function Prepare properties link.
CmisElement::setElement public function Set element.
CmisElement::__construct public function
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.