You are here

pattern.class.php in Patterns 6.2

File

pattern.class.php
View source
<?php

class Pattern {
  private $info = array(
    'title' => "",
    'description' => "",
    'author' => "",
    'author_email' => "",
    'category' => "",
    'version' => "0.1",
  );
  private $modules = array();
  private $actions = array();
  function __construct($title, $author, $author_email) {
    $this
      ->info('title', $title);
    $this
      ->info('author', $author);
    $this
      ->info('author_email', $author_email);
  }
  public function info($key = null, $value = null) {
    if ($key == null) {
      return $this->info;
    }
    $this->info[$key] = $value;
  }
  public function modules($modules_list) {
    $modules = self::list_to_array($modules_list);
    $this->modules = array_merge($this->modules, $modules);
  }

  // Actions
  private function action($tag, $data) {
    $this->actions[] = array_merge($data, array(
      'tag' => $tag,
    ));
  }
  public function variable($name, $value) {
    $this
      ->action('variable', array(
      'name' => $name,
      'value' => $value,
    ));
  }
  public function view($name, $description, $tag, $base = "node") {
    $view = new PatternView($name, $description, $tag, $base);
    $this
      ->action('view', array(
      'patterns_object' => $view,
    ));
    return $view;
  }

  // Utility
  public function export() {
    foreach ($this->actions as &$action) {
      if (array_key_exists('patterns_object', $action)) {
        $data = $action['patterns_object']
          ->export();

        // TODO: export shouldn't be destructive
        unset($action['patterns_object']);
        $action = array_merge($action, $data);
      }
    }
    return array(
      'info' => $this->info,
      'modules' => $this->modules,
      'actions' => $this->actions,
    );
  }
  private static function list_to_array($list) {
    return explode(",", preg_replace("/[,\\s]+/", ",", $list));
  }

}
class PatternView {
  private $action = 'update';
  private $details = array();
  private $displays = array();
  function __construct($name, $description, $tag, $base = "node") {
    $this->details = array(
      'name' => $name,
      'description' => $description,
      'tag' => $tag,
      'base' => $base,
    );
  }
  public function display($name, $title, $display_id) {
    $display = new PatternViewDisplay($name, $title, $display_id);
    $this->displays[] = $display;
    return $display;
  }
  public function export() {
    $displays = array();
    foreach ($this->displays as $display) {
      $displays[] = $display
        ->export();
    }
    return array(
      'action' => $this->action,
      'details' => $this->details,
      'display' => $displays,
    );
  }

}
class PatternViewDisplay {
  private $name;
  private $display_id;
  private $sections = array();
  private $items = array();
  function __construct($name, $title, $display_id) {
    $this->name = $name;
    $this->display_title = $title;
    $this->display_id = $display_id;
    if ($title) {
      $this
        ->section('title', $title);
    }
  }
  public function section($name, $value, $data = array()) {
    $this->sections[] = array(
      'section' => $name,
      'value' => $value,
      // 'data' => $data,
      'override' => true,
    );
  }
  public function item($type, $name, $data) {
    $this->items[] = array(
      'type' => $type,
      'name' => $name,
      'data' => $data,
      'override' => true,
    );
  }
  public function filter($name, $operator, $value) {
    $this
      ->item('filter', $name, array(
      'operator' => $operator,
      'value' => $value,
    ));
  }
  public function sort($name, $order, $extra = array()) {
    $this
      ->item('sort', $name, array_merge(array(
      'order' => strtoupper($order),
    ), $extra));
  }
  public function export() {
    $array = array(
      'name' => $this->name,
      'display_title' => $this->display_title,
      'display_id' => $this->display_id,
      'section' => $this->sections,
    );

    // Avoiding an error from an empty item array
    if ($this->items) {
      $array = array_merge($array, array(
        'item' => $this->items,
      ));
    }
    return $array;
  }

}