View source
<?php
class CaseTrackerCaseType extends Entity {
public $type;
public $label;
public function __construct($values = array()) {
parent::__construct($values, 'casetracker_case_type');
}
function isLocked() {
return isset($this->status) && empty($this->is_new) && ($this->status & ENTITY_IN_CODE || $this->status & ENTITY_FIXED);
}
}
class CaseTrackerCaseTypeController extends EntityAPIControllerExportable {
public function __construct($entityType) {
parent::__construct($entityType);
}
public function create(array $values = array()) {
$values += array(
'id' => '',
'is_new' => TRUE,
);
$case_type = parent::create($values);
return $case_type;
}
}
class CaseTrackerCaseTypeUIController extends EntityDefaultUIController {
public function hook_menu() {
$items = parent::hook_menu();
$items[$this->path]['type'] = MENU_LOCAL_TASK;
$items[$this->path]['description'] = 'Manage case entity types, including adding
and removing fields and the display of fields.';
return $items;
}
}
function casetracker_case_type_access($op, $type = NULL, $account = NULL) {
return user_access('administer casetracker_case types', $account);
}
function casetracker_case_get_types($type_name = NULL) {
$types = entity_load_multiple_by_name('casetracker_case_type', isset($type_name) ? array(
$type_name,
) : FALSE);
return isset($type_name) ? reset($types) : $types;
}
function casetracker_case_type_load($type) {
return casetracker_case_get_types($type);
}
function casetracker_case_type_form($form, &$form_state, $case_type, $op = 'edit') {
if ($op == 'clone') {
$case_type->label .= ' (cloned)';
$case_type->type = '';
}
$form['label'] = array(
'#title' => t('Label'),
'#type' => 'textfield',
'#default_value' => $case_type->label,
'#description' => t('The human-readable name of this case type.'),
'#required' => TRUE,
'#size' => 30,
);
$form['type'] = array(
'#type' => 'machine_name',
'#default_value' => isset($case_type->type) ? $case_type->type : '',
'#maxlength' => 32,
'#machine_name' => array(
'exists' => 'casetracker_case_get_types',
'source' => array(
'label',
),
),
'#description' => t('A unique machine-readable name for this case type. It must only contain lowercase letters, numbers, and underscores.'),
);
$default_fields = array(
t('Project reference: an Entity Reference type field to reference Case Tracker Projects.'),
t('Description: a Long Text field to describe more about the cases.'),
t('Status: a Text field with some predefined options.'),
t('Priority: a Text field with some predefined options.'),
);
$form['default_fields'] = array(
'#type' => 'markup',
'#markup' => t('This case type will have the following default fields:') . theme('item_list', array(
'items' => $default_fields,
)),
);
$form['additional_fields'] = array(
'#type' => 'checkboxes',
'#title' => t('Do you want to put some additional fields to this Case Type?'),
'#description' => t('You will be able to create other fields any time through the Manage Fields page for this case type.'),
'#options' => array(
'field_casetracker_case_due_date' => t('Due date: a Date type field to be used on tasks that needs to predefine a due date.'),
'field_casetracker_assigned_to' => t('Assigned To: an Entity Reference type field to reference site Users as the responsible for tasks.'),
),
);
$form['actions'] = array(
'#type' => 'actions',
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save case type'),
'#weight' => 40,
'#suffix' => l(t('Cancel'), 'admin/structure/casetracker/case-types'),
);
return $form;
}
function casetracker_case_type_form_submit(&$form, &$form_state) {
$case_type = entity_ui_form_submit_build_entity($form, $form_state);
if ($form_state['op'] == 'add') {
$additional_fields = array();
foreach ($form_state['values']['additional_fields'] as $field_name => $additional_field) {
if ($additional_field) {
$additional_fields[] = $additional_field;
}
}
casetracker_case_type_add_default_fields($case_type, $additional_fields);
drupal_set_message(t('Case type "@title" was created successfully', array(
'@title' => $case_type->label,
)));
}
else {
drupal_set_message(t('Case type "@title" was edited successfully', array(
'@title' => $case_type->label,
)));
}
$case_type
->save();
$form_state['redirect'] = 'admin/structure/casetracker/case-types';
}
function casetracker_case_type_form_submit_delete(&$form, &$form_state) {
$form_state['redirect'] = 'admin/structure/casetracker/case-types/manage/' . $form_state['casetracker_case_type']->type . '/delete';
}
function casetracker_case_type_add_default_fields($case_type, $additional_fields) {
$field_names = array(
'field_casetracker_project_ref',
'field_casetracker_description',
'field_casetracker_case_status',
'field_casetracker_case_priority',
);
if (!empty($additional_fields)) {
$field_names = array_merge($field_names, $additional_fields);
}
$fields = casetracker_basic_fields_definitions('casetracker_case', $case_type->type, $field_names);
_casetracker_process_fields_for_new_bundle('casetracker_case', $case_type->type, $fields);
}