| 1 |
<?php
|
| 2 |
|
| 3 |
/**
|
| 4 |
* Implementation of hook_init().
|
| 5 |
*/
|
| 6 |
function activeedit_init() {
|
| 7 |
if (user_access('edit via ajax')) {
|
| 8 |
activeedit_modules_includes();
|
| 9 |
activeedit_load();
|
| 10 |
}
|
| 11 |
}
|
| 12 |
|
| 13 |
/**
|
| 14 |
* Implementation of hook_perm().
|
| 15 |
*/
|
| 16 |
function activeedit_perm() {
|
| 17 |
return array('edit via ajax');
|
| 18 |
}
|
| 19 |
|
| 20 |
/**
|
| 21 |
* Load available types by module.
|
| 22 |
*/
|
| 23 |
function activeedit_modules_includes() {
|
| 24 |
$path = drupal_get_path('module', 'activeedit') .'/modules';
|
| 25 |
$files = file_scan_directory($path, '\.inc$');
|
| 26 |
foreach ($files as $file) {
|
| 27 |
if (module_exists($file->name)) {
|
| 28 |
include_once $file->filename;
|
| 29 |
}
|
| 30 |
}
|
| 31 |
}
|
| 32 |
|
| 33 |
/**
|
| 34 |
* Implementation of hook_form_alter().
|
| 35 |
*/
|
| 36 |
function activeedit_form_alter(&$form, $form_state, $form_id) {
|
| 37 |
if (isset($_REQUEST['activeedit_element'])) {
|
| 38 |
$key = $_REQUEST['activeedit_element'];
|
| 39 |
$elements = activeedit_get_elements();
|
| 40 |
if (isset($elements[$key]) && key($elements[$key]['#form']) == $form_id) {
|
| 41 |
formfilter_filter_form($form, $elements[$key]['#form'][$form_id], TRUE);
|
| 42 |
}
|
| 43 |
}
|
| 44 |
}
|
| 45 |
|
| 46 |
function activeedit_load() {
|
| 47 |
static $loaded = FALSE;
|
| 48 |
if (!$loaded) {
|
| 49 |
$path = drupal_get_path('module', 'activeedit');
|
| 50 |
drupal_add_js($path .'/activeedit.js');
|
| 51 |
drupal_add_css($path .'/activeedit.css');
|
| 52 |
|
| 53 |
$selector = activeedit_popup_data();
|
| 54 |
$javascript = drupal_add_js();
|
| 55 |
// If popups_add_popups was previously called, add our data.
|
| 56 |
if (isset($javascript['setting']) && isset($javascript['setting']['popups'])) {
|
| 57 |
$settings = array('popups', array('links' => array($selector => array())));
|
| 58 |
drupal_add_js($settings, 'setting');
|
| 59 |
}
|
| 60 |
// Otherwise, call it now.
|
| 61 |
else {
|
| 62 |
popups_add_popups($selector);
|
| 63 |
}
|
| 64 |
|
| 65 |
// Send activeedit settings.
|
| 66 |
drupal_add_js(array('activeEdit' => activeedit_get_elements(TRUE)), 'setting');
|
| 67 |
$loaded = TRUE;
|
| 68 |
}
|
| 69 |
}
|
| 70 |
|
| 71 |
function activeedit_get_elements($prepare = FALSE) {
|
| 72 |
|
| 73 |
$elements = array();
|
| 74 |
foreach (module_implements('activeedit_elements') as $module) {
|
| 75 |
$elements = array_merge_recursive($elements, module_invoke($module, 'activeedit_elements'));
|
| 76 |
}
|
| 77 |
|
| 78 |
// Test for access.
|
| 79 |
foreach (array_keys ($elements) as $key) {
|
| 80 |
if (!$elements[$key]['#access']) {
|
| 81 |
unset($elements[$key]);
|
| 82 |
}
|
| 83 |
}
|
| 84 |
|
| 85 |
if ($prepare) {
|
| 86 |
_activeedit_prepare($elements);
|
| 87 |
}
|
| 88 |
|
| 89 |
return $elements;
|
| 90 |
}
|
| 91 |
|
| 92 |
function _activeedit_prepare(&$elements) {
|
| 93 |
foreach (element_children ($elements) as $key) {
|
| 94 |
// Convert to appropriate paths.
|
| 95 |
$elements[$key]['#url'] = url($elements[$key]['#url']);
|
| 96 |
// Unset the properties we don't need to pass to the client.
|
| 97 |
foreach (element_properties($elements[$key]) as $property) {
|
| 98 |
if (!in_array($property, array('#selector', '#url', '#title'))) {
|
| 99 |
unset($elements[$key][$property]);
|
| 100 |
}
|
| 101 |
}
|
| 102 |
}
|
| 103 |
}
|
| 104 |
|
| 105 |
/**
|
| 106 |
* Return popup data.
|
| 107 |
*
|
| 108 |
* We don't implement hook_popups() because we handle these data in a custom
|
| 109 |
* way in hook_init().
|
| 110 |
*/
|
| 111 |
function activeedit_popup_data() {
|
| 112 |
return array(
|
| 113 |
'a.activeedit',
|
| 114 |
);
|
| 115 |
}
|
| 116 |
|
| 117 |
|