View source
<?php
define('ROOMS_NOT_AVAILABLE', 0);
define('ROOMS_AVAILABLE', 1);
define('ROOMS_ON_REQUEST', 2);
define('ROOMS_ANON_BOOKED', 3);
define('ROOMS_UNCONFIRMED_BOOKINGS', -1);
define('ROOMS_BLOCKED', 100);
define('ROOMS_UPDATED', 200);
define('ROOMS_WRONG_UNIT', 300);
function rooms_unit_menu() {
$items = array();
$items['admin/rooms/unit-type/description-source'] = array(
'title' => 'Unit type description source',
'page callback' => 'rooms_unit_type_description_source',
'access arguments' => array(
'bypass rooms_unit entities access',
),
'type' => MENU_CALLBACK,
);
return $items;
}
function rooms_unit_entity_info() {
$return['rooms_unit'] = array(
'label' => t('Bookable Units'),
'entity class' => 'RoomsUnit',
'controller class' => 'RoomsUnitController',
'base table' => 'rooms_units',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'unit_id',
'bundle' => 'type',
'label' => 'name',
),
'bundles' => array(),
'bundle keys' => array(
'bundle' => 'type',
),
'view modes' => array(
'display' => array(
'label' => t('Display'),
'custom settings' => FALSE,
),
),
'label callback' => 'entity_class_label',
'uri callback' => 'entity_class_uri',
'creation callback' => 'rooms_unit_create',
'access callback' => 'rooms_unit_access',
'access arguments' => array(
'user key' => 'uid',
'access tag' => 'rooms_unit_access',
),
'permission labels' => array(
'singular' => t('bookable unit'),
'plural' => t('bookable units'),
),
'module' => 'rooms_unit',
'admin ui' => array(
'path' => 'admin/rooms/units',
'file' => 'rooms_unit.admin.inc',
'controller class' => 'RoomsUnitUIController',
'menu wildcard' => '%rooms_unit',
),
'translation' => array(
'entity_translation' => array(
'base path' => 'admin/rooms/units/unit/%rooms_unit',
'default settings' => array(
'default_language' => LANGUAGE_NONE,
'hide_language_selector' => FALSE,
),
),
),
);
$return['rooms_unit_type'] = array(
'label' => t('Bookable Unit Type'),
'entity class' => 'RoomsUnitType',
'controller class' => 'RoomsUnitTypeController',
'base table' => 'rooms_unit_type',
'fieldable' => TRUE,
'bundle of' => 'rooms_unit',
'exportable' => TRUE,
'entity keys' => array(
'id' => 'id',
'name' => 'type',
'label' => 'label',
),
'access callback' => 'rooms_unit_type_access',
'module' => 'rooms_unit',
'admin ui' => array(
'path' => 'admin/rooms/units/unit-types',
'file' => 'rooms_unit_type.admin.inc',
'controller class' => 'RoomsUnitTypeUIController',
),
);
return $return;
}
function rooms_unit_entity_info_alter(&$entity_info) {
foreach (rooms_unit_get_types() as $type => $info) {
$entity_info['rooms_unit']['bundles'][$type] = array(
'label' => $info->label,
'admin' => array(
'path' => 'admin/rooms/units/unit-types/manage/%rooms_unit_type',
'real path' => 'admin/rooms/units/unit-types/manage/' . $type,
'bundle argument' => 5,
'access arguments' => array(
'bypass rooms_unit entities access',
),
),
);
}
$entity_info['node']['view modes']['rooms_list'] = array(
'label' => t('Rooms Results View'),
'custom settings' => FALSE,
);
}
function rooms_unit_permission() {
$permissions = array(
'administer rooms_unit_type entities' => array(
'title' => t('Administer bookable unit types'),
'description' => t('Allows users to add bookable unit types and configure their fields.'),
'restrict access' => TRUE,
),
'view any rooms_unit unpublished entity' => array(
'title' => t('View any unpublished bookable unit'),
'description' => t('Allows users to view any unpublished bookable unit.'),
'restrict access' => TRUE,
),
'view own rooms_unit unpublished entities' => array(
'title' => t('View own unpublished bookable units'),
'description' => t('Allows users to view own unpublished bookable units.'),
),
'manage room units' => array(
'title' => t('Manage Room Units'),
'description' => t('Allows access to the Rooms Unit Admin'),
'restrict access' => TRUE,
),
);
$permissions += rooms_entity_access_permissions('rooms_unit');
$entity_info = entity_get_info('rooms_unit');
foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
$permissions['view own rooms_unit entities of bundle ' . $bundle_name] = array(
'title' => t('View own published %bundle @entity_type', array(
'@entity_type' => 'bookable units',
'%bundle' => $bundle_info['label'],
)),
);
$permissions['view any rooms_unit entity of bundle ' . $bundle_name] = array(
'title' => t('View any published %bundle @entity_type', array(
'@entity_type' => 'bookable unit',
'%bundle' => $bundle_info['label'],
)),
);
}
return $permissions;
}
function rooms_unit_access($op, $unit = NULL, $account = NULL) {
return rooms_entity_access($op, $unit, $account, 'rooms_unit');
}
function rooms_unit_access_filter($op, $units = array(), $account = NULL) {
$filtered_units = array();
if (empty($account)) {
$account = $GLOBALS['user'];
}
foreach ($units as $key => $unit) {
if (rooms_unit_access($op, $unit, $account)) {
$filtered_units[$key] = $unit;
}
}
return $filtered_units;
}
function rooms_unit_type_access($op, $type = NULL, $account = NULL) {
return user_access('administer rooms_unit_type entities', $account);
}
function rooms_unit_query_rooms_unit_access_alter(QueryAlterableInterface $query) {
foreach ($query
->getTables() as $table) {
if ($table['table'] === 'rooms_units') {
rooms_entity_access_query_alter($query, 'rooms_unit', $table['alias']);
break;
}
}
}
function rooms_unit_rooms_entity_access_view_condition_rooms_unit_alter(&$conditions, $context) {
$account = $context['account'];
if (user_access('view any rooms_unit unpublished entity', $account)) {
return;
}
$old_conditions = $conditions;
if ($old_conditions
->count()) {
$conditions = db_and();
if ($account->uid && user_access('view own rooms_unit unpublished entities', $account)) {
$or = db_and()
->condition($context['base_table'] . '.status', 0)
->condition($context['base_table'] . '.uid', $account->uid);
$conditions
->condition(db_or()
->condition($or)
->condition($context['base_table'] . '.status', 1));
}
else {
$conditions
->condition($context['base_table'] . '.status', 1);
}
$conditions
->condition($old_conditions);
}
}
function _rooms_unit_add_access() {
if (user_access('administer rooms_unit_type entities')) {
return TRUE;
}
$types = rooms_unit_get_types();
foreach ($types as $type) {
if (rooms_unit_access('create', rooms_unit_create(array(
'type' => $type->type,
'uid' => 0,
)))) {
return TRUE;
}
}
return FALSE;
}
function rooms_unit_get_types($type_name = NULL, $reset = FALSE) {
$types = entity_load_multiple_by_name('rooms_unit_type', isset($type_name) ? array(
$type_name,
) : FALSE);
return isset($type_name) ? reset($types) : $types;
}
function rooms_unit_types_ids() {
$unit_types = array();
$types = rooms_unit_get_types();
foreach ($types as $type) {
$unit_types[$type->type] = $type->label;
}
return $unit_types;
}
function rooms_unit_entity_insert($entity, $type) {
if ($type == 'rooms_unit_type') {
if (field_read_field('rooms_booking_unit_options') === FALSE) {
$field = array(
'field_name' => 'rooms_booking_unit_options',
'type' => 'rooms_options',
'cardinality' => -1,
);
field_create_field($field);
}
if (field_read_instance('rooms_unit', 'rooms_booking_unit_options', $entity->type) === FALSE) {
$instance = array(
'field_name' => 'rooms_booking_unit_options',
'entity_type' => 'rooms_unit',
'label' => 'Options',
'bundle' => $entity->type,
'required' => FALSE,
'widget' => array(
'type' => 'rooms_options',
),
);
field_create_instance($instance);
}
}
}
function rooms_unit_type_load($type, $reset = FALSE) {
return rooms_unit_get_types($type, $reset);
}
function rooms_unit_load($unit_id, $reset = FALSE) {
$units = rooms_unit_load_multiple(array(
$unit_id,
), array(), $reset);
return reset($units);
}
function rooms_unit_load_multiple($unit_ids = array(), $conditions = array(), $reset = FALSE) {
return entity_load('rooms_unit', $unit_ids, $conditions, $reset);
}
function rooms_unit_delete(RoomsUnit $unit) {
$unit
->delete();
}
function rooms_unit_delete_multiple(array $unit_ids) {
entity_get_controller('rooms_unit')
->delete($unit_ids);
}
function rooms_unit_create($values = array()) {
return entity_get_controller('rooms_unit')
->create($values);
}
function rooms_unit_save(RoomsUnit $unit) {
return $unit
->save();
}
function rooms_unit_type_create($values = array()) {
return entity_get_controller('rooms_unit_type')
->create($values);
}
function rooms_unit_type_save(RoomsUnitType $type) {
$type
->save();
}
function rooms_unit_type_delete(RoomsUnitType $type) {
$type
->delete();
}
function rooms_unit_uri(RoomsUnit $unit) {
return array(
'path' => 'unit/' . $unit->unit_id,
);
}
function rooms_unit_page_title(RoomsUnit $unit) {
return $unit->name;
}
function rooms_unit_ids($bundle = '') {
$units = array();
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'rooms_unit');
if ($bundle != '') {
$query
->entityCondition('bundle', $bundle);
}
$result = $query
->execute();
if (count($result) > 0) {
$entities = entity_load('rooms_unit', array_keys($result['rooms_unit']));
foreach ($entities as $unit) {
$wrapper = entity_metadata_wrapper('rooms_unit', $unit);
$units[$wrapper->unit_id
->value()] = $wrapper->name
->value();
}
}
return $units;
}
function rooms_unit_page_view($unit, $view_mode = 'full') {
$controller = entity_get_controller('rooms_unit');
$content = $controller
->view(array(
$unit->unit_id => $unit,
), $view_mode);
drupal_set_title($unit->name);
return $content;
}
function rooms_unit_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'rooms_unit') . '/views',
);
}
function rooms_unit_theme() {
return array(
'rooms_unit_add_list' => array(
'variables' => array(
'content' => array(),
),
'file' => 'rooms_unit.admin.inc',
),
'rooms_unit' => array(
'render element' => 'elements',
'template' => 'rooms_unit',
),
'rooms_unit_extra_data' => array(
'variables' => array(
'unit_extra_data',
'unit' => NULL,
),
'template' => 'rooms_unit-extra-data',
),
);
}
function rooms_unit_type_description_source($string = '') {
$matches = array();
if ($string) {
$result = db_select('node', 'n')
->fields('n', array(
'title',
'nid',
))
->condition('title', db_like($string) . '%', 'LIKE')
->condition('n.type', 'unit_description')
->range(0, 10)
->execute();
foreach ($result as $description) {
$matches[$description->title . ':' . $description->nid] = check_plain($description->title);
}
}
drupal_json_output($matches);
}
function rooms_unit_menu_local_tasks_alter(&$data, $router_item, $root_path) {
if ($root_path == 'admin/rooms/units') {
$item = menu_get_item('admin/rooms/units/add');
if ($item['access']) {
$data['actions']['output'][] = array(
'#theme' => 'menu_local_action',
'#link' => $item,
);
}
}
}
function rooms_unit_form_rooms_unit_type_operation_form_alter(&$form, &$form_state, $form_id) {
if ($form_state['op'] == 'delete') {
$unit_type = $form_state['rooms_unit_type'];
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'rooms_unit')
->propertyCondition('type', $unit_type->type);
$units = $query
->execute();
if (isset($units['rooms_unit']) && count($units['rooms_unit'])) {
form_set_error('confirm', t('This unit type has associated units. Please delete all units before attempting to delete this unit type.'));
unset($form['description']);
unset($form['actions']);
unset($form['confirm']);
}
}
}
function rooms_unit_module_implements_alter(&$implementations, $hook) {
if ($hook == 'form_alter' && isset($implementations['rooms_unit'])) {
$group = $implementations['rooms_unit'];
unset($implementations['rooms_unit']);
$implementations['rooms_unit'] = $group;
}
}
function rooms_unit_form_rooms_unit_edit_form_alter(&$form, &$form_state) {
if (module_exists('entity_translation')) {
if (isset($form['translation'])) {
$form['translation']['#group'] = 'additional_settings';
}
}
}
function rooms_unit_state_options() {
return array(
ROOMS_NOT_AVAILABLE => t('Unavailable'),
ROOMS_AVAILABLE => t('Available'),
ROOMS_ON_REQUEST => t('On Request'),
ROOMS_ANON_BOOKED => t('Anonymous Booking'),
);
}
function rooms_unit_get_unit_options($unit) {
$options =& drupal_static(__FUNCTION__);
if (isset($options['units'][$unit->unit_id])) {
return $options['units'][$unit->unit_id];
}
if (isset($options['unit_types'][$unit->type])) {
$unit_type_options = $options['unit_types'][$unit->type];
}
else {
$unit_type = rooms_unit_type_load($unit->type);
$unit_type_options = is_array(field_get_items('rooms_unit_type', $unit_type, 'rooms_booking_unit_options')) ? field_get_items('rooms_unit_type', $unit_type, 'rooms_booking_unit_options') : array();
$options['unit_types'][$unit->type] = $unit_type_options;
}
$unit_options = is_array(field_get_items('rooms_unit', $unit, 'rooms_booking_unit_options')) ? field_get_items('rooms_unit', $unit, 'rooms_booking_unit_options') : array();
$options['units'][$unit->unit_id] = array_merge($unit_type_options, $unit_options);
return $options['units'][$unit->unit_id];
}
class RoomsUnit extends Entity {
public function __construct($values = array()) {
parent::__construct($values, 'rooms_unit');
}
protected function defaultLabel() {
return $this->name;
}
protected function defaultUri() {
return array(
'path' => 'unit/' . $this->unit_id,
);
}
}
class RoomsUnitType extends Entity {
public $type;
public $label;
public function __construct($values = array()) {
parent::__construct($values, 'rooms_unit_type');
}
}
class RoomsUnitController extends EntityAPIController {
public function __construct($entityType) {
parent::__construct($entityType);
}
public function create(array $values = array()) {
$unit_type = rooms_unit_type_load($values['type'], TRUE);
$values += array(
'unit_id' => '',
'is_new' => TRUE,
'title' => '',
'created' => '',
'changed' => '',
'base_price' => isset($unit_type->data['base_price']) ? $unit_type->data['base_price'] : 0,
'min_sleeps' => isset($unit_type->data['min_sleeps']) ? $unit_type->data['min_sleeps'] : 0,
'max_sleeps' => isset($unit_type->data['max_sleeps']) ? $unit_type->data['max_sleeps'] : 0,
'min_children' => isset($unit_type->data['min_children']) ? $unit_type->data['min_children'] : 0,
'max_children' => isset($unit_type->data['max_children']) ? $unit_type->data['max_children'] : 0,
'data' => array(
'cot_surcharge' => isset($unit_type->data['cot_surcharge']) ? $unit_type->data['cot_surcharge'] : 0,
),
);
$unit = parent::create($values);
return $unit;
}
public function buildContent($entity, $view_mode = 'full', $langcode = NULL, $content = array()) {
$content = parent::buildContent($entity, $view_mode, $langcode, $content);
$content['state'] = array(
'#markup' => t('State') . ': ' . $entity->bookable . '<br/>',
);
$content['type'] = array(
'#markup' => t('Type') . ': ' . $entity->type . '<br/>',
);
$content['sleeps'] = array(
'#markup' => t('Sleeps') . ': ' . $entity->max_sleeps . '<br/>',
);
if (isset($entity->data['bed_arrangement'])) {
$content['bed_arrangement'] = array(
'#markup' => t('Double beds') . ': ' . $entity->data['bed_arrangement']['doubles'] . '<br/>' . t('Single beds') . ': ' . $entity->data['bed_arrangement']['singles'] . '<br/>',
);
}
return $content;
}
}
class RoomsUnitTypeController extends EntityAPIControllerExportable {
public function __construct($entityType) {
parent::__construct($entityType);
}
public function create(array $values = array()) {
$values += array(
'id' => '',
'is_new' => TRUE,
'data' => '',
);
$unit_type = parent::create($values);
return $unit_type;
}
}