You are here

content_type_groups.controller.inc in Content type groups 7.2

Contains controllers and entity hooks for the content_type_group entity.

File

content_type_groups.controller.inc
View source
<?php

/**
 * @file
 * Contains controllers and entity hooks for the content_type_group entity.
 */

/**
 * UI controller for Content Type Group.
 */
class ContentTypeGroupUIController extends EntityDefaultUIController {

  /**
   * Overrides hook_menu() defaults.
   */
  public function hook_menu() {
    $items = parent::hook_menu();
    $wildcard = '%content_type_group';

    // List of all CTGs
    $items[$this->path] = array(
      'title' => 'Content type groups',
      'description' => 'Manage content type groups',
      'page callback' => 'content_type_groups_admin',
      'file' => 'content_type_groups.admin.inc',
      'file path' => drupal_get_path('module', $this->entityInfo['module']),
      'access arguments' => array(
        'administer content types',
      ),
      // This permission is good enough for now. Does it really need a separate permission, honestly?
      'type' => MENU_LOCAL_ACTION,
    );

    // Add new CTG
    $items[$this->path . '/add'] = array(
      'title' => 'Add content type group',
      'description' => 'Add a new content type group',
      'page callback' => 'content_type_groups_admin_add_page',
      'file' => 'content_type_groups.admin.inc',
      'file path' => drupal_get_path('module', $this->entityInfo['module']),
      'type' => MENU_LOCAL_ACTION,
      'access arguments' => array(
        'administer content types',
      ),
    );

    // Edit existing CTG
    $items[$this->path . '/' . $wildcard . '/edit'] = array(
      'title' => 'Edit content type group',
      'description' => 'Edit an existing content type group',
      'load arguments' => array(
        TRUE,
      ),
      'page callback' => 'entity_ui_get_form',
      'page arguments' => array(
        'content_type_group',
        4,
      ),
      'file' => 'content_type_groups.admin.inc',
      'file path' => drupal_get_path('module', $this->entityInfo['module']),
      'access arguments' => array(
        'administer content types',
      ),
    );

    // Delete existing CTG
    $items[$this->path . '/' . $wildcard . '/delete'] = array(
      'title' => 'Delete content type group',
      'description' => 'Delete an existing content type group',
      'load arguments' => array(
        TRUE,
      ),
      'page callback' => 'drupal_get_form',
      'page arguments' => array(
        'content_type_group_form_delete_confirm',
        4,
      ),
      'file' => 'content_type_groups.admin.inc',
      'file path' => drupal_get_path('module', $this->entityInfo['module']),
      'access arguments' => array(
        'administer content types',
      ),
    );
    return $items;
  }

}

/**
 * Content Type Group controller (equivalent to a model in MVC).
 */
class ContentTypeGroupController extends EntityAPIController {
  public function create(array $values = array()) {
    $values += array(
      'type' => NULL,
      'name' => '',
      'content_types' => array(),
    );
    return parent::create($values);
  }
  protected function buildQuery($ids, $conditions = array(), $revision_id = FALSE) {
    $query = parent::buildQuery($ids, $conditions, $revision_id);

    // Specify additional fields from the user and node tables.
    $query
      ->leftJoin('content_type_groups_types', 't', 'base.type = t.group_type');
    $query
      ->addField('t', 'content_type', 'content_type');
    $query
      ->addField('t', 'weight', 'weight');
    return $query;
  }

  /**
   * Overridden.
   * @see EntityAPIController#load($ids, $conditions)
   *
   * In contrast to the parent implementation, multiple rows are
   * grouped by field type to handle multiple content types for each CTG.
   * This is identical to parent::load() except for the marked section.
   */
  public function load($ids = array(), $conditions = array()) {
    $entities = array();

    // Revisions are not statically cached, and require a different query to
    // other conditions, so separate the revision id into its own variable.
    if ($this->revisionKey && isset($conditions[$this->revisionKey])) {
      $revision_id = $conditions[$this->revisionKey];
      unset($conditions[$this->revisionKey]);
    }
    else {
      $revision_id = FALSE;
    }

    // Create a new variable which is either a prepared version of the $ids
    // array for later comparison with the entity cache, or FALSE if no $ids
    // were passed. The $ids array is reduced as items are loaded from cache,
    // and we need to know if it's empty for this reason to avoid querying the
    // database when all requested entities are loaded from cache.
    $passed_ids = !empty($ids) ? array_flip($ids) : FALSE;

    // Try to load entities from the static cache.
    if ($this->cache && !$revision_id) {
      $entities = $this
        ->cacheGet($ids, $conditions);

      // If any entities were loaded, remove them from the ids still to load.
      if ($passed_ids) {
        $ids = array_keys(array_diff_key($passed_ids, $entities));
      }
    }

    // Support the entitycache module if activated.
    if (!empty($this->entityInfo['entity cache']) && !$revision_id && $ids && !$conditions) {
      $cached_entities = EntityCacheControllerHelper::entityCacheGet($this, $ids, $conditions);

      // If any entities were loaded, remove them from the ids still to load.
      $ids = array_diff($ids, array_keys($cached_entities));
      $entities += $cached_entities;

      // Add loaded entities to the static cache if we are not loading a
      // revision.
      if ($this->cache && !empty($cached_entities) && !$revision_id) {
        $this
          ->cacheSet($cached_entities);
      }
    }

    // Load any remaining entities from the database. This is the case if $ids
    // is set to FALSE (so we load all entities), if there are any ids left to
    // load or if loading a revision.
    if (!($this->cacheComplete && $ids === FALSE && !$conditions) && ($ids === FALSE || $ids || $revision_id)) {
      $queried_entities = array();
      foreach ($this
        ->query($ids, $conditions, $revision_id) as $record) {

        // Skip entities already retrieved from cache.
        if (isset($entities[$record->{$this->idKey}])) {
          continue;
        }

        // For DB-based entities take care of serialized columns.
        if (!empty($this->entityInfo['base table'])) {
          $schema = drupal_get_schema($this->entityInfo['base table']);
          foreach ($schema['fields'] as $field => $info) {
            if (!empty($info['serialize']) && isset($record->{$field})) {
              $record->{$field} = unserialize($record->{$field});

              // Support automatic merging of 'data' fields into the entity.
              if (!empty($info['merge']) && is_array($record->{$field})) {
                foreach ($record->{$field} as $key => $value) {
                  $record->{$key} = $value;
                }
                unset($record->{$field});
              }
            }
          }
        }

        // THIS IS THE ONLY PART OVERRIDDEN FROM PARENT
        $all_node_types = node_type_get_names();
        if (isset($queried_entities[$record->{$this->idKey}])) {
          $queried_entities[$record->{$this->idKey}]->content_types[$record->content_type] = array(
            'name' => $all_node_types[$record->content_type],
            '#weight' => $record->weight,
          );
        }
        else {
          $content_type = array(
            'name' => $all_node_types[$record->content_type],
            '#weight' => $record->weight,
          );
          $record->content_types = array(
            $record->content_type => $content_type,
          );
          unset($record->content_type);
          unset($record->weight);
          $queried_entities[$record->{$this->idKey}] = $record;
        }

        // END OVERRIDE
      }
    }

    // Pass all entities loaded from the database through $this->attachLoad(),
    // which attaches fields (if supported by the entity type) and calls the
    // entity type specific load callback, for example hook_node_load().
    if (!empty($queried_entities)) {
      $this
        ->attachLoad($queried_entities, $revision_id);
      $entities += $queried_entities;
    }

    // Entitycache module support: Add entities to the entity cache if we are
    // not loading a revision.
    if (!empty($this->entityInfo['entity cache']) && !empty($queried_entities) && !$revision_id) {
      EntityCacheControllerHelper::entityCacheSet($this, $queried_entities);
    }
    if ($this->cache) {

      // Add entities to the cache if we are not loading a revision.
      if (!empty($queried_entities) && !$revision_id) {
        $this
          ->cacheSet($queried_entities);

        // Remember if we have cached all entities now.
        if (!$conditions && $ids === FALSE) {
          $this->cacheComplete = TRUE;
        }
      }
    }

    // Ensure that the returned array is ordered the same as the original
    // $ids array if this was passed in and remove any invalid ids.
    if ($passed_ids && ($passed_ids = array_intersect_key($passed_ids, $entities))) {
      foreach ($passed_ids as $id => $value) {
        $passed_ids[$id] = $entities[$id];
      }
      $entities = $passed_ids;
    }
    return $entities;
  }

}
class ContentTypeGroup extends Entity {
  protected function defaultLabel() {
    return $this->name;
  }
  protected function defaultUri() {
    return array(
      'path' => CONTENT_TYPE_GROUPS_ADMIN_PATH . '/manage/' . $this
        ->identifier(),
    );
  }

}

// ---------------------------------------
// ENTITY CRUD HOOKS
// ---------------------------------------

/**
 * Implements hook_entity_presave().
 */
function content_type_groups_entity_presave($entity, $entity_type) {
  if ($entity_type == CONTENT_TYPE_GROUPS_ENTITY_NAME) {

    // The form submits all checkboxes with unchecked ones having a value of 0. We need
    // to remove those and only keep the ones that were checked for saving in the database.
    foreach ($entity->content_types as $content_type => $checked) {
      if (!$checked) {
        unset($entity->content_types[$content_type]);
      }
    }
  }
}

/**
 * Implements hook_entity_insert().
 */
function content_type_gorups_entity_insert($entity, $entity_type) {
  if ($entity_type == CONTENT_TYPE_GROUPS_ENTITY_NAME) {
    dpm($entity);

    /*
    // Delete all types for this group
    db_delete('content_type_groups_types')
      ->condition('group_type', $entity->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();
    }

    // Add the allowed methods to the key-methods permissions table
    foreach ($entity->content_types as $content_type) {
      db_insert('csm_api_keys_methods')
        ->fields(array(
            'api_key' => $entity->api_key,
            'method'  => $method,
          ))
        ->execute();
    }
    */
  }
}

Functions

Classes

Namesort descending Description
ContentTypeGroup
ContentTypeGroupController Content Type Group controller (equivalent to a model in MVC).
ContentTypeGroupUIController UI controller for Content Type Group.