Newer
Older
Earl Miles
committed
<?php
// $Id$
/**
Earl Miles
committed
* Provide API for adding contexts for modules that embed displays.
*/
/**
* Provide a list of the ways contexts can be embedded.
*
* This provides a full list of context types that Panels understands
* and can let modules utilize.
Earl Miles
committed
*/
function panels_common_context_info($type = NULL) {
static $info = NULL;
// static doesn't work with functions like t().
if (empty($info)) {
$info = array(
'argument' => array(
'title' => t('Arguments'),
'singular title' => t('argument'),
'description' => t("Arguments are parsed from the URL and translated into contexts that may be added to the display via the 'content' tab. These arguments are parsed in the order received, and you may use % in your URL to hold the place of an object; the rest of the arguments will come after the URL. For example, if the URL is node/%/panel and your user visits node/1/panel/foo, the first argument will be 1, and the second argument will be foo."),
'add button' => t('Add argument'),
'context function' => 'panels_get_argument',
'form id' => 'panels_common_edit_argument_form',
'key' => 'arguments', // the key that data will be stored on an object, eg $panel_page
Earl Miles
committed
'sortable' => TRUE,
),
'relationship' => array(
'title' => t('Relationships'),
'singular title' => t('relationship'),
'description' => t('Relationships are contexts that are created from already existing contexts; the add relationship button will only appear once there is another context available. Relationships can load objects based upon how they are related to each other; for example, the author of a node, or a taxonomy term attached to a node, or the vocabulary of a taxonomy term.'),
'add button' => t('Add relationship'),
'context function' => 'panels_get_relationship',
'form id' => 'panels_common_edit_relationship_form',
'key' => 'relationships',
Earl Miles
committed
'sortable' => FALSE,
),
'context' => array(
'title' => t('Contexts'),
'singular title' => t('context'),
'description' => t('Contexts are embedded directly into the panel; you generally must select an object in the panel. For example, you could select node 5, or the term "animals" or the user "administrator"'),
'add button' => t('Add context'),
'context function' => 'panels_get_context',
'form id' => 'panels_common_edit_context_form',
'key' => 'contexts',
Earl Miles
committed
'sortable' => FALSE,
),
'requiredcontext' => array(
'title' => t('Required contexts'),
'singular title' => t('required context'),
'description' => t('Required contexts are passed in from some external source, such as a containing panel. If a mini panel has required contexts, it can only appear when that context is available, and therefore will not show up as a standard Drupal block.'),
'add button' => t('Add required context'),
'context function' => 'panels_get_context',
'form id' => 'panels_common_edit_requiredcontext_form',
'key' => 'requiredcontexts',
Earl Miles
committed
'sortable' => TRUE,
),
);
}
if ($type === NULL) {
return $info;
}
return $info[$type];
}
/**
* Get the data belonging to a particular context.
*/
function panels_common_context_data($type, $name) {
$info = panels_common_context_info($type);
if (function_exists($info['context function'])) {
return $info['context function']($name);
}
}
/**
* Add the argument table plus gadget plus javascript to the form.
*/
function panels_common_add_argument_form($module, &$form, &$form_state, &$form_location, $object) {
$form_location = array(
Earl Miles
committed
'#prefix' => '<div id="panels-arguments-table">',
'#suffix' => '</div>',
Earl Miles
committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
'#theme' => 'panels_common_context_item_form',
'#panel_name' => $object->name,
'#panels_context_type' => 'argument',
'#panels_context_module' => $module,
);
$args = panels_get_arguments();
$choices = array();
foreach ($args as $name => $arg) {
$choices[$name] = $arg['title'];
}
asort($choices);
if (!empty($choices) || !empty($object->arguments)) {
panels_common_add_item_table('argument', $form_location, $choices, $object->arguments);
}
}
function panels_common_add_context_form($module, &$form, &$form_state, &$form_location, $object) {
$form_location = array(
'#prefix' => '<div id="panels-contexts-table">',
'#suffix' => '</div>',
'#theme' => 'panels_common_context_item_form',
'#panel_name' => $object->name,
'#panels_context_type' => 'context',
'#panels_context_module' => $module,
);
// Store the order the choices are in so javascript can manipulate it.
$form_location['markup'] = array(
'#value' => ' ',
);
$choices = array();
foreach (panels_get_contexts() as $name => $arg) {
if (empty($arg['no ui'])) {
$choices[$name] = $arg['title'];
}
}
asort($choices);
if (!empty($choices) || !empty($object->contexts)) {
panels_common_add_item_table('context', $form_location, $choices, $object->contexts);
}
}
function panels_common_add_required_context_form($module, &$form, &$form_state, &$form_location, $object) {
$form_location = array(
'#prefix' => '<div id="panels-requiredcontexts-table">',
'#suffix' => '</div>',
'#theme' => 'panels_common_context_item_form',
'#panel_name' => $object->name,
'#panels_context_type' => 'requiredcontext',
'#panels_context_module' => $module,
);
// Store the order the choices are in so javascript can manipulate it.
$form_location['markup'] = array(
'#value' => ' ',
);
$choices = array();
foreach (panels_get_contexts() as $name => $arg) {
$choices[$name] = $arg['title'];
}
asort($choices);
if (!empty($choices) || !empty($object->contexts)) {
panels_common_add_item_table('requiredcontext', $form_location, $choices, $object->requiredcontexts);
}
}
function panels_common_add_relationship_form($module, &$form, &$form_state, &$form_location, $object) {
$form_location = array(
'#prefix' => '<div id="panels-relationships-table">',
'#suffix' => '</div>',
'#theme' => 'panels_common_context_item_form',
'#panel_name' => $object->name,
'#panels_context_type' => 'relationship',
'#panels_context_module' => $module,
);
// Store the order the choices are in so javascript can manipulate it.
$form_location['markup'] = array(
'#value' => ' ',
);
$available_relationships = panels_get_relevant_relationships(panels_context_load_contexts($object));
panels_common_add_item_table('relationship', $form_location, $available_relationships, $object->relationships);
Earl Miles
committed
}
function panels_common_add_context_js($base) {
panels_load_include('ajax');
panels_modal_js_includes();
// TODO: extract the parts necessary for the modal form into their own css file.
Earl Miles
committed
drupal_add_css(panels_get_path('css/panels_dnd.css'));
}
/**
* Add the context table to the page.
*/
function panels_common_add_item_table($type, &$form, $available_contexts, $items) {
$form[$type] = array(
'#tree' => TRUE,
);
$module = $form['#panels_context_module'];
$name = $form['#panel_name'];
Earl Miles
committed
if (isset($items) && is_array($items)) {
foreach ($items as $position => $context) {
panels_common_add_item_to_form($module, $type, $name, $form[$type][$position], $position, $context);
Earl Miles
committed
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
}
}
$type_info = panels_common_context_info($type);
$form['description'] = array(
'#prefix' => '<div class="description">',
'#suffix' => '</div>',
'#value' => $type_info['description'],
);
panels_common_add_item_table_buttons($type, $module, $form, $available_contexts);
}
function panels_common_add_item_table_buttons($type, $module, &$form, $available_contexts) {
$form['buttons'] = array(
'#tree' => TRUE,
);
if (!empty($available_contexts)) {
$type_info = panels_common_context_info($type);
$module = $form['#panels_context_module'];
$name = $form['#panel_name'];
// The URL for this ajax button
$form['buttons'][$type]['add-url'] = array(
'#attributes' => array('class' => "panels-$type-add-url"),
'#type' => 'hidden',
'#value' => url("panels/ajax/context-add/$module/$type/$name", array('absolute' => TRUE)),
);
// This also will be in the URL.
$form['buttons'][$type]['item'] = array(
'#attributes' => array('class' => "panels-$type-add-url"),
'#type' => 'select',
'#options' => $available_contexts,
);
Earl Miles
committed
$form['buttons'][$type]['add'] = array(
'#type' => 'submit',
'#attributes' => array('class' => 'panels-ajax-link'),
'#id' => "panels-$type-add",
'#value' => $type_info['add button'],
);
}
}
/**
* Add a row to the form. Used both in the main form and by
* the ajax to add an item.
*/
function panels_common_add_item_to_form($module, $type, $name, &$form, $position, $item) {
panels_load_include('ajax');
Earl Miles
committed
// This is the single function way to load any plugin by variable type.
$info = panels_common_context_data($type, $item['name']);
$form['title'] = array(
'#value' => check_plain($item['identifier']),
);
// Relationships not sortable.
$type_info = panels_common_context_info($type);
if (!empty($type_info['sortable'])) {
$form['position'] = array(
'#type' => 'weight',
'#default_value' => $position,
'#attributes' => array('class' => 'drag-position'),
Earl Miles
committed
);
}
$form['remove'] = array('#value' => panels_ajax_image_button('icon-delete.png', "panels/ajax/context-delete/$module/$type/$name/$position", t('Remove this item.'), 'panels-no-modal'));
Earl Miles
committed
$form['settings'] = array('#value' => panels_ajax_image_button('icon-configure.png', "panels/ajax/context-configure/$module/$type/$name/$position", t('Configure settings for this item.')));
Earl Miles
committed
}
/**
* Theme the form item for the context entry.
*/
function theme_panels_common_context_item_row($type, $form, $position, $count, $with_tr = TRUE) {
$output = '<td class="title"> ' . drupal_render($form['title']) . '</td>';
if (!empty($form['position'])) {
$output .= '<td class="position"> ' . drupal_render($form['position']) . '</td>';
Earl Miles
committed
}
$output .= '<td class="operation">' . drupal_render($form['settings']);
Earl Miles
committed
$output .= drupal_render($form['remove']) . '</td>';
if ($with_tr) {
$output = '<tr id="' . $type . '-row-' . $position . '" class="draggable ' . $type . '-row ' . ($count % 2 ? 'even' : 'odd') . '">' . $output . '</tr>';
Earl Miles
committed
}
return $output;
}
/**
* Add the contexts form to panel page settings
*/
function theme_panels_common_context_item_form($form) {
$output = '';
$type = $form['#panels_context_type'];
$module = $form['#panels_context_module'];
$name = $form['#panel_name'];
$type_info = panels_common_context_info($type);
if (!empty($form[$type]) && empty($form['#only_buttons'])) {
Earl Miles
committed
$count = 0;
$rows = '';
foreach (array_keys($form[$type]) as $id) {
if (!is_numeric($id)) {
continue;
}
$rows .= theme('panels_common_context_item_row', $type, $form[$type][$id], $id, $count++);
}
Earl Miles
committed
$output .= '<table id="' . $type . '-table">';
$output .= '<thead>';
$output .= '<tr>';
$output .= '<th class="title">' . $type_info['title'] . '</th>';
Earl Miles
committed
if (!empty($type_info['sortable']) && $count) {
$output .= '<th class="position">' . t('Weight') . '</th>';
}
$output .= '<th class="operation">' . t('Operation') . '</th>';
Earl Miles
committed
$output .= '</thead>';
$output .= '<tbody>';
Earl Miles
committed
$output .= $rows;
Earl Miles
committed
$output .= '</tbody>';
$output .= '</table>';
}
if (!empty($form['buttons'])) {
// Display the add context item.
$row = array();
$row[] = array('data' => drupal_render($form['buttons'][$type]['item']), 'class' => 'title');
$row[] = array('data' => drupal_render($form['buttons'][$type]['add']), 'class' => 'add', 'width' => "60%");
$output .= '<div class="buttons">';
$output .= drupal_render($form['buttons'][$type]);
$output .= theme('table', array(), array($row), array('id' => $type . '-add-table'));
$output .= '</div>';
}
if (!empty($form['description'])) {
$output .= drupal_render($form['description']);
}
if (!empty($type_info['sortable'])) {
drupal_add_tabledrag($type . '-table', 'order', 'sibling', 'drag-position');
}
Earl Miles
committed
return $output;
}
// ---------------------------------------------------------------------------
// AJAX forms and stuff.
Earl Miles
committed
/**
* Ajax entry point to add an context
*/
function panels_ajax_context_item_add($module = NULL, $type = NULL, $panel_name = NULL, $name = NULL) {
panels_load_include('plugins');
panels_load_include('ajax');
Earl Miles
committed
if (!$name) {
return panels_ajax_render();
Earl Miles
committed
}
// Load stored object from cache.
if (!($object = panels_cache_get("panel_object:$module", $panel_name))) {
panels_ajax_render(t('Invalid panel name.'));
Earl Miles
committed
}
// Get info about what we're adding.
Earl Miles
committed
$info = panels_common_context_data($type, $name);
if (empty($info)) {
panels_ajax_render();
}
$type_info = panels_common_context_info($type);
Earl Miles
committed
// Create a reference to the place our context lives. Since this is fairly
// generic, this is the easiest way to get right to the place of the
// object without knowing precisely what data we're poking at.
$ref = &$object->{$type_info['key']};
Earl Miles
committed
// Give this item an id, which is really just the nth version
Earl Miles
committed
// of this particular context.
$id = panels_common_get_arg_id($ref, $name) + 1;
// Figure out the position for our new context.
$position = empty($ref) ? 0 : max(array_keys($ref)) + 1;
// Create the basis for our new context.
$ref[$position] = array(
'identifier' => $info['title'] . ($id > 1 ? ' ' . $id : ''),
'keyword' => panels_common_get_keyword($object, $info['keyword']),
'id' => $id,
Earl Miles
committed
'name' => $name,
Earl Miles
committed
);
$contexts = panels_context_load_contexts($object);
$form_state = array(
'object' => &$object,
'module' => $module,
'type' => $type,
'name' => $name,
'ajax' => TRUE,
'info' => $info,
'position' => $position,
'contexts' => $contexts,
'ref' => &$ref,
'title' => t('Add @type "@context"', array('@type' => $type_info['singular title'], '@context' => $info['title'])),
);
Earl Miles
committed
$output = panels_ajax_form_wrapper($type_info['form id'], $form_state);
Earl Miles
committed
if (empty($output)) {
// successful submit
Earl Miles
committed
panels_cache_set("panel_object:$module", $panel_name, $object);
$arg_form_state = array();
Earl Miles
committed
$arg_form = array(
'#post' => array(),
'#programmed' => FALSE,
'#tree' => FALSE,
);
Earl Miles
committed
// Build a chunk of the form to merge into the displayed form
$arg_form[$type] = array(
'#tree' => TRUE,
);
$arg_form[$type][$position] = array(
'#tree' => TRUE,
);
Earl Miles
committed
panels_common_add_item_to_form($module, $type, $panel_name, $arg_form[$type][$position], $position, $ref[$position]);
$arg_form = form_builder($type_info['form id'], $arg_form, $arg_form_state);
Earl Miles
committed
// Build the relationships table so we can ajax it in.
// This is an additional thing that goes in here.
$rel_form = array(
'#theme' => 'panels_common_context_item_form',
'#panel_name' => $panel_name,
'#panels_context_type' => 'relationship',
'#panels_context_module' => $module,
'#only_buttons' => TRUE,
Earl Miles
committed
'#post' => array(),
'#programmed' => FALSE,
'#tree' => FALSE,
Earl Miles
committed
);
$rel_form['relationship'] = array(
'#tree' => TRUE,
);
$all_contexts = panels_context_load_contexts($object);
$available_relationships = panels_get_relevant_relationships($all_contexts);
Earl Miles
committed
$output = new stdClass();
if (!empty($available_relationships)) {
panels_common_add_item_table_buttons('relationship', $module, $rel_form, $available_relationships);
$rel_form = form_builder('dummy_form_id', $rel_form, $arg_form_state);
$output->replace = array('div#panels-relationships-table div.buttons' => drupal_render($rel_form));
Earl Miles
committed
}
$output->type = 'add';
$output->output = theme('panels_common_context_item_row', $type, $arg_form[$type][$position], $position, $position);
$output->region = '#' . $type . '-table tbody';
$output->id = '#' . $type . '-row-' . $position;
Earl Miles
committed
}
panels_ajax_render($output);
Earl Miles
committed
}
/**
* Ajax entry point to edit an item
*/
function panels_ajax_context_item_edit($module = NULL, $type = NULL, $panel_name = NULL, $position = NULL) {
panels_load_include('plugins');
panels_load_include('ajax');
Earl Miles
committed
if (!isset($position)) {
return panels_ajax_render();
Earl Miles
committed
}
// Load stored object from cache.
if (!($object = panels_cache_get("panel_object:$module", $panel_name))) {
panels_ajax_render(t('Invalid panel name.'));
Earl Miles
committed
}
$type_info = panels_common_context_info($type);
// Create a reference to the place our context lives. Since this is fairly
// generic, this is the easiest way to get right to the place of the
// object without knowing precisely what data we're poking at.
$ref = &$object->{$type_info['key']};
Earl Miles
committed
$name = $ref[$position]['name'];
if (empty($name)) {
panels_ajax_render();
}
// load the context
$info = panels_common_context_data($type, $name);
if (empty($info)) {
Earl Miles
committed
panels_ajax_render(t('Invalid context type'));
Earl Miles
committed
}
$contexts = panels_context_load_contexts($object);
// Remove this context, because we can't really allow circular contexts.
unset($contexts[panels_context_context_id($ref[$position])]);
$form_state = array(
'object' => &$object,
'module' => $module,
'type' => $type,
'name' => $name,
'ajax' => TRUE,
'info' => $info,
'position' => $position,
'contexts' => $contexts,
'ref' => &$ref,
'title' => t('Edit @type "@context"', array('@type' => $type_info['singular title'], '@context' => $info['title'])),
);
Earl Miles
committed
$output = panels_ajax_form_wrapper($type_info['form id'], $form_state);
Earl Miles
committed
if (empty($output)) {
// successful submit
Earl Miles
committed
panels_cache_set("panel_object:$module", $panel_name, $object);
$output = new stdClass();
Earl Miles
committed
$output->type = $output->output = 'dismiss';
Earl Miles
committed
$arg_form = array(
'#post' => array(),
'#programmed' => FALSE,
'#tree' => FALSE,
);
Earl Miles
committed
// Build a chunk of the form to merge into the displayed form
$arg_form[$type] = array(
'#tree' => TRUE,
);
$arg_form[$type][$position] = array(
'#tree' => TRUE,
);
Earl Miles
committed
panels_common_add_item_to_form($module, $type, $panel_name, $arg_form[$type][$position], $position, $ref[$position]);
$arg_form = form_builder($type_info['form id'], $arg_form, $arg_form_state);
Earl Miles
committed
$output->replace = array(
'#' . $type . '-row-' . $position => theme('panels_common_context_item_row', $type, $arg_form[$type][$position], $position, $position)
);
Earl Miles
committed
}
panels_ajax_render($output);
Earl Miles
committed
}
/**
* Form (for ajax use) to add a context
*/
function panels_common_edit_context_form(&$form_state) {
$object = $form_state['object'];
$context = $form_state['info'];
$position = $form_state['position'];
$contexts = $form_state['contexts'];
Earl Miles
committed
$ctext = $object->contexts[$position];
Earl Miles
committed
$form['description'] = array(
'#prefix' => '<div class="description">',
'#suffix' => '</div>',
'#value' => check_plain($context['description']),
);
// Basic context values
$form['context']['#tree'] = TRUE;
$form['context']['name'] = array(
'#type' => 'hidden',
'#value' => $context['name'],
);
Earl Miles
committed
$form['context']['id'] = array(
'#type' => 'hidden',
'#value' => $ctext['id'],
);
Earl Miles
committed
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
$form['context']['identifier'] = array(
'#type' => 'textfield',
'#title' => t('Identifier'),
'#description' => t('Enter a name to identify this !type on administrative screens.', array('!type' =>t('context'))),
'#default_value' => $ctext['identifier'],
);
$form['context']['keyword'] = array(
'#type' => 'textfield',
'#title' => t('Keyword'),
'#description' => t('Enter a keyword to use for substitution in titles.'),
'#default_value' => $ctext['keyword'],
);
// Settings particular to this context
$context_settings = array();
if (isset($ctext['context_settings'])) {
$context_settings = $ctext['context_settings'];
}
if (isset($context['settings form']) && function_exists($context['settings form'])) {
$form['context']['context_settings'] = $context['settings form']($context_settings);
$form['context']['context_settings']['#tree'] = TRUE;
}
$form['next'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
/**
* validate a context edited/added via ajax
*/
function panels_common_edit_context_form_validate($form, &$form_state) {
$context = $form_state['info'];
Earl Miles
committed
if (isset($context['settings form validate']) && function_exists($context['settings form validate'])) {
$context['settings form validate']($form['context']['context_settings'], $form_state['values']['context']['context_settings'], $form_state);
Earl Miles
committed
}
}
/**
* Updates an context edited/added via ajax
*/
function panels_common_edit_context_form_submit($form, &$form_state) {
$info = $form_state['info'];
Earl Miles
committed
if (isset($info['settings form submit']) && function_exists($info['settings form submit'])) {
$info['settings form submit']($form, $form_state['values']['context']['context_settings'], $form_state);
Earl Miles
committed
}
$context = $form_state['values']['context'];
$form_state['ref'][$form_state['position']] = $context;
Earl Miles
committed
}
/**
* Form (for ajax use) to add a context
*/
function panels_common_edit_requiredcontext_form(&$form_state) {
$object = $form_state['object'];
$context = $form_state['info'];
$position = $form_state['position'];
$contexts = $form_state['contexts'];
Earl Miles
committed
$ctext = $object->requiredcontexts[$position];
Earl Miles
committed
$form['start_form'] = array('#value' => '<div class="modal-form clear-block">');
$form['description'] = array(
'#prefix' => '<div class="description">',
'#suffix' => '</div>',
'#value' => check_plain($context['description']),
);
// Basic context values
$form['requiredcontext']['#tree'] = TRUE;
$form['requiredcontext']['name'] = array(
'#type' => 'hidden',
'#value' => $context['name'],
);
Earl Miles
committed
$form['requiredcontext']['id'] = array(
'#type' => 'hidden',
'#value' => $ctext['id'],
);
Earl Miles
committed
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
$form['requiredcontext']['identifier'] = array(
'#type' => 'textfield',
'#title' => t('Identifier'),
'#description' => t('Enter a name to identify this !type on administrative screens.', array('!type' =>t('required context'))),
'#default_value' => $ctext['identifier'],
);
$form['requiredcontext']['keyword'] = array(
'#type' => 'textfield',
'#title' => t('Keyword'),
'#description' => t('Enter a keyword to use for substitution in titles.'),
'#default_value' => $ctext['keyword'],
);
$form['end_form'] = array('#value' => '</div>');
$form['next'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
/**
* Updates a required context edited/added via ajax
*/
function panels_common_edit_requiredcontext_form_submit($form, &$form_state) {
$form_state['ref'][$form_state['position']] = $form_state['values']['requiredcontext'];
Earl Miles
committed
}
/**
* Form (for ajax use) to add a relationship
*/
function panels_common_edit_relationship_form(&$form_state) {
$object = $form_state['object'];
$relationship = $form_state['info'];
$position = $form_state['position'];
$contexts = $form_state['contexts'];
Earl Miles
committed
$rel = $object->relationships[$position];
Earl Miles
committed
$form['description'] = array(
'#prefix' => '<div class="description">',
'#suffix' => '</div>',
'#value' => check_plain($relationship['description']),
);
// Basic relationship values
$form['relationship']['#tree'] = TRUE;
$form['relationship']['context'] = panels_context_selector($contexts, $relationship['required context'], $rel['context']);
$form['relationship']['name'] = array(
'#type' => 'hidden',
'#value' => $relationship['name'],
);
Earl Miles
committed
$form['relationship']['id'] = array(
'#type' => 'hidden',
'#value' => $rel['id'],
);
Earl Miles
committed
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
$form['relationship']['identifier'] = array(
'#type' => 'textfield',
'#title' => t('Identifier'),
'#description' => t('Enter a name to identify this !type on administrative screens.', array('!type' =>t('relationship'))),
'#default_value' => $rel['identifier'],
);
$form['relationship']['keyword'] = array(
'#type' => 'textfield',
'#title' => t('Keyword'),
'#description' => t('Enter a keyword to use for substitution in titles.'),
'#default_value' => $rel['keyword'],
);
// Settings particular to this relationship
$relationship_settings = array();
if (isset($rel['relationship_settings'])) {
$relationship_settings = $rel['relationship_settings'];
}
if (isset($relationship['settings form']) && function_exists($relationship['settings form'])) {
$form['relationship']['relationship_settings'] = $relationship['settings form']($relationship_settings);
$form['relationship']['relationship_settings']['#tree'] = TRUE;
}
$form['next'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
/**
* validate an relationship edited/added via ajax
*/
function panels_common_edit_relationship_form_validate($form, &$form_state) {
$relationship = $form_state['info'];
Earl Miles
committed
if (isset($relationship['settings form validate']) && function_exists($relationship['settings form validate'])) {
$relationship['settings form validate']($form['relationship']['relationship_settings'], $form_state['values']['relationship']['relationship_settings'], $form_state);
Earl Miles
committed
}
}
/**
* Updates an relationship edited/added via ajax
*/
function panels_common_edit_relationship_form_submit($form, &$form_state) {
$relationship = $form_state['info'];
Earl Miles
committed
if (isset($relationship['settings form submit']) && function_exists($relationship['settings form submit'])) {
$relationship['settings form submit']($form, $form_state['values']['relationship_settings'], $form_state);
Earl Miles
committed
}
$form_state['ref'][$form_state['position']] = $form_state['values']['relationship'];
Earl Miles
committed
}
/**
* Form (for ajax use) to add an argument
*/
function panels_common_edit_argument_form(&$form_state) {
Earl Miles
committed
// Basic values required to orient ourselves
$object = $form_state['object'];
$argument = $form_state['info'];
$position = $form_state['position'];
$contexts = $form_state['contexts'];
Earl Miles
committed
$arg = $object->arguments[$position];
Earl Miles
committed
Earl Miles
committed
if (!isset($arg['default'])) {
$arg['default'] = 'ignore';
$arg['title'] = '';
}
Earl Miles
committed
$form['description'] = array(
'#prefix' => '<div class="description">',
'#suffix' => '</div>',
'#value' => check_plain($argument['description']),
);
// Basic argument values
$form['argument']['#tree'] = TRUE;
Earl Miles
committed
Earl Miles
committed
$form['argument']['name'] = array(
'#type' => 'hidden',
'#value' => $argument['name'],
);
Earl Miles
committed
$form['argument']['id'] = array(
'#type' => 'hidden',
'#value' => $arg['id'],
);
Earl Miles
committed
$form['argument']['default'] = array(
'#type' => 'select',
'#title' => t('Default'),
'#options' => array(
'ignore' => t('Ignore it; content that requires this context will not be available.'),
'404' => t('Display page not found or display nothing at all.'),
Earl Miles
committed
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
),
'#default_value' => $arg['default'],
'#description' => t('If the argument is missing or is not valid, select how this should behave.'),
);
$form['argument']['title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#default_value' => $arg['title'],
'#description' => t('Enter a title to use when this argument is present. You may use %KEYWORD substitution, where the keyword is specified by the administrator.'),
);
$form['argument']['identifier'] = array(
'#type' => 'textfield',
'#title' => t('Identifier'),
'#description' => t('Enter a name to identify this !type on administrative screens.', array('!type' =>t('argument'))),
'#default_value' => $arg['identifier'],
);
$form['argument']['keyword'] = array(
'#type' => 'textfield',
'#title' => t('Keyword'),
'#description' => t('Enter a keyword to use for substitution in titles.'),
'#default_value' => $arg['keyword'],
);
// Settings particular to this argument
$argument_settings = array();
if (isset($arg['argument_settings'])) {
$argument_settings = $arg['argument_settings'];
}
Earl Miles
committed
Earl Miles
committed
if (isset($argument['settings form']) && function_exists($argument['settings form'])) {
$form['argument']['argument_settings'] = $argument['settings form']($argument_settings);
}
Earl Miles
committed
$form['argument']['argument_settings']['#tree'] = TRUE;
Earl Miles
committed
$form['next'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
/**
* validate an argument edited/added via ajax
*/
function panels_common_edit_argument_form_validate($form, &$form_state) {
$argument = $form_state['info'];
Earl Miles
committed
if (isset($argument['settings form validate']) && function_exists($argument['settings form validate'])) {
$argument['settings form validate']($form, $form_state['values']['argument_settings'], $form_state);
Earl Miles
committed
}
}
/**
* Updates an argument edited/added via ajax
*/
Earl Miles
committed
function panels_common_edit_argument_form_submit($form, &$form_state) {
$argument = $form_state['info'];
Earl Miles
committed
Earl Miles
committed
if (!isset($form_state['values']['argument_settings'])) {
$form_state['values']['argument_settings'] = array();
}
Earl Miles
committed
if (isset($argument['settings form submit']) && function_exists($argument['settings form submit'])) {
$argument['settings form submit']($form, $form_state['values']['argument_settings'], $form_state);
Earl Miles
committed
}
$form_state['ref'][$form_state['position']] = $form_state['values']['argument'];
Earl Miles
committed
}
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
/**
* Ajax entry point to edit an item
*/
function panels_ajax_context_item_delete($module = NULL, $type = NULL, $panel_name = NULL, $position = NULL) {
panels_load_include('plugins');
panels_load_include('ajax');
if (!isset($position)) {
return panels_ajax_render();
}
// Load stored object from cache.
if (!($object = panels_cache_get("panel_object:$module", $panel_name))) {
panels_ajax_render(t('Invalid panel name.'));
}
$type_info = panels_common_context_info($type);
// Create a reference to the place our context lives. Since this is fairly
// generic, this is the easiest way to get right to the place of the
// object without knowing precisely what data we're poking at.
$ref = &$object->{$type_info['key']};
if (!array_key_exists($position, $ref)) {
panels_ajax_render(t('Unable to delete missing item!'));
}
unset($ref[$position]);
panels_cache_set("panel_object:$module", $panel_name, $object);
$output = new stdClass();
$output->type = $output->output = 'dismiss';
$output->replace = array('#' . $type . '-row-' . $position => '');
$output->exec = "Drupal.Panels.restripeTable('#$type-table');";
panels_ajax_render($output);
}
Earl Miles
committed
// --- End of contexts
function panels_common_save_context($type, &$ref, $form_values) {
$type_info = panels_common_context_info($type);
Earl Miles
committed
// Organize arguments
$new = array();
$order = array();
foreach ($ref as $id => $context) {
$position = $form_values[$type][$id]['position'];
$order[$position] = $id;
}
ksort($order);
foreach ($order as $id) {
$new[] = $ref[$id];
}
$ref = $new;
}
// TODO: Move this somewhere more appropriate
function panels_common_get_arg_id($arguments, $name) {
// Figure out which instance of this argument we're creating
$id = 0;
foreach ($arguments as $arg) {
if ($arg['name'] == $name) {
if ($arg['id'] > $id) {
$id = $arg['id'];
}
Earl Miles
committed
}
}
return $id;
}
function panels_common_get_keyword($page, $word) {
// Create a complete set of keywords
$keywords = array();
foreach (array('arguments', 'relationships', 'contexts', 'requiredcontexts') as $type) {
if (!empty($page->$type) && is_array($page->$type)) {
foreach ($page->$type as $info) {
$keywords[$info['keyword']] = TRUE;
}
}
}
$keyword = $word;