You are here

class ContentTypeGroup in Content type groups 7.2

Same name in this branch
  1. 7.2 content_type_groups.controller.inc \ContentTypeGroup
  2. 7.2 content_type_groups.class.inc \ContentTypeGroup
Same name and namespace in other branches
  1. 7 content_type_groups.module \ContentTypeGroup

Utility class to handle content type groups

Hierarchy

Expanded class hierarchy of ContentTypeGroup

1 string reference to 'ContentTypeGroup'
content_type_groups_entity_info in ./content_type_groups.module
Implements hook_entity_info().

File

./content_type_groups.class.inc, line 5

View source
class ContentTypeGroup {
  protected $type;

  // Machine name
  protected $name;

  // Display name
  public $content_types = array();

  // Content types belonging to this group.
  // array(
  //   $content_type => array(
  //     'name'   => $name,      // Display name of the content type
  //     '#weight' => $weight,   // Weight of the content type in this group
  //   )
  // )
  private static $table_groups = 'content_type_groups_groups';
  private static $table_types = 'content_type_groups_types';
  public function __construct($type = NULL) {
    $this->type = check_plain($type);

    // If a type is passed in, we're assuming that there's a
    // matching content type group, so go find it
    if ($type) {
      $result = db_select(self::$table_groups, 'g')
        ->fields('g', array(
        'name',
      ))
        ->condition('type', $type, '=')
        ->execute()
        ->fetchAssoc();
      if ($result) {
        $this->name = $result['name'];

        // Get the content types in this group
        $result = db_select(self::$table_types, 't')
          ->fields('t', array(
          'content_type',
          'weight',
        ))
          ->condition('group_type', $type, '=')
          ->orderBy('weight', 'ASC')
          ->execute();
        if ($result) {
          $all_node_types = node_type_get_names();
          foreach ($result as $row) {
            $this->content_types[$row->content_type] = array(
              'name' => $all_node_types[$row->content_type],
              '#weight' => $row->weight,
            );
          }
        }
      }
    }
  }

  /**
   * Creates or updates the current content type group.
   *
   * @return ContentTypeGroup
   *   Current ContentTypeGroup object (for chaining)
   */
  public function save() {
    $transaction = db_transaction();
    try {

      // Add/update the group info
      $result = db_merge(self::$table_groups)
        ->key(array(
        'type' => $this->type,
      ))
        ->fields(array(
        'name' => $this->name,
      ))
        ->execute();
      if ($result) {

        // Delete all types for this group
        db_delete(self::$table_types)
          ->condition('group_type', $this->type)
          ->execute();

        // Insert the new types for this group
        foreach ($this->content_types as $content_type => $type_data) {
          db_insert(self::$table_types)
            ->fields(array(
            'group_type' => $this->type,
            'content_type' => $content_type,
            'weight' => $type_data['weight'],
          ))
            ->execute();
        }
      }
    } catch (Exception $e) {
      $transaction
        ->rollback();
      watchdog_exception('content type groups', $e);
      throw $e;
    }
    return $this;
  }

  /**
   * Deletes the current content type group.
   *
   * @return ContentTypeGroup
   *   Empty ContentTypeGroup object (for chaining)
   */
  public function delete() {
    $transaction = db_transaction();
    try {

      // Delete the content types for this group
      db_delete(self::$table_types)
        ->condition('group_type', $this->type)
        ->execute();

      // Delete the content type group
      db_delete(self::$table_groups)
        ->condition('type', $this->type)
        ->execute();
    } catch (Exception $e) {
      $transaction
        ->rollback();
      watchdog_exception('content type groups', $e);
      throw $e;
    }

    // Clear the current content type group
    $this->type = NULL;
    $this->name = NULL;
    $this->content_types = array();
    return $this;
  }

  /**
   * Adds a new content type to the current content type group.
   *
   * @param string $content_type
   *   Machine name of the content type group to add
   *
   * @param int $weight
   *
   */
  public function addContentType($content_type, $weight = 0) {
    $this->content_types[$content_type] = array(
      'name' => NULL,
      'weight' => $weight,
    );
  }

  /**
   * Removes the given content type from the current content type group.
   *
   * @param string $content_type
   *   Machine name of the content type to remove
   */
  public function deleteContentType($content_type) {
    if (isset($this->content_types[$content_type])) {
      unset($this->content_types[$content_type]);
    }
  }

  /**
   * Returns a list of content types belonging to this group
   * in a format made for easy looping.
   *
   * @param bool $fetch_as_full
   *   If TRUE, returns the data as full type information
   *
   * @return array
   *   $machine_name => $name
   */
  public function typeList($fetch_as_full = FALSE) {
    if ($fetch_as_full) {
      return $this->content_types;
    }
    else {
      $types = array();
      foreach ($this->content_types as $machine_name => $data) {
        $types[$machine_name] = $data['name'];
      }
      return $types;
    }
  }

  /**
   * Renames the given content type across all content type groups.
   * Use this when a content type's machine name is renamed.
   *
   * @param string $old_content_type
   *   The content type's old machine name.
   * @param string $new_content_type
   *   The content type's new machine name.
   */
  public static function renameContentType($old_content_type, $new_content_type) {
    $result = db_update(self::$table_types)
      ->fields(array(
      'content_type' => $new_content_type,
    ))
      ->condition('content_type', $old_content_type)
      ->execute();
    return $result;
  }

  /**
   * Removes the given content type across all content type groups.
   * Use this when a content type is deleted.
   *
   * @param string $old_content_type
   *   The content type's old machine name.
   */
  public static function removeContentType($content_type) {
    $result = db_delete(self::$table_types)
      ->condition('content_type', $content_type)
      ->execute();
    return $result;
  }

  /**
   * Returns a list of all content type groups.
   *
   * @return array
   *   $machine_name => ContentTypeGroup($machine_name)
   */
  public static function fetch($fetch_as_objects = FALSE) {
    if ($fetch_as_objects) {
      $result = db_select(self::$table_groups, 'g')
        ->fields('g', array(
        'type',
      ))
        ->execute();
      $data = array();
      foreach ($result as $row) {
        $data[$row->type] = new ContentTypeGroup($row->type);
      }
    }
    else {

      // We only need to do $machine_name => $display_name
      $result = db_select(self::$table_groups, 'g')
        ->fields('g', array(
        'type',
        'name',
      ))
        ->execute();
      $data = array();
      foreach ($result as $row) {
        $data[$row->type] = $row->name;
      }
    }
    return $data;
  }

  /**
   * Property setter
   */
  public function __set($property, $val) {
    switch ($property) {
      case 'type':
      case 'name':

        // Sanitize and trim input
        $this->{$property} = trim(check_plain($val));
    }
  }

  /**
   * Property getter
   */
  public function __get($property) {
    switch ($property) {
      case 'type':
      case 'name':
        return $this->{$property};
      default:
        return NULL;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentTypeGroup::$content_types public property
ContentTypeGroup::$name protected property
ContentTypeGroup::$table_groups private static property
ContentTypeGroup::$table_types private static property
ContentTypeGroup::$type protected property
ContentTypeGroup::addContentType public function Adds a new content type to the current content type group.
ContentTypeGroup::delete public function Deletes the current content type group.
ContentTypeGroup::deleteContentType public function Removes the given content type from the current content type group.
ContentTypeGroup::fetch public static function Returns a list of all content type groups.
ContentTypeGroup::removeContentType public static function Removes the given content type across all content type groups. Use this when a content type is deleted.
ContentTypeGroup::renameContentType public static function Renames the given content type across all content type groups. Use this when a content type's machine name is renamed.
ContentTypeGroup::save public function Creates or updates the current content type group.
ContentTypeGroup::typeList public function Returns a list of content types belonging to this group in a format made for easy looping.
ContentTypeGroup::__construct public function
ContentTypeGroup::__get public function Property getter
ContentTypeGroup::__set public function Property setter