newsletter_template.module in Newsletter 7.2
Module for the Newsletter Template Entity
File
modules/template/newsletter_template.moduleView source
<?php
/**
* @file
* Module for the Newsletter Template Entity
*/
/**
* Implements hook_menu().
function newsletter_template_menu() {
$items = array();
$items['admin/config/media/newsletter/templates'] = array(
'title' => 'Templates',
'description' => 'Administer newsletter templates.',
'access arguments' => array('administer newsletter templates'),
'page callback' => 'drupal_get_form',
'page arguments' => array('newsletter_template_list'),
'file' => 'includes/newsletter_template.admin.inc',
'weight' => 4,
);
$items['admin/config/media/newsletter/templates/mail'] = array(
'title' => 'Mail templates',
'description' => 'Administer mail templates.',
'access arguments' => array('administer newsletter templates'),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/config/media/newsletter/templates/mail/add'] = array(
'title' => 'Add new mail template',
'description' => 'Administer mail templates.',
'access arguments' => array('administer newsletter templates'),
'type' => MENU_LOCAL_ACTION,
'page callback' => 'newsletter_template_add',
'file' => 'includes/newsletter_template.admin.inc',
);
$items['admin/config/media/newsletter/templates/delete/%newsletter_template'] = array(
'title' => 'Delete newsletter templates',
'page callback' => 'drupal_get_form',
'page arguments' => array('newsletter_template_delete', 6),
'access arguments' => array('administer newsletter templatess'),
'file' => 'includes/newsletter_template.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/config/media/newsletter/templates/edit/%newsletter_template'] = array(
'title' => 'Edit newsletter templates',
'page callback' => 'drupal_get_form',
'page arguments' => array('newsletter_template_edit', 6),
'access arguments' => array('administer newsletter templates'),
'file' => 'includes/newsletter_template.admin.inc',
'type' => MENU_CALLBACK,
);
return $items;
} */
/**
* Implements hook_entity_info().
*/
function newsletter_template_entity_info() {
$entities['newsletter_template'] = array(
'label' => t('Newsletter template'),
'entity class' => 'NewsletterTemplate',
'controller class' => 'NewsletterTemplateController',
'base table' => 'newsletter_template',
'revision table' => 'newsletter_template_revision',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'template_id',
'revision' => 'revision_id',
'bundle' => 'type',
),
// Bundles are defined by the subscriber types below.
'bundles' => array(),
'bundle keys' => array(
'bundle' => 'type',
),
'label callback' => 'entity_class_label',
'uri callback' => 'entity_class_uri',
'creation callback' => 'newsletter_template_create',
'access callback' => 'newsletter_template_access',
'module' => 'newsletter_template',
// The information below is used by the NewsletterTemplateUIController.
'admin ui' => array(
'path' => 'admin/config/media/newsletter/templates',
'file' => 'includes/newsletter_template.admin.inc',
'controller class' => 'NewsletterTemplateUIController',
'menu wildcard' => '%newsletter_template',
),
);
// Add bundle info but bypass entity_load() as we cannot use it here.
$types = db_select('newsletter_template_type', 'ns')
->fields('ns')
->execute()
->fetchAllAssoc('type');
foreach ($types as $type => $info) {
$entities['newsletter_template']['bundles'][$type] = array(
'label' => $info->name,
'admin' => array(
'path' => 'admin/structure/newsletter-template-types/manage/%newsletter_template_type',
'real path' => 'admin/structure/newsletter-template-types/manage/' . $type,
'bundle argument' => 4,
'access arguments' => array(
'administer newsletter template types',
),
),
);
}
// The entity that holds information about the entity types.
$entities['newsletter_template_type'] = array(
'label' => t('Newsletter template type'),
'entity class' => 'NewsletterTemplateType',
'controller class' => 'NewsletterTemplateTypeController',
'base table' => 'newsletter_template_type',
'fieldable' => FALSE,
'bundle of' => 'newsletter_template',
'exportable' => TRUE,
'entity keys' => array(
'id' => 'id',
'name' => 'type',
'label' => 'name',
),
'access callback' => 'newsletter_template_type_access',
'module' => 'newsletter_template',
// Enable the entity API's admin UI.
'admin ui' => array(
'path' => 'admin/structure/newsletter-template-types',
'file' => 'includes/newsletter_template.admin.inc',
'controller class' => 'NewsletterTemplateTypeUIController',
),
);
return $entities;
}
/**
* Implements hook_permission().
*/
function newsletter_template_permission() {
// We set up permisssions to manage entity types, manage all entities and the
// permissions for each individual entity.
$permissions = array(
'administer newsletter template types' => array(
'title' => t('Administer newsletter template types'),
'description' => t('Create and delete fields for newsletter template types, and set their permissions.'),
),
'administer newsletter templates' => array(
'title' => t('Administer newsletter templates'),
'description' => t('Edit and delete all newsletter templates'),
),
);
// Generate permissions per template type.
foreach (newsletter_template_get_types() as $type) {
$type_name = check_plain($type->type);
$permissions += array(
"edit any {$type_name} template" => array(
'title' => t('%type_name: Edit any newsletter template', array(
'%type_name' => $type->name,
)),
),
"view any {$type_name} template" => array(
'title' => t('%type_name: View any newsletter template', array(
'%type_name' => $type->name,
)),
),
);
}
return $permissions;
}
/**
* Implements hook_views_api().
*/
function newsletter_template_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'newsletter_template') . '/includes/views',
);
}
/**
* Determines whether the given user has access to a newsletter template.
*
* @param $op
* The operation being performed. One of 'view', 'update', 'create', 'delete'
* or just 'edit' (being the same as 'create' or 'update').
* @param $template
* Optionally a template or a template type to check access for.
* If nothing is given, access for all templates is determined.
* @param $account
* The user to check for. Leave it to NULL to check for the global user.
*
* @return boolean
* Whether access is allowed or not.
*/
function newsletter_template_access($op, $template = NULL, $account = NULL) {
if (user_access('administer newsletter templates', $account)) {
return TRUE;
}
if (isset($template) && ($type_name = $template->type)) {
$op = $op == 'view' ? 'view' : 'edit';
if (user_access("{$op} any {$type_name} newsletter template", $account)) {
return TRUE;
}
elseif ($template->uid == $GLOBALS['user']->uid && user_access('subscribe newsletters')) {
return TRUE;
}
}
return FALSE;
}
/**
* Access callback for the entity API.
*/
function newsletter_template_type_access($op, $type = NULL, $account = NULL) {
return user_access('administer newsletter subscriber types', $account);
}
/**
* Gets an array of all newsletter template types, keyed by the type name.
*
* @param $type_name
* If set, the type with the given name is returned.
* @return NeswletterTemplateType[]
* Depending whether $type isset, an array of newsletter template types
* or a single one.
*/
function newsletter_template_get_types($type_name = NULL) {
$types = entity_load_multiple_by_name('newsletter_template_type', isset($type_name) ? array(
$type_name,
) : FALSE);
return isset($type_name) ? reset($types) : $types;
}
/**
* Menu argument loader; Load a template type by string.
*
* @param $type
* The machine-readable name of a template type to load.
*
* @return
* A template type array or FALSE if $type does not exist.
*/
function newsletter_template_type_load($type) {
return newsletter_template_get_types($type);
}
/**
* Fetch a template object.
*
* @param $template_id
* Integer specifying the template id.
* @param $reset
* A boolean indicating that the internal cache should be reset.
*
* @return
* A fully-loaded $template object or FALSE if it cannot be loaded.
*
* @see newsletter_template_load_multiple()
*/
function newsletter_template_load($template_id, $reset = FALSE) {
if (empty($template_id)) {
return array();
}
$templates = newsletter_template_load_multiple(array(
$template_id,
), array(), $reset);
return reset($templates);
}
/**
* Load multiple templates by IDs.
*
* @param $template_ids
* An array of template IDs.
* @param $reset
* A boolean indicating that the internal cache should be reset.
*
* @return
* An array of template objects, indexed by template_id.
*
* @see entity_load()
* @see newsletter_template_load()
*/
function newsletter_template_load_multiple($template_ids = array(), $reset = FALSE) {
return entity_load('newsletter_template', $template_ids, array(), $reset);
}
/**
* Deletes a template.
*/
function newsletter_template_delete(NewsletterTemplate $template) {
$template
->delete();
}
/**
* Delete multiple templates.
*
* @param $template_ids
* An array of newsletter template IDs.
*/
function newsletter_template_delete_multiple(array $template_ids) {
entity_get_controller('newsletter_template')
->delete($template_ids);
}
/**
* Create a template object.
*/
function newsletter_template_create($values = array()) {
return entity_get_controller('newsletter_template')
->create($values);
}
/**
* Saves a template to the database.
*
* @param $template
* The template object.
*/
function newsletter_template_save(NewsletterTemplate $template) {
return $template
->save();
}
/**
* Create a template type object.
*/
function newsletter_template_type_create($values = array()) {
return entity_get_controller('newsletter_template_type')
->create($values);
}
/**
* Saves a template type to the db.
*/
function newsletter_template_type_save(NewsletterTemplateType $type) {
$type
->save();
}
/**
* Deletes a template type from the db.
*/
function newsletter_template_type_delete(NewsletterTemplateType $type) {
$type
->delete();
}
/**
* The class used for newsletter template entities.
*/
class NewsletterTemplate extends Entity {
public function __construct($values = array()) {
parent::__construct($values, 'newsletter_template');
}
protected function defaultLabel() {
return $this->subject;
}
}
/**
* The class used for newsletter template type entities.
*/
class NewsletterTemplateType extends Entity {
public $type;
public $label;
public function __construct($values = array()) {
parent::__construct($values, 'newsletter_template_type');
}
}
Functions
Name | Description |
---|---|
newsletter_template_access | Determines whether the given user has access to a newsletter template. |
newsletter_template_create | Create a template object. |
newsletter_template_delete | Deletes a template. |
newsletter_template_delete_multiple | Delete multiple templates. |
newsletter_template_entity_info | Implements hook_entity_info(). |
newsletter_template_get_types | Gets an array of all newsletter template types, keyed by the type name. |
newsletter_template_load | Fetch a template object. |
newsletter_template_load_multiple | Load multiple templates by IDs. |
newsletter_template_permission | Implements hook_permission(). |
newsletter_template_save | Saves a template to the database. |
newsletter_template_type_access | Access callback for the entity API. |
newsletter_template_type_create | Create a template type object. |
newsletter_template_type_delete | Deletes a template type from the db. |
newsletter_template_type_load | Menu argument loader; Load a template type by string. |
newsletter_template_type_save | Saves a template type to the db. |
newsletter_template_views_api | Implements hook_views_api(). |
Classes
Name | Description |
---|---|
NewsletterTemplate | The class used for newsletter template entities. |
NewsletterTemplateType | The class used for newsletter template type entities. |