You are here

casetracker_case_type.inc in Case Tracker 7.2

CaseTrackerCase type editing UI.

File

casetracker_case_type.inc
View source
<?php

/**
 * @file
 * CaseTrackerCase type editing UI.
 */

/**
 * The class used for case type entities
 */
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);
  }

}

/**
 * The Controller for Case entities
 */
class CaseTrackerCaseTypeController extends EntityAPIControllerExportable {
  public function __construct($entityType) {
    parent::__construct($entityType);
  }

  /**
   *
   * @param $type
   *   The machine-readable type of the case.
   *
   * @return
   *   A case type object with all default fields initialized.
   */
  public function create(array $values = array()) {

    // Add values that are specific to our Case
    $values += array(
      'id' => '',
      'is_new' => TRUE,
    );
    $case_type = parent::create($values);
    return $case_type;
  }

}

/**
 * UI controller.
 */
class CaseTrackerCaseTypeUIController extends EntityDefaultUIController {

  /**
   * Overrides hook_menu() defaults.
   */
  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;
  }

}

/**
 * Access callback for the entity API.
 */
function casetracker_case_type_access($op, $type = NULL, $account = NULL) {
  return user_access('administer casetracker_case types', $account);
}

/**
 * Gets an array of all casetracker_case types, keyed by the type name.
 *
 * @param $type_name
 *   If set, the type with the given name is returned.
 * @return CaseTrackerCaseType[]
 *   Depending whether $type isset, an array of casetracker_case types or
 *   a single one.
 */
function casetracker_case_get_types($type_name = NULL) {

  // entity_load will get the Entity controller for our casetracker_case entity
  // and call the load function of that object - we are loading entities by name here.
  $types = entity_load_multiple_by_name('casetracker_case_type', isset($type_name) ? array(
    $type_name,
  ) : FALSE);
  return isset($type_name) ? reset($types) : $types;
}

/**
 * Menu argument loader; Load a casetracker_case type by string.
 *
 * @param $type
 *   The machine-readable name of a casetracker_case type to load.
 * @return
 *   A casetracker_case type array or FALSE if $type does not exist.
 */
function casetracker_case_type_load($type) {
  return casetracker_case_get_types($type);
}

/**
 * Saves a casetracker_case type to the db.
 */

// function casetracker_case_type_save(CaseTrackerCaseType $type) {
//   $type->save();
// }

/**
 * Deletes a casetracker_case type from the db.
 */

// function casetracker_case_type_delete(CaseTrackerCaseType $type) {
//   $type->delete();
// }

/**
 * Generates the case type editing form.
 */
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,
  );

  // Machine-readable type name.
  $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;
}

/**
 * Form API submit callback for the type form.
 */
function casetracker_case_type_form_submit(&$form, &$form_state) {
  $case_type = entity_ui_form_submit_build_entity($form, $form_state);

  // If we create new case type we need to add default fields to it.
  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';
}

/**
 * Form API submit callback for the delete button.
 */
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';
}

/**
 * Add default fields to newly created case type.
 * P.S: needs to be in this file, because casetracker.install doesn't found
 * the casetracker.fields file to create and remove fields
 */
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);
}

Functions

Namesort descending Description
casetracker_case_get_types Gets an array of all casetracker_case types, keyed by the type name.
casetracker_case_type_access Access callback for the entity API.
casetracker_case_type_add_default_fields Add default fields to newly created case type. P.S: needs to be in this file, because casetracker.install doesn't found the casetracker.fields file to create and remove fields
casetracker_case_type_form Generates the case type editing form.
casetracker_case_type_form_submit Form API submit callback for the type form.
casetracker_case_type_form_submit_delete Form API submit callback for the delete button.
casetracker_case_type_load Menu argument loader; Load a casetracker_case type by string.

Classes

Namesort descending Description
CaseTrackerCaseType The class used for case type entities
CaseTrackerCaseTypeController The Controller for Case entities
CaseTrackerCaseTypeUIController UI controller.