View source
<?php
function content_type_groups_help($path, $arg) {
if ($path == 'admin/help#content_type_groups') {
$output = '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t("The Content type groups module allows you to create groups of content types for reference in forms and views") . '</p>';
$output .= '<p>' . t("The module provides a configuration screen allowing administrators to define an unlimited amount of content type groups") . '</p>';
return $output;
}
}
function content_type_groups_menu() {
$items = array();
$items['admin/structure/types/groups'] = array(
'title' => 'Content type groups',
'description' => 'Manage content type groups',
'page callback' => 'content_type_groups_admin',
'access arguments' => array(
'administer content types',
),
'file' => 'content_type_groups.admin.inc',
'type' => MENU_LOCAL_ACTION,
);
$items['admin/structure/types/groups/add'] = array(
'title' => 'Add content type group',
'description' => 'Add a new content type group',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'content_type_groups_group_form',
),
'access arguments' => array(
'administer content types',
),
'file' => 'content_type_groups.admin.inc',
'type' => MENU_LOCAL_ACTION,
);
$items['admin/structure/types/groups/manage/%'] = array(
'title' => 'Edit content type group',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'content_type_groups_group_form',
5,
),
'access arguments' => array(
'administer content types',
),
'file' => 'content_type_groups.admin.inc',
);
$items['admin/structure/types/groups/manage/%/edit'] = array(
'title' => 'Edit',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/structure/types/groups/manage/%/delete'] = array(
'title' => 'Delete',
'page arguments' => array(
'content_type_groups_group_delete_confirm',
5,
),
'access arguments' => array(
'administer content types',
),
'file' => 'content_type_groups.admin.inc',
);
return $items;
}
function content_type_groups_node_type_update($info) {
if (isset($info->old_type)) {
ContentTypeGroup::renameContentType($info->old_type, $info->type);
}
}
function content_type_groups_node_type_delete($info) {
ContentTypeGroup::removeContentType($info->type);
}
function content_type_groups_theme() {
return array(
'content_type_groups_group_form' => array(
'render element' => 'form',
'file' => 'content_type_groups.theme.inc',
),
);
}
function content_type_groups_views_api() {
return array(
'api' => 2,
);
}
function content_type_groups_features_api() {
return array(
'content_type_groups' => array(
'name' => t('Content type groups'),
'default_hook' => 'content_type_groups_features_default_settings',
),
);
}
class ContentTypeGroup {
protected $type;
protected $name;
public $content_types = array();
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 ($type) {
$result = db_select(self::$table_groups, 'g')
->fields('g', array(
'name',
))
->condition('type', $type, '=')
->execute()
->fetchAssoc();
if ($result) {
$this->name = $result['name'];
$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,
);
}
}
}
}
}
public function save() {
$transaction = db_transaction();
try {
$result = db_merge(self::$table_groups)
->key(array(
'type' => $this->type,
))
->fields(array(
'name' => $this->name,
))
->execute();
if ($result) {
db_delete(self::$table_types)
->condition('group_type', $this->type)
->execute();
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;
}
public function delete() {
$transaction = db_transaction();
try {
db_delete(self::$table_types)
->condition('group_type', $this->type)
->execute();
db_delete(self::$table_groups)
->condition('type', $this->type)
->execute();
} catch (Exception $e) {
$transaction
->rollback();
watchdog_exception('content type groups', $e);
throw $e;
}
$this->type = NULL;
$this->name = NULL;
$this->content_types = array();
return $this;
}
public function addContentType($content_type, $weight = 0) {
$this->content_types[$content_type] = array(
'name' => NULL,
'weight' => $weight,
);
}
public function deleteContentType($content_type) {
if (isset($this->content_types[$content_type])) {
unset($this->content_types[$content_type]);
}
}
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;
}
}
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;
}
public static function removeContentType($content_type) {
$result = db_delete(self::$table_types)
->condition('content_type', $content_type)
->execute();
return $result;
}
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 {
$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;
}
public function __set($property, $val) {
switch ($property) {
case 'type':
case 'name':
$this->{$property} = trim(check_plain($val));
}
}
public function __get($property) {
switch ($property) {
case 'type':
case 'name':
return $this->{$property};
default:
return NULL;
}
}
}