Skip to content
This repository was archived by the owner on May 9, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cm_tools.info
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
name = "Computerminds tools"
core = "7.x"
description = "A collection of tools and helpers written by Computerminds."

files[] = "handlers/ajax_operations/CMToolsAjaxOperation.inc"
files[] = "handlers/ajax_operations/CMToolsAjaxOperationBroken.inc"
files[] = "handlers/ajax_operations/CMToolsAjaxOperationInterface.inc"
149 changes: 149 additions & 0 deletions cm_tools.module
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,115 @@

include_once 'cm_tools.element.inc';

/**
* Implements hook_menu().
*/
function cm_tools_menu() {
$items = array();

$items['ajax-operation/%cm_tools_ajax_operation'] = array(
'title' => 'AJAX Operation',
'page callback' => 'cm_tools_ajax_operation_callback',
'page arguments' => array(1),
'delivery callback' => 'ajax_deliver',
'access callback' => 'cm_tools_ajax_operation_access',
'access arguments' => array(1),
'theme callback' => 'ajax_base_page_theme',
'type' => MENU_CALLBACK,
'file' => 'includes/ajax_operations.inc',
);

return $items;
}

/**
* Menu loader for cm_tools ajax operations.
*
* @param $operation_name
*
* @return CMToolsAjaxOperationInterface|FALSE
*/
function cm_tools_ajax_operation_load($operation_name) {
ctools_include('plugins');
if (($plugin = ctools_get_plugins('cm_tools', 'ajax_operation', $operation_name)) && ($class = ctools_plugin_get_class($plugin, 'handler'))) {
$op = new $class();
$op->setPluginInfo($plugin);
return $op;
}
return FALSE;
}

/**
* Determine whether a user has access to the given
* ajax operation.
*
* @param CMToolsAjaxOperationInterface $op
* @param null $account
* (Optional) The user account to check access for.
* Defaults to the currently logged in user.
*
* @return boolean
*/
function cm_tools_ajax_operation_access(CMToolsAjaxOperationInterface $op, $account = NULL) {
if (!isset($account)) {
$account = $GLOBALS['user'];
}
return $op->access($account);
}

/**
* Implements hook_ctools_plugin_type().
*/
function cm_tools_ctools_plugin_type() {
return array(
'ajax_operation' => array(
'cache' => TRUE,
'use hooks' => TRUE,
'hook' => 'cm_tools_ajax_operations',
'classes' => array('handler'),
'alterable' => TRUE,
'defaults' => array(
'abstract' => FALSE,
'label' => 'Broken Ajax Operation',
'description' => '',
'parameters' => array(),
'handler' => array(
'class' => 'CMToolsAjaxOperationBroken',
'file' => 'CMToolsAjaxOperationBroken.inc',
'path' => drupal_get_path('module', 'cm_tools') . '/handlers/ajax_operations',
),
),
),
);
}

/**
* Implements hook_ctools_plugin_api().
*
* @param $owner
* @param $api
* @return mixed
*/
function cm_tools_ctools_plugin_api($owner, $api) {
if ($owner === 'cm_tools' && $api === 'ajax_operation') {
return 1;
}
}

/**
* Implements hook_hook_info().
*/
function cm_tools_hook_info() {
$hooks = array();
$hooks['cm_tools_ajax_operations'] = array(
'group' => 'ajax_operations',
);
$hooks['cm_tools_ajax_operations_alter'] = array(
'group' => 'ajax_operations',
);
return $hooks;
}

/**
* Include .inc files as necessary.
*
Expand Down Expand Up @@ -51,6 +160,46 @@ function cm_tools_form_include(&$form_state, $file, $module = 'cm_tools', $dir =
form_load_include($form_state, 'inc', $module, $dir . $file);
}

/**
* Include js files as necessary.
*
* This helper function is used by cm_tools but can also be used in other
* modules in the same way as explained in the comments of cm_tools_include.
*
* @param $file
* The base file name to be included.
* @param $module
* Optional module containing the include.
* @param $dir
* Optional subdirectory containing the include file.
*/
function cm_tools_add_js($file, $module = 'cm_tools', $dir = 'js') {
drupal_add_js(drupal_get_path('module', $module) . "/$dir/$file.js");
}

/**
* Format a javascript file name for use with $form['#attached']['js'].
*
* This helper function is used by cm_tools but can also be used in other
* modules in the same way as explained in the comments of cm_tools_include.
*
* @code
* $form['#attached']['js'] = array(cm_tools_attach_js('auto-submit'));
* @endcode
*
* @param $file
* The base file name to be included.
* @param $module
* Optional module containing the include.
* @param $dir
* Optional subdirectory containing the include file.
*
* @return string
*/
function cm_tools_attach_js($file, $module = 'cm_tools', $dir = 'js') {
return drupal_get_path('module', $module) . "/$dir/$file.js";
}

/**
* Inserts one or more suggestions into a theme_hook_suggestions array.
*
Expand Down
40 changes: 40 additions & 0 deletions handlers/ajax_operations/CMToolsAjaxOperation.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* @file Helpful parent class for all Ajax Operations.
*/
class CMToolsAjaxOperation implements CMToolsAjaxOperationInterface {

protected $plugin_info;

/**
* {@inheritdoc}
*/
public function setPluginInfo(array $plugin_info) {
$this->plugin_info = $plugin_info;
}

/**
* {@inheritdoc}
*/
public function getPluginInfo($key = NULL) {
if (isset($key)) {
return isset($this->plugin_info[$key]) ? $this->plugin_info[$key] : NULL;
}
return isset($this->plugin_info) ? $this->plugin_info : array();
}

/**
* {@inheritdoc}
*/
public function access($account) {
return TRUE;
}

/**
* {@inheritdoc}
*/
public function execute($parameters) {
return array();
}
}
15 changes: 15 additions & 0 deletions handlers/ajax_operations/CMToolsAjaxOperationBroken.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

/**
* @file AjaxOperation class used in case of error.
*/

class CMToolsAjaxOperationBroken extends CMToolsAjaxOperation {

/**
* {@inheritdoc}
*/
public function access($account) {
return FALSE;
}
}
49 changes: 49 additions & 0 deletions handlers/ajax_operations/CMToolsAjaxOperationInterface.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/**
* @file Interface for CMTools Ajax Operations.
*/
interface CMToolsAjaxOperationInterface {

/**
* Sets the CTools plugin info for this handler..
*
* @param array $plugin_info
*
* @return array
*/
public function setPluginInfo(array $plugin_info);

/**
* Return CTools plugin info associated with this handler.
*
* @param null $key
* Optionally return the value of a certain key in the info
* array.
*
* @return array|mixed|null
*/
public function getPluginInfo($key = NULL);

/**
* Determine whether the given user is permitted to access
* this Ajax operation.
*
* @param $account
*
* @return boolean
*/
public function access($account);

/**
* Execute your operation and return a standard array of
* Drupal Ajax commands to execute client side.
*
* @param $parameters
* Array of parameters passed to your operation.
*
* @return array
*/
public function execute($parameters);

}
Loading