Initial commit of Farm Plan module: provides a farm_plan asset type. Copied and modified from farm_asset module.

This commit is contained in:
Michael Stenta 2017-09-06 16:46:29 -04:00
parent dc8e0dd7c9
commit 57ab97c736
10 changed files with 1526 additions and 0 deletions

View File

@ -0,0 +1,147 @@
<?php
/**
* @file
* Farm plan admin pages.
*/
/**
* Generates the farm plan type editing form.
*
* @param array $form
* The form array.
* @param array $form_state
* The form state array.
* @param string $farm_plan_type
* The farm plan type.
* @param string $op
* The operation being performed.
*
* @return array
* Returns a form array.
*/
function farm_plan_type_form(array $form, array &$form_state, $farm_plan_type, $op = 'edit') {
if ($op == 'clone') {
$farm_plan_type->label .= ' (cloned)';
$farm_plan_type->type = '';
}
$form['label'] = array(
'#title' => t('Label'),
'#type' => 'textfield',
'#default_value' => $farm_plan_type->label,
'#description' => t('The human-readable name of this plan type.'),
'#required' => TRUE,
'#size' => 30,
);
// Machine-readable type name.
$form['type'] = array(
'#type' => 'machine_name',
'#default_value' => !empty($farm_plan_type->type) ? $farm_plan_type->type : '',
'#maxlength' => 32,
'#disabled' => $farm_plan_type->isLocked() && $op != 'clone',
'#machine_name' => array(
'exists' => 'farm_plan_types',
'source' => array('label'),
),
'#description' => t('A unique machine-readable name for this plan type. It must only contain lowercase letters, numbers, and underscores.'),
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save plan type'),
'#weight' => 40,
);
if (!$farm_plan_type->isLocked() && $op != 'add' && $op != 'clone') {
$form['actions']['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete plan type'),
'#weight' => 45,
'#limit_validation_errors' => array(),
'#submit' => array('farm_plan_type_form_submit_delete'),
);
}
return $form;
}
/**
* Submit handler for creating/editing plan types.
*
* @param array $form
* The form array.
* @param array $form_state
* The form state array.
*/
function farm_plan_type_form_submit(array &$form, array &$form_state) {
$farm_plan_type = entity_ui_form_submit_build_entity($form, $form_state);
// Save and go back.
farm_plan_type_save($farm_plan_type);
// Redirect user back to list of plan types.
$form_state['redirect'] = 'admin/config/farm/plan-types';
}
/**
* Submit handler for deleting plan types.
*
* @param array $form
* The form array.
* @param array $form_state
* The form state array.
*/
function farm_plan_type_form_submit_delete(array &$form, array &$form_state) {
$form_state['redirect'] = 'admin/config/farm/plan-types/' . $form_state['farm_plan_type']->type . '/delete';
}
/**
* Plan type delete form.
*
* @param array $form
* The form array.
* @param array $form_state
* The form state array.
* @param string $farm_plan_type
* The farm plan type.
*
* @return array
* Returns a form array.
*/
function farm_plan_type_form_delete_confirm(array $form, array &$form_state, $farm_plan_type) {
$form['farm_plan_type'] = array(
'#type' => 'value',
'#value' => $farm_plan_type,
);
// Always provide entity id in the same form key as in the entity edit form.
$form['farm_plan_type_id'] = array(
'#type' => 'value',
'#value' => entity_id('farm_plan_type', $farm_plan_type));
return confirm_form($form,
t('Are you sure you want to delete plan type %title?', array('%title' => entity_label('farm_plan_type', $farm_plan_type))),
'farm/plan/' . entity_id('farm_plan_type', $farm_plan_type),
t('This action cannot be undone.'),
t('Delete'),
t('Cancel')
);
}
/**
* Plan type delete form submit handler.
*
* @param array $form
* The form array.
* @param array $form_state
* The form state array.
*/
function farm_plan_type_form_delete_confirm_submit(array $form, array &$form_state) {
$farm_plan_type = $form_state['values']['farm_plan_type'];
farm_plan_type_delete($farm_plan_type);
watchdog('farm_plan', '@type: deleted %title.', array('@type' => $farm_plan_type->type, '%title' => $farm_plan_type->label));
drupal_set_message(t('@type %title has been deleted.', array('@type' => $farm_plan_type->type, '%title' => $farm_plan_type->label)));
$form_state['redirect'] = 'admin/config/farm/plan-types';
}

View File

@ -0,0 +1,120 @@
<?php
/**
* @file
* Farm plan classes.
*/
/**
* Farm plan class.
*/
class FarmPlan extends Entity {
/**
* {@inheritdoc}
*/
protected function defaultLabel() {
return $this->name;
}
/**
* {@inheritdoc}
*/
protected function defaultUri() {
return array('path' => 'farm/plan/' . $this->identifier());
}
}
/**
* Farm plan controller class.
*/
class FarmPlanController extends EntityAPIController {
/**
* {@inheritdoc}
*/
public function create(array $values = array()) {
global $user;
$values += array(
'id' => NULL,
'name' => '',
'type' => '',
'uid' => $user->uid,
'created' => REQUEST_TIME,
'changed' => REQUEST_TIME,
'active' => TRUE,
);
return parent::create($values);
}
/**
* {@inheritdoc}
*/
public function save($entity, DatabaseTransaction $transaction = NULL) {
// Set the changed timestamp.
$entity->changed = REQUEST_TIME;
return parent::save($entity, $transaction);
}
}
/**
* Farm plan type class.
*/
class FarmPlanType extends Entity {
public $type;
public $label;
public $weight = 0;
/**
* {@inheritdoc}
*/
public function __construct($values = array()) {
parent::__construct($values, 'farm_plan_type');
}
/**
* {@inheritdoc}
*/
public function isLocked() {
return isset($this->status) && empty($this->is_new) && (($this->status & ENTITY_IN_CODE) || ($this->status & ENTITY_FIXED));
}
}
/**
* Farm plan type controller class.
*/
class FarmPlanTypeController extends EntityAPIControllerExportable {
/**
* {@inheritdoc}
*/
public function create(array $values = array()) {
$values += array(
'id' => NULL,
'type' => '',
'label' => '',
);
return parent::create($values);
}
}
/**
* UI controller for farm plan types.
*/
class FarmPlanTypeUIController extends EntityDefaultUIController {
/**
* Overrides hook_menu() defaults.
*/
public function hook_menu() {
$items = parent::hook_menu();
$items[$this->path]['description'] = 'Manage farm plan types.';
return $items;
}
}

View File

@ -0,0 +1,12 @@
<?php
/**
* @file
* farm_plan.features.inc
*/
/**
* Implements hook_views_api().
*/
function farm_plan_views_api($module = NULL, $api = NULL) {
return array("api" => "3.0");
}

View File

@ -0,0 +1,14 @@
name = Farm Plan
description = A farm plan entity type.
core = 7.x
package = farmOS (beta)
dependencies[] = ctools
dependencies[] = entity
dependencies[] = features
dependencies[] = views
dependencies[] = views_bulk_operations
features[ctools][] = views:views_default:3.0
features[features_api][] = api:2
features[views_view][] = farm_plan
files[] = farm_plan.class.inc
files[] = views/plugins/farm_plan_plugin_argument_validate_farm_plan.inc

View File

@ -0,0 +1,117 @@
<?php
/**
* @file
* Farm plan install.
*/
/**
* Implements hook_schema().
*/
function farm_plan_schema() {
$schema['farm_plan'] = array(
'description' => 'Farm plans',
'fields' => array(
'id' => array(
'description' => 'Plan ID',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'name' => array(
'description' => 'Plan name',
'type' => 'varchar',
'length' => '255',
'not null' => FALSE,
),
'type' => array(
'description' => 'Plan type',
'type' => 'varchar',
'length' => '255',
'not null' => FALSE,
),
'uid' => array(
'description' => 'Plan owner',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'created' => array(
'description' => 'Timestamp when the plan was created',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'changed' => array(
'description' => 'Timestamp when the plan was last modified',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'active' => array(
'description' => 'Boolean indicating whether the plan is active.',
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
'default' => 1,
),
),
'primary key' => array('id'),
'indexes' => array(
'name' => array('name'),
'type_index' => array('type'),
'uid' => array('uid'),
'created' => array('created'),
'modified' => array('changed'),
'active' => array('active'),
),
);
$schema['farm_plan_type'] = array(
'description' => 'Stores information about all defined plan types.',
'fields' => array(
'id' => array(
'description' => 'Primary Key: Unique plan type ID.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'type' => array(
'description' => 'The machine-readable name of this plan type.',
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
),
'label' => array(
'description' => 'The human-readable name of this plan type.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
// The following fields are required to make types exportable via
// Entity API and Ctools.
'status' => array(
'type' => 'int',
'not null' => TRUE,
// Set the default to ENTITY_CUSTOM without using the constant as it is
// not safe to use it at this point.
'default' => 0x01,
'size' => 'tiny',
'description' => 'The exportable status of the entity.',
),
'module' => array(
'description' => 'The name of the providing module if the entity has been defined in code.',
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
),
),
'primary key' => array('id'),
'unique keys' => array(
'type' => array('type'),
),
);
return $schema;
}

View File

@ -0,0 +1,594 @@
<?php
/**
* @file
* Farm plan - A farm plan entity type.
*/
// Include Features code.
include_once 'farm_plan.features.inc';
/****************************************************************
* Drupal hooks
* **************************************************************/
/**
* Implements hook_permission().
*/
function farm_plan_permission() {
$perms = array(
'administer farm_plan module' => array(
'title' => t('Administer farm plan module'),
'description' => t('Gives full access to everything in the farm plan module.'),
'restrict access' => TRUE,
),
'administer farm_plan types' => array(
'title' => t('Administer farm plan types'),
'restrict access' => TRUE,
),
'view farm plans' => array(
'title' => t('View farm plans'),
'description' => t('Allows users to view the full list of farm plans.'),
),
);
// Add permissions for each farm_plan type.
foreach (farm_plan_types() as $farm_plan_type) {
$type = $farm_plan_type->type;
$ops = array('view', 'edit', 'delete');
$scopes = array('any', 'own');
$perms += array(
"create $type farm plans" => array(
'title' => t('Create new %type_name farm plans', array('%type_name' => $farm_plan_type->label)),
),
);
foreach ($ops as $op) {
foreach ($scopes as $scope) {
$perms += array(
"$op $scope $type farm plans" => array(
'title' => drupal_ucfirst($op) . ' ' . $scope . ' ' . t('%type_name farm plans', array('%type_name' => $farm_plan_type->label)),
),
);
}
}
}
return $perms;
}
/**
* Implements hook_menu().
*/
function farm_plan_menu() {
$items = array();
$items['farm/plan/add'] = array(
'title' => 'Add plan',
'page callback' => 'farm_plan_add_types_page',
'access callback' => 'farm_plan_add_access',
'file' => 'farm_plan.pages.inc',
);
foreach (farm_plan_types() as $type => $info) {
$items['farm/plan/add/' . $type] = array(
'title' => 'Add ' . $info->label,
'page callback' => 'farm_plan_add',
'page arguments' => array(3),
'access callback' => 'farm_plan_access',
'access arguments' => array('create', 3),
'file' => 'farm_plan.pages.inc',
);
}
$farm_plan_uri = 'farm/plan/%farm_plan';
$farm_plan_uri_argument_position = 2;
$items[$farm_plan_uri] = array(
'title callback' => 'entity_label',
'title arguments' => array('farm_plan', $farm_plan_uri_argument_position),
'page callback' => 'farm_plan_view',
'page arguments' => array($farm_plan_uri_argument_position),
'access callback' => 'farm_plan_access',
'access arguments' => array('view', $farm_plan_uri_argument_position),
'file' => 'farm_plan.pages.inc',
);
$items[$farm_plan_uri . '/view'] = array(
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items[$farm_plan_uri . '/delete'] = array(
'title' => 'Delete plan',
'title callback' => 'farm_plan_label',
'title arguments' => array($farm_plan_uri_argument_position),
'page callback' => 'drupal_get_form',
'page arguments' => array('farm_plan_delete_form', $farm_plan_uri_argument_position),
'access callback' => 'farm_plan_access',
'access arguments' => array('update', $farm_plan_uri_argument_position),
'file' => 'farm_plan.pages.inc',
);
$items[$farm_plan_uri . '/edit'] = array(
'title' => 'Edit',
'page callback' => 'drupal_get_form',
'page arguments' => array('farm_plan_form', $farm_plan_uri_argument_position),
'access callback' => 'farm_plan_access',
'access arguments' => array('update', $farm_plan_uri_argument_position),
'file' => 'farm_plan.pages.inc',
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
);
// Plan type delete form.
$items['admin/config/farm/plan-types/%farm_plan_type/delete'] = array(
'title' => 'Delete',
'page callback' => 'drupal_get_form',
'page arguments' => array('farm_plan_type_form_delete_confirm', 4),
'access arguments' => array('administer farm_plan types'),
'weight' => 1,
'type' => MENU_NORMAL_ITEM,
'file' => 'farm_plan.admin.inc',
);
return $items;
}
/**
* Implements hook_entity_info().
*/
function farm_plan_entity_info() {
$return = array(
'farm_plan' => array(
'label' => t('Farm plan'),
'entity class' => 'FarmPlan',
'controller class' => 'FarmPlanController',
'base table' => 'farm_plan',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'id',
'bundle' => 'type',
'label' => 'name',
),
'bundle keys' => array(
'bundle' => 'type',
),
'bundles' => array(),
'load hook' => 'farm_plan_load',
'view modes' => array(
'full' => array(
'label' => t('Default'),
'custom settings' => FALSE,
),
),
'label callback' => 'entity_class_label',
'uri callback' => 'entity_class_uri',
'module' => 'farm_plan',
'access callback' => 'farm_plan_access',
),
);
$return['farm_plan_type'] = array(
'label' => t('Farm plan type'),
'entity class' => 'FarmPlanType',
'controller class' => 'FarmPlanTypeController',
'base table' => 'farm_plan_type',
'fieldable' => FALSE,
'bundle of' => 'farm_plan',
'exportable' => TRUE,
'entity keys' => array(
'id' => 'id',
'name' => 'type',
'label' => 'label',
),
'module' => 'farm_plan',
// Enable the entity API admin UI.
'admin ui' => array(
'path' => 'admin/config/farm/plan-types',
'file' => 'farm_plan.admin.inc',
'controller class' => 'FarmPlanTypeUIController',
),
'access callback' => 'farm_plan_type_access',
);
return $return;
}
/**
* Implements hook_entity_info_alter().
*/
function farm_plan_entity_info_alter(&$entity_info) {
foreach (farm_plan_types() as $type => $info) {
$entity_info['farm_plan']['bundles'][$type] = array(
'label' => $info->label,
'admin' => array(
'path' => 'admin/config/farm/plan-types/manage/%farm_plan_type',
'real path' => 'admin/config/farm/plan-types/manage/' . $type,
'bundle argument' => 5,
),
);
}
}
/**
* Implements hook_entity_property_info_alter().
*/
function farm_plan_entity_property_info_alter(&$info) {
$properties = &$info['farm_plan']['properties'];
$properties['name'] = array(
'label' => t('Name'),
'description' => t('The name of the plan.'),
'setter callback' => 'entity_property_verbatim_set',
'schema field' => 'name',
);
$properties['type'] = array(
'label' => t('Farm plan type'),
'type' => 'token',
'description' => t('The farm plan type.'),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'administer farm_plan module',
'options list' => 'farm_plan_type_get_names',
'required' => TRUE,
'schema field' => 'type',
);
$properties['uid'] = array(
'label' => t('Owner'),
'type' => 'user',
'description' => t('The owner of the plan.'),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'administer farm_plan module',
'required' => TRUE,
'schema field' => 'uid',
);
$properties['created'] = array(
'label' => t('Created'),
'type' => 'date',
'description' => t('The timestamp when the plan was created.'),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'administer farm_plan module',
'required' => TRUE,
'schema field' => 'created',
);
$properties['changed'] = array(
'label' => t('Changed'),
'type' => 'date',
'description' => t('The timestamp when the plan was last modified.'),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'administer farm_plan module',
'required' => TRUE,
'schema field' => 'changed',
);
$properties['active'] = array(
'label' => t('Active'),
'description' => t('Whether the plan is active.'),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'administer farm_plan module',
'schema field' => 'active',
'type' => 'boolean',
);
}
/**
* Implements hook_field_extra_fields().
*/
function farm_plan_field_extra_fields() {
$farm_plan_types = farm_plan_types();
$extra_fields = array(
'farm_plan' => array(),
);
foreach ($farm_plan_types as $type) {
$extra_fields['farm_plan'][$type->type] = array(
'form' => array(
// Add plan name field to field UI.
'name' => array(
'label' => t('Name'),
'description' => t('The name of the plan.'),
'weight' => -10,
),
),
);
}
return $extra_fields;
}
/**
* Implements hook_entity_view().
*/
function farm_plan_entity_view($entity, $type, $view_mode, $langcode) {
// If the entity is not a farm_plan, bail.
if ($type != 'farm_plan') {
return;
}
// Add the plan's "active" status.
if ($entity->active) {
$status = 'Yes';
}
else {
$status = 'No';
drupal_set_message(t('This plan is no longer active. Inactive plans should be considered "archived" and should not be edited or deleted unless they contain information that is incorrect.'), 'warning');
}
$entity->content['active'] = array(
'#markup' => '<div><strong>Active plan:</strong> ' . $status . '</div>',
'#weight' => -100,
);
}
/***************************************************************
* Access functions
* *************************************************************/
/**
* Access callback for plan entities.
*
* @param string $op
* The operation being performed. One of 'view', 'update', 'create', 'delete'.
* @param FarmPlan|string $farm_plan
* Optionally a specific plan entity to check.
* @param object $account
* The user to check for. Leave it to NULL to check for the global user.
*
* @return bool
* Whether access is allowed or not.
*/
function farm_plan_access($op, $farm_plan = NULL, $account = NULL) {
$rights = &drupal_static(__FUNCTION__, array());
// If $op is not one of the supported ones, deny access.
if (!in_array($op, array('create', 'view', 'update', 'delete'), TRUE)) {
return FALSE;
}
// If no user object is supplied, the access check is for the current user.
if (empty($account)) {
global $user;
$account = $user;
}
// If no plan is provided, check for access to all plans.
if (empty($farm_plan)) {
return user_access('view farm plans', $account);
}
// $farm_plan may be either an object or a plan type. Since plan types
// cannot be an integer, use either id or type as the static cache id.
$cid = is_object($farm_plan) ? $farm_plan->id : $farm_plan;
// If we've already checked access for this plan, user and op, return from
// cache.
if (isset($rights[$account->uid][$cid][$op])) {
return $rights[$account->uid][$cid][$op];
}
// If the user has 'administer farm_plan module' permission, grant them
// access.
if (user_access('administer farm_plan module', $account)) {
$rights[$account->uid][$cid][$op] = TRUE;
return TRUE;
}
// Check access to the plan based on it's type.
$type = is_string($farm_plan) ? $farm_plan : $farm_plan->type;
$farm_plan_types = farm_plan_types();
$type_names = array();
foreach ($farm_plan_types as $name => $farm_plan_type) {
$type_names[] = $name;
}
if (in_array($type, $type_names)) {
if ($op == 'create' && user_access('create ' . $type . ' farm plans', $account)) {
$rights[$account->uid][$cid][$op] = TRUE;
return TRUE;
}
if ($op == 'view') {
if (user_access('view any ' . $type . ' farm plans', $account) || (user_access('view own ' . $type . ' farm plans', $account) && ($account->uid == $farm_plan->uid))) {
$rights[$account->uid][$cid][$op] = TRUE;
return TRUE;
}
}
if ($op == 'update') {
if (user_access('edit any ' . $type . ' farm plans', $account) || (user_access('edit own ' . $type . ' farm plans', $account) && ($account->uid == $farm_plan->uid))) {
$rights[$account->uid][$cid][$op] = TRUE;
return TRUE;
}
}
if ($op == 'delete') {
if (user_access('delete any ' . $type . ' farm plans', $account) || (user_access('delete own ' . $type . ' farm plans', $account) && ($account->uid == $farm_plan->uid))) {
$rights[$account->uid][$cid][$op] = TRUE;
return TRUE;
}
}
}
// If all else fails, deny access.
return FALSE;
}
/**
* Access callback: Checks whether the user has permission to add a plan.
*
* @param object|null $account
* The user account.
*
* @return bool
* TRUE if the user has add permission, otherwise FALSE.
*/
function farm_plan_add_access($account = NULL) {
// If no user object is supplied, the access check is for the current user.
if (empty($account)) {
global $user;
$account = $user;
}
// Check each of the plan types to see if the user has access.
$types = farm_plan_types();
foreach ($types as $type) {
if (farm_plan_access('create', $type->type, $account)) {
return TRUE;
}
}
// If all else fails, deny access.
return FALSE;
}
/**
* Access callback for plan types.
*
* @param string $op
* The operation being performed.
* @param FarmPlanType $farm_plan_type
* The farm plan entity.
*
* @return bool
* Returns true if the user has access.
*/
function farm_plan_type_access($op, FarmPlanType $farm_plan_type = NULL) {
return user_access('administer farm_plan types');
}
/***************************************************************
* Farm plan API functions
* *************************************************************/
/**
* Load a plan.
*
* @param int $id
* The plan id.
* @param bool $reset
* Whether or not to reset the entity cache.
*
* @return FarmPlan
* Returns a farm plan object.
*/
function farm_plan_load($id, $reset = FALSE) {
$farm_plans = farm_plan_load_multiple(array($id), array(), $reset);
return reset($farm_plans);
}
/**
* Load multiple plans based on certain conditions.
*
* @param array $ids
* An array of farm plan ids.
* @param array $conditions
* An array of entity load conditions.
* @param bool $reset
* Whether or not to reset the entity cache.
*
* @return array
* Returns an array of farm plans.
*/
function farm_plan_load_multiple($ids = array(), $conditions = array(), $reset = FALSE) {
return entity_load('farm_plan', $ids, $conditions, $reset);
}
/**
* Save plan.
*
* @param FarmPlan $farm_plan
* The farm plan entity.
*/
function farm_plan_save(FarmPlan $farm_plan) {
entity_save('farm_plan', $farm_plan);
}
/**
* Delete single plan.
*
* @param FarmPlan $farm_plan
* The farm plan entity.
*/
function farm_plan_delete(FarmPlan $farm_plan) {
entity_delete('farm_plan', entity_id('farm_plan', $farm_plan));
}
/**
* Delete multiple plans.
*
* @param array $farm_plan_ids
* An array of farm plan ids.
*/
function farm_plan_delete_multiple(array $farm_plan_ids) {
entity_delete_multiple('farm_plan', $farm_plan_ids);
}
/***************************************************************
* Farm plan type API functions
* *************************************************************/
/**
* Load plan type.
*
* @param string $farm_plan_type
* The farm plan type.
*
* @return FarmPlanType
* Returns a farm plan type entity.
*/
function farm_plan_type_load($farm_plan_type) {
return farm_plan_types($farm_plan_type);
}
/**
* List of plan types.
*
* @param string $type_name
* The farm plan type name.
*
* @return FarmPlanType|array
* Returns either a single type, or an array of types.
*/
function farm_plan_types($type_name = NULL) {
$types = entity_load_multiple_by_name('farm_plan_type', isset($type_name) ? array($type_name) : FALSE);
return isset($type_name) ? reset($types) : $types;
}
/**
* Save plan type entity.
*
* @param FarmPlanType $farm_plan_type
* The farm plan type entity.
*/
function farm_plan_type_save(FarmPlanType $farm_plan_type) {
entity_save('farm_plan_type', $farm_plan_type);
}
/**
* Delete single plan type.
*
* @param FarmPlanType $farm_plan_type
* The farm plan type entity.
*/
function farm_plan_type_delete(FarmPlanType $farm_plan_type) {
entity_delete('farm_plan_type', entity_id('farm_plan_type', $farm_plan_type));
}
/**
* Delete multiple plan types.
*
* @param array $farm_plan_type_ids
* An array of farm plan type ids.
*/
function farm_plan_type_delete_multiple(array $farm_plan_type_ids) {
entity_delete_multiple('farm_plan_type', $farm_plan_type_ids);
}
/**
* Get the names of all plan types.
*
* @return array
* Returns an array of plan type names, keyed by machine name.
*/
function farm_plan_type_get_names() {
$names = array();
$types = farm_plan_types();
foreach ($types as $type) {
$names[$type->type] = $type->label;
}
return $names;
}

View File

@ -0,0 +1,248 @@
<?php
/**
* @file
* Farm plan pages.
*/
/**
* Plan view callback.
*
* @param FarmPlan $farm_plan
* The farm plan entity.
*
* @return array
* Returns the entity render array.
*/
function farm_plan_view(FarmPlan $farm_plan) {
// Set the page title.
drupal_set_title(entity_label('farm_plan', $farm_plan));
// Build the plan's render array.
$build = entity_view('farm_plan', array(entity_id('farm_plan', $farm_plan) => $farm_plan), 'full');
// Return the render array.
return $build;
}
/**
* Page to select plan type to add new plan.
*/
function farm_plan_add_types_page() {
$items = array();
foreach (farm_plan_types() as $farm_plan_type_key => $farm_plan_type) {
if (farm_plan_access('create', $farm_plan_type)) {
$items[] = l(entity_label('farm_plan_type', $farm_plan_type), 'farm/plan/add/' . $farm_plan_type_key);
}
}
return array(
'list' => array(
'#theme' => 'item_list',
'#items' => $items,
'#title' => t('Select a type of plan to create.'),
),
);
}
/**
* Add new plan page callback.
*
* @param string $type
* The farm plan type.
*
* @return array
* Returns a form array.
*/
function farm_plan_add($type) {
$farm_plan_type = farm_plan_types($type);
$farm_plan = entity_create('farm_plan', array('type' => $type));
drupal_set_title(t('Add @name', array('@name' => entity_label('farm_plan_type', $farm_plan_type))));
$output = drupal_get_form('farm_plan_form', $farm_plan);
return $output;
}
/**
* Plan form.
*
* @param array $form
* The form array.
* @param array $form_state
* The form state array.
* @param FarmPlan $farm_plan
* The farm plan entity.
*
* @return array
* Returns a form array.
*/
function farm_plan_form(array $form, array &$form_state, FarmPlan $farm_plan) {
$form['farm_plan'] = array(
'#type' => 'value',
'#value' => $farm_plan,
);
// Load the plan type.
$farm_plan_type = farm_plan_type_load($farm_plan->type);
// Plan name.
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#description' => t('Give this %type a name.', array('%type' => $farm_plan_type->label)),
'#default_value' => $farm_plan->name,
'#required' => TRUE,
'#weight' => -100,
);
// Additional settings (vertical tabs at the bottom of the form).
$form['additional_settings'] = array(
'#type' => 'vertical_tabs',
'#weight' => 99,
);
// Plan active/inactive (new plans are active by default).
if (empty($farm_plan->id)) {
$farm_plan->active = TRUE;
}
$form['plan_status'] = array(
'#type' => 'fieldset',
'#title' => t('Plan status'),
'#description' => t('Mark this plan as active/inactive. Inactive plans will not show in most lists, but will be visible in archives.'),
'#collapsible' => TRUE,
'#group' => 'additional_settings'
);
$form['plan_status']['active'] = array(
'#type' => 'checkbox',
'#title' => t('Active'),
'#default_value' => $farm_plan->active,
);
// Plan user id.
$form['uid'] = array(
'#type' => 'value',
'#value' => $farm_plan->uid,
);
field_attach_form('farm_plan', $farm_plan, $form, $form_state);
$submit = array();
if (!empty($form['#submit'])) {
$submit += $form['#submit'];
}
$form['actions'] = array(
'#weight' => 100,
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#submit' => $submit + array('farm_plan_form_submit'),
);
// Show Delete button if allowed.
$farm_plan_id = entity_id('farm_plan', $farm_plan);
if (!empty($farm_plan_id) && farm_plan_access('delete', $farm_plan)) {
// Get the destination query parameter. If it is the current path, change
// to <front> (because the current path won't exist once the plan is
// deleted).
$destination = drupal_get_destination();
if ($destination['destination'] == current_path()) {
$destination['destination'] = '<front>';
}
$form['actions']['delete'] = array(
'#type' => 'markup',
'#markup' => l(t('Delete'), 'farm/plan/' . $farm_plan_id . '/delete', array('query' => $destination)),
);
}
return $form;
}
/**
* Plan validate handler.
*
* @param array $form
* The form array.
* @param array $form_state
* The form state array.
*/
function farm_plan_form_validate(array $form, array &$form_state) {
}
/**
* Plan submit handler.
*
* @param array $form
* The form array.
* @param array $form_state
* The form state array.
*/
function farm_plan_form_submit(array $form, array &$form_state) {
$farm_plan = $form_state['values']['farm_plan'];
entity_form_submit_build_entity('farm_plan', $farm_plan, $form, $form_state);
farm_plan_save($farm_plan);
$farm_plan_uri = entity_uri('farm_plan', $farm_plan);
$form_state['redirect'] = $farm_plan_uri['path'];
drupal_set_message(t('Plan saved: <a href="@uri">%title</a>', array('@uri' => base_path() . $farm_plan_uri['path'], '%title' => entity_label('farm_plan', $farm_plan))));
}
/**
* Delete confirmation form.
*
* @param array $form
* The form array.
* @param array $form_state
* The form state array.
* @param FarmPlan $farm_plan
* The farm plan entity.
*
* @return array
* Returns a form array.
*/
function farm_plan_delete_form(array $form, array &$form_state, FarmPlan $farm_plan) {
$form['farm_plan'] = array(
'#type' => 'value',
'#value' => $farm_plan,
);
// Always provide entity id in the same form key as in the entity edit form.
$form['farm_plan_type_id'] = array(
'#type' => 'value',
'#value' => entity_id('farm_plan', $farm_plan));
$farm_plan_uri = entity_uri('farm_plan', $farm_plan);
return confirm_form($form,
t('Are you sure you want to delete %title?', array('%title' => entity_label('farm_plan', $farm_plan))),
$farm_plan_uri['path'],
t('This action cannot be undone.'),
t('Delete'),
t('Cancel')
);
}
/**
* Delete form submit handler.
*
* @param array $form
* The form array.
* @param array $form_state
* The form state array.
*/
function farm_plan_delete_form_submit(array $form, array &$form_state) {
$farm_plan = $form_state['values']['farm_plan'];
farm_plan_delete($farm_plan);
drupal_set_message(t('%title was deleted.', array('%title' => entity_label('farm_plan', $farm_plan))));
$form_state['redirect'] = '<front>';
}

View File

@ -0,0 +1,21 @@
<?php
/**
* @file
* Farm plan views hooks.
*/
/**
* Implements hook_views_plugins().
*/
function farm_plan_views_plugins() {
return array(
'argument validator' => array(
'farm_plan' => array(
'title' => t('Farm plan'),
'handler' => 'farm_plan_plugin_argument_validate_farm_plan',
'path' => drupal_get_path('module', 'farm_plan') . '/views/plugins',
),
),
);
}

View File

@ -0,0 +1,191 @@
<?php
/**
* @file
* farm_plan.views_default.inc
*/
/**
* Implements hook_views_default_views().
*/
function farm_plan_views_default_views() {
$export = array();
$view = new view();
$view->name = 'farm_plan';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'farm_plan';
$view->human_name = 'Farm Plans';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'Plans';
$handler->display->display_options['use_ajax'] = TRUE;
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['access']['perm'] = 'view farm plans';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['exposed_form']['options']['reset_button'] = TRUE;
$handler->display->display_options['exposed_form']['options']['autosubmit'] = TRUE;
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '50';
$handler->display->display_options['style_plugin'] = 'table';
$handler->display->display_options['style_options']['columns'] = array(
'id' => 'id',
'name' => 'name',
'type' => 'type',
'active' => 'active',
);
$handler->display->display_options['style_options']['default'] = '-1';
$handler->display->display_options['style_options']['info'] = array(
'id' => array(
'sortable' => 0,
'default_sort_order' => 'asc',
'align' => '',
'separator' => '',
'empty_column' => 0,
),
'name' => array(
'sortable' => 0,
'default_sort_order' => 'asc',
'align' => '',
'separator' => '',
'empty_column' => 0,
),
'type' => array(
'sortable' => 0,
'default_sort_order' => 'asc',
'align' => '',
'separator' => '',
'empty_column' => 0,
),
'active' => array(
'sortable' => 0,
'default_sort_order' => 'asc',
'align' => '',
'separator' => '',
'empty_column' => 0,
),
);
/* No results behavior: Global: Unfiltered text */
$handler->display->display_options['empty']['area_text_custom']['id'] = 'area_text_custom';
$handler->display->display_options['empty']['area_text_custom']['table'] = 'views';
$handler->display->display_options['empty']['area_text_custom']['field'] = 'area_text_custom';
$handler->display->display_options['empty']['area_text_custom']['empty'] = TRUE;
$handler->display->display_options['empty']['area_text_custom']['content'] = 'No plans found.';
/* Field: Farm plan: Farm plan ID */
$handler->display->display_options['fields']['id']['id'] = 'id';
$handler->display->display_options['fields']['id']['table'] = 'farm_plan';
$handler->display->display_options['fields']['id']['field'] = 'id';
$handler->display->display_options['fields']['id']['label'] = 'Plan ID';
$handler->display->display_options['fields']['id']['separator'] = '';
/* Field: Farm plan: Name */
$handler->display->display_options['fields']['name']['id'] = 'name';
$handler->display->display_options['fields']['name']['table'] = 'farm_plan';
$handler->display->display_options['fields']['name']['field'] = 'name';
$handler->display->display_options['fields']['name']['alter']['make_link'] = TRUE;
$handler->display->display_options['fields']['name']['alter']['path'] = 'farm/plan/[id]';
/* Field: Farm plan: Farm plan type */
$handler->display->display_options['fields']['type']['id'] = 'type';
$handler->display->display_options['fields']['type']['table'] = 'farm_plan';
$handler->display->display_options['fields']['type']['field'] = 'type';
$handler->display->display_options['fields']['type']['label'] = 'Plan type';
/* Field: Farm plan: Active */
$handler->display->display_options['fields']['active']['id'] = 'active';
$handler->display->display_options['fields']['active']['table'] = 'farm_plan';
$handler->display->display_options['fields']['active']['field'] = 'active';
$handler->display->display_options['fields']['active']['not'] = 0;
/* Sort criterion: Farm plan: Name */
$handler->display->display_options['sorts']['name']['id'] = 'name';
$handler->display->display_options['sorts']['name']['table'] = 'farm_plan';
$handler->display->display_options['sorts']['name']['field'] = 'name';
$handler->display->display_options['sorts']['name']['exposed'] = TRUE;
$handler->display->display_options['sorts']['name']['expose']['label'] = 'Name';
/* Sort criterion: Farm plan: Farm plan type */
$handler->display->display_options['sorts']['type']['id'] = 'type';
$handler->display->display_options['sorts']['type']['table'] = 'farm_plan';
$handler->display->display_options['sorts']['type']['field'] = 'type';
$handler->display->display_options['sorts']['type']['exposed'] = TRUE;
$handler->display->display_options['sorts']['type']['expose']['label'] = 'Type';
/* Sort criterion: Farm plan: Active */
$handler->display->display_options['sorts']['active']['id'] = 'active';
$handler->display->display_options['sorts']['active']['table'] = 'farm_plan';
$handler->display->display_options['sorts']['active']['field'] = 'active';
$handler->display->display_options['sorts']['active']['exposed'] = TRUE;
$handler->display->display_options['sorts']['active']['expose']['label'] = 'Active';
/* Filter criterion: Farm plan: Name */
$handler->display->display_options['filters']['name']['id'] = 'name';
$handler->display->display_options['filters']['name']['table'] = 'farm_plan';
$handler->display->display_options['filters']['name']['field'] = 'name';
$handler->display->display_options['filters']['name']['operator'] = 'contains';
$handler->display->display_options['filters']['name']['group'] = 1;
$handler->display->display_options['filters']['name']['exposed'] = TRUE;
$handler->display->display_options['filters']['name']['expose']['operator_id'] = 'name_op';
$handler->display->display_options['filters']['name']['expose']['label'] = 'Name';
$handler->display->display_options['filters']['name']['expose']['operator'] = 'name_op';
$handler->display->display_options['filters']['name']['expose']['identifier'] = 'name';
/* Filter criterion: Farm plan: Farm plan type */
$handler->display->display_options['filters']['type']['id'] = 'type';
$handler->display->display_options['filters']['type']['table'] = 'farm_plan';
$handler->display->display_options['filters']['type']['field'] = 'type';
$handler->display->display_options['filters']['type']['group'] = 1;
$handler->display->display_options['filters']['type']['exposed'] = TRUE;
$handler->display->display_options['filters']['type']['expose']['operator_id'] = 'type_op';
$handler->display->display_options['filters']['type']['expose']['label'] = 'Plan type';
$handler->display->display_options['filters']['type']['expose']['operator'] = 'type_op';
$handler->display->display_options['filters']['type']['expose']['identifier'] = 'type';
/* Filter criterion: Farm plan: Active */
$handler->display->display_options['filters']['active']['id'] = 'active';
$handler->display->display_options['filters']['active']['table'] = 'farm_plan';
$handler->display->display_options['filters']['active']['field'] = 'active';
$handler->display->display_options['filters']['active']['value'] = '1';
$handler->display->display_options['filters']['active']['group'] = 1;
$handler->display->display_options['filters']['active']['exposed'] = TRUE;
$handler->display->display_options['filters']['active']['expose']['operator_id'] = '';
$handler->display->display_options['filters']['active']['expose']['label'] = 'Active';
$handler->display->display_options['filters']['active']['expose']['operator'] = 'active_op';
$handler->display->display_options['filters']['active']['expose']['identifier'] = 'active';
/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'farm/plans';
$handler->display->display_options['menu']['type'] = 'normal';
$handler->display->display_options['menu']['title'] = 'Plans';
$handler->display->display_options['menu']['weight'] = '0';
$handler->display->display_options['menu']['name'] = 'farm';
$handler->display->display_options['menu']['context'] = 0;
$handler->display->display_options['menu']['context_only_inline'] = 0;
$translatables['farm_plan'] = array(
t('Master'),
t('Plans'),
t('more'),
t('Apply'),
t('Reset'),
t('Sort by'),
t('Asc'),
t('Desc'),
t('Items per page'),
t('- All -'),
t('Offset'),
t('« first'),
t(' previous'),
t('next '),
t('last »'),
t('No plans found.'),
t('Plan ID'),
t('.'),
t('Name'),
t('Plan type'),
t('Active'),
t('Type'),
t('Page'),
);
$export['farm_plan'] = $view;
return $export;
}

View File

@ -0,0 +1,62 @@
<?php
/**
* @file
* Contains the 'farm plan' argument validator plugin.
*/
/**
* Validate whether an argument is an acceptable farm_plan entity.
*/
class farm_plan_plugin_argument_validate_farm_plan extends views_plugin_argument_validate {
function option_definition() {
$options = parent::option_definition();
$options['types'] = array('default' => array());
return $options;
}
function options_form(&$form, &$form_state) {
$types = farm_plan_types();
$options = array();
foreach ($types as $type => $definition) {
$options[$type] = check_plain($definition->label);
}
$form['types'] = array(
'#type' => 'checkboxes',
'#title' => t('Plan types'),
'#options' => $options,
'#default_value' => $this->options['types'],
'#description' => t('If you wish to validate for specific plan types, check them; if none are checked, all plans will pass.'),
);
}
function options_submit(&$form, &$form_state, &$options = array()) {
// Filter unselected items so we don't unnecessarily store giant arrays.
$options['types'] = array_filter($options['types']);
}
function validate_argument($argument) {
// If the argument is not a number, fail.
if (!is_numeric($argument)) {
return FALSE;
}
// Attempt to load the farm plan entity.
$farm_plan = farm_plan_load($argument);
// If the plan didn't load, fail.
if (empty($farm_plan)) {
return FALSE;
}
// Set the validated title.
$this->argument->validated_title = check_plain(entity_label('farm_plan', $farm_plan));
// Load the valid plan types from options, and return TRUE if the list is
// empty, or if the type is in the list.
$types = array_filter($this->options['types']);
return empty($types) || !empty($types[$farm_plan->type]);
}
}