/**
  * AJAX entry point to edit a pane.
  */
 function ajax_edit_pane($pid = NULL, $step = NULL)
 {
     if (empty($this->cache->display->content[$pid])) {
         ctools_modal_render(t('Error'), t('Invalid pane id.'));
     }
     $pane =& $this->cache->display->content[$pid];
     // Check if we should skip pane translation.
     if (_mlpanels_pane_skip($pane->type, $pane->subtype)) {
         // Pass to default renderer.
         return parent::ajax_edit_pane($pid, $step);
     }
     $content_type = ctools_get_content_type($pane->type);
     $subtype = ctools_content_get_subtype($content_type, $pane->subtype);
     $settings = _mlpanels_settings();
     if (empty($step)) {
         $messages[] = t('You can translate settings for different languages,
   but you must set %renderer as display renderer in order to see result.', array('%renderer' => t('Multilingual Standard')));
     }
     if ($settings['show_types']) {
         $messages[] = t('Pane type to disable %type', array('%type' => $pane->type . '::' . $pane->subtype));
     }
     // Get language.
     $path = explode('/', $_GET['q']);
     $tmp = explode('_', end($path));
     if ($tmp[0] == 'mlpanels') {
         $conf_lng = $tmp[1];
     }
     if (empty($conf_lng)) {
         $conf_lng = LANGUAGE_NONE;
     }
     // Prepare language dependent config.
     if (!empty($pane->configuration['mlpanels'])) {
         $ml_config = $pane->configuration['mlpanels'];
         $ml_config[LANGUAGE_NONE] = $pane->configuration;
         unset($ml_config[LANGUAGE_NONE]['mlpanels']);
     } else {
         $ml_config[LANGUAGE_NONE] = $pane->configuration;
     }
     if (!empty($ml_config[$conf_lng])) {
         $configuration = $ml_config[$conf_lng];
     } else {
         $messages[] = t('No configuration exists for this language yet, using default.');
         $configuration = $ml_config[LANGUAGE_NONE];
     }
     // Safety check.
     if (isset($configuration['mlpanels'])) {
         unset($configuration['mlpanels']);
     }
     // Change finish button text.
     $finish_text = t('Finish');
     if ($conf_lng != LANGUAGE_NONE) {
         if (_mlpanels_settings('keep_window')) {
             $finish_text = t('Save Translation and Continue');
         } else {
             $finish_text = t('Save Translation and Finish');
         }
     }
     $form_state = array('display' => &$this->cache->display, 'contexts' => $this->cache->display->context, 'pane' => &$pane, 'display cache' => &$this->cache, 'ajax' => TRUE, 'modal' => TRUE, 'modal return' => TRUE, 'commands' => array());
     $form_info = array('path' => $this->get_url('edit-pane', $pid, '%step', 'mlpanels_' . $conf_lng), 'show cancel' => TRUE, 'finish text' => $finish_text, 'next callback' => 'panels_ajax_edit_pane_next', 'finish callback' => 'panels_ajax_edit_pane_finish', 'cancel callback' => 'panels_ajax_edit_pane_cancel');
     // This is used to get our form in form alter.
     if ($conf_lng != LANGUAGE_NONE) {
         $form_info['untranslate text'] = t('Remove Translation');
         $form_info['untranslate hidden'] = empty($ml_config[$conf_lng]);
     }
     // Building form.
     $output = ctools_content_form('edit', $form_info, $form_state, $content_type, $pane->subtype, $subtype, $configuration, $step);
     // Add language links to the form.
     $languages = array(LANGUAGE_NONE => (object) array('name' => t('Default'), 'language' => LANGUAGE_NONE)) + language_list();
     foreach ($languages as $lng) {
         $class = array('ctools-use-modal');
         $class[] = $lng->language;
         if (empty($ml_config[$lng->language])) {
             $class[] = 'empty';
         }
         if ($conf_lng == $lng->language) {
             $class[] = 'current';
         }
         $links[] = l($lng->name, $this->get_url('edit-pane', $pid, $form_state['step'], 'mlpanels_' . $lng->language), array('attributes' => array('class' => $class), 'html' => TRUE));
     }
     $output['mlpanels'] = array('#markup' => theme('item_list', array('items' => $links, 'attributes' => array('class' => array('mlpanels_lnd_list')))));
     $output['mlpanels_messages'] = array('#markup' => '<div class="message-target"></div>');
     // If $rc is FALSE, there was no actual form.
     if ($output === FALSE || !empty($form_state['cancel'])) {
         // Dismiss the modal.
         $this->commands[] = ctools_modal_command_dismiss();
     } elseif (!empty($form_state['clicked_button']) && $form_state['clicked_button']['#wizard type'] == 'untranslate') {
         // Unset surrent translation.
         unset($ml_config[$conf_lng]);
         // Update pane configuration.
         $form_state['pane']->configuration = array('mlpanels' => $ml_config) + $ml_config[LANGUAGE_NONE];
         // References get blown away with AJAX caching. This will fix that.
         $this->cache->display->content[$pid] = $form_state['pane'];
         panels_edit_cache_set($this->cache);
         $this->command_update_pane($pid);
         if (_mlpanels_settings('keep_window') && $conf_lng != LANGUAGE_NONE) {
             drupal_set_message(t('Translation removed.'));
             $this->commands[] = ajax_command_remove('#modal-content .messages');
             $this->commands[] = ajax_command_html('#modal-content .message-target', theme('status_messages'));
             $this->commands[] = ajax_command_invoke('#modal-content input.pane-untranslate', 'hide');
             $this->commands[] = ajax_command_invoke('.mlpanels_lnd_list a.' . $conf_lng, 'addClass', array('empty'));
         } else {
             $this->commands[] = ctools_modal_command_dismiss();
         }
     } elseif (!empty($form_state['complete'])) {
         // Save our settings for selected language.
         $ml_config[$conf_lng] = $configuration;
         // Update pane configuration.
         $form_state['pane']->configuration = array('mlpanels' => $ml_config) + $ml_config[LANGUAGE_NONE];
         // References get blown away with AJAX caching. This will fix that.
         $this->cache->display->content[$pid] = $form_state['pane'];
         panels_edit_cache_set($this->cache);
         $this->command_update_pane($pid);
         if (_mlpanels_settings('keep_window') && $conf_lng != LANGUAGE_NONE) {
             drupal_set_message(t('Translation updated.'));
             $this->commands[] = ajax_command_remove('#modal-content .messages');
             $this->commands[] = ajax_command_html('#modal-content .message-target', theme('status_messages'));
             $this->commands[] = ajax_command_invoke('#modal-content input.pane-untranslate', 'show');
             $this->commands[] = ajax_command_invoke('.mlpanels_lnd_list a.' . $conf_lng, 'removeClass', array('empty'));
         } else {
             $this->commands[] = ctools_modal_command_dismiss();
         }
     } else {
         // Show messages.
         if ($settings['show_messages']) {
             if (isset($messages)) {
                 foreach ($messages as $msg) {
                     drupal_set_message($msg);
                 }
             }
         }
         // This overwrites any previous commands.
         $this->commands = ctools_modal_form_render($form_state, $output);
         array_unshift($this->commands, array('command' => 'mlpanels_ckefix'));
     }
 }
 /**
  * AJAX command to present a dialog with a list of available content.
  */
 function ajax_select_content($region = NULL, $category = NULL)
 {
     if (!array_key_exists($region, $this->plugins['layout']['regions'])) {
         ctools_modal_render(t('Error'), t('Invalid input'));
     }
     $title = t('Add content to !s', array('!s' => $this->plugins['layout']['regions'][$region]));
     $categories = $this->get_categories($this->cache->content_types);
     if (empty($categories)) {
         $output = t('There are no content types you may add to this display.');
     } else {
         $output = theme('panels_add_content_modal', array('renderer' => $this, 'categories' => $categories, 'category' => $category, 'region' => $region));
     }
     $this->commands[] = ctools_modal_command_display($title, $output);
     // Give keybord focus to the first item in the category we just loaded.
     if (!empty($category)) {
         $this->commands[] = ajax_command_invoke(".panels-add-content-modal .panels-section-columns :focusable:first", 'focus');
     }
 }
 /**
  * AJAX Callback for the path table.
  *
  * @param $form
  * @param $form_state
  * @param $offset_to_root
  * @param $clear
  *
  * @throws \Exception
  * @return array
  */
 public static function ajaxCallbackBase($form, $form_state, $offset_to_root, $clear = false)
 {
     if (!array_key_exists('triggering_element', $form_state)) {
         throw new \Exception('The trigger could not be found.');
     }
     $trigger = $form_state['triggering_element'];
     $parents = array_slice($trigger['#array_parents'], 0, count($trigger['#array_parents']) - $offset_to_root);
     $parents[] = 'paths';
     $parents_form = drupal_array_get_nested_value($form, $parents);
     $commands = array();
     $commands[] = ajax_command_replace(null, theme('publisher_purge_configure_content_type_table', array('paths_form' => $parents_form)));
     $commands[] = ajax_command_prepend(null, theme('status_messages'));
     if ($clear) {
         // Get the "Add Path" textbox.
         $add_path_parents = array_slice($parents, 0, count($parents) - 1);
         $add_path_parents[] = 'add_path';
         $add_path_parents[] = 'path';
         $add_path = drupal_array_get_nested_value($form, $add_path_parents);
         // TODO: Actually find out why the form API is adding numbers to the IDs.
         $add_path_id = str_replace('--2', '', $add_path['#id']);
         $commands[] = ajax_command_invoke('#' . $add_path_id, 'val', array(''));
     }
     return array('#type' => 'ajax', '#commands' => $commands);
 }