/**
 * Send TRI or EVN editor via ajax or by echoing.
 *
 * @param string $type      TRI or EVN
 * @param string $mode      Editor mode 'add' or 'edit'
 * @param array  $item      Data necessary to create the editor
 * @param string $title     Title of the editor
 * @param string $db        Database
 * @param string $operation Operation 'change' or ''
 *
 * @return void
 */
function PMA_RTE_sendEditor($type, $mode, $item, $title, $db, $operation = null)
{
    if ($item !== false) {
        // Show form
        if ($type == 'TRI') {
            $editor = PMA_TRI_getEditorForm($mode, $item);
        } else {
            // EVN
            $editor = PMA_EVN_getEditorForm($mode, $operation, $item);
        }
        if ($GLOBALS['is_ajax_request']) {
            $response = PMA_Response::getInstance();
            $response->addJSON('message', $editor);
            $response->addJSON('title', $title);
        } else {
            echo "\n\n<h2>{$title}</h2>\n\n{$editor}";
            unset($_POST);
        }
        exit;
    } else {
        $message = __('Error in processing request:') . ' ';
        $message .= sprintf(PMA_RTE_getWord('not_found'), htmlspecialchars(PMA_Util::backquote($_REQUEST['item_name'])), htmlspecialchars(PMA_Util::backquote($db)));
        $message = PMA_message::error($message);
        if ($GLOBALS['is_ajax_request']) {
            $response = PMA_Response::getInstance();
            $response->isSuccess(false);
            $response->addJSON('message', $message);
            exit;
        } else {
            $message->display();
        }
    }
}
 /**
  * Test for PMA_TRI_getEditorForm
  *
  * @param string $data    Data for trigger
  * @param array  $matcher Matcher
  *
  * @return void
  *
  * @dataProvider provider_ajax
  */
 public function testgetEditorForm_ajax($data, $matcher)
 {
     $GLOBALS['is_ajax_request'] = true;
     PMA_TRI_setGlobals();
     $this->assertTag($matcher, PMA_TRI_getEditorForm('edit', $data), '', false);
 }
/**
 * Handles editor requests for adding or editing an item
 *
 * @return void
 */
function PMA_TRI_handleEditor()
{
    global $_REQUEST, $_POST, $errors, $db, $table;
    if (!empty($_REQUEST['editor_process_add']) || !empty($_REQUEST['editor_process_edit'])) {
        $sql_query = '';
        $item_query = PMA_TRI_getQueryFromRequest();
        if (!count($errors)) {
            // set by PMA_RTN_getQueryFromRequest()
            // Execute the created query
            if (!empty($_REQUEST['editor_process_edit'])) {
                // Backup the old trigger, in case something goes wrong
                $trigger = PMA_TRI_getDataFromName($_REQUEST['item_original_name']);
                $create_item = $trigger['create'];
                $drop_item = $trigger['drop'] . ';';
                $result = PMA_DBI_try_query($drop_item);
                if (!$result) {
                    $errors[] = sprintf(__('The following query has failed: "%s"'), htmlspecialchars($drop_item)) . '<br />' . __('MySQL said: ') . PMA_DBI_getError(null);
                } else {
                    $result = PMA_DBI_try_query($item_query);
                    if (!$result) {
                        $errors[] = sprintf(__('The following query has failed: "%s"'), htmlspecialchars($item_query)) . '<br />' . __('MySQL said: ') . PMA_DBI_getError(null);
                        // We dropped the old item, but were unable to create the new one
                        // Try to restore the backup query
                        $result = PMA_DBI_try_query($create_item);
                        if (!$result) {
                            // OMG, this is really bad! We dropped the query,
                            // failed to create a new one
                            // and now even the backup query does not execute!
                            // This should not happen, but we better handle
                            // this just in case.
                            $errors[] = __('Sorry, we failed to restore the dropped trigger.') . '<br />' . __('The backed up query was:') . "\"" . htmlspecialchars($create_item) . "\"" . '<br />' . __('MySQL said: ') . PMA_DBI_getError(null);
                        }
                    } else {
                        $message = PMA_Message::success(__('Trigger %1$s has been modified.'));
                        $message->addParam(PMA_Util::backquote($_REQUEST['item_name']));
                        $sql_query = $drop_item . $item_query;
                    }
                }
            } else {
                // 'Add a new item' mode
                $result = PMA_DBI_try_query($item_query);
                if (!$result) {
                    $errors[] = sprintf(__('The following query has failed: "%s"'), htmlspecialchars($item_query)) . '<br /><br />' . __('MySQL said: ') . PMA_DBI_getError(null);
                } else {
                    $message = PMA_Message::success(__('Trigger %1$s has been created.'));
                    $message->addParam(PMA_Util::backquote($_REQUEST['item_name']));
                    $sql_query = $item_query;
                }
            }
        }
        if (count($errors)) {
            $message = PMA_Message::error(__('<b>One or more errors have occured while processing your request:</b>'));
            $message->addString('<ul>');
            foreach ($errors as $string) {
                $message->addString('<li>' . $string . '</li>');
            }
            $message->addString('</ul>');
        }
        $output = PMA_Util::getMessage($message, $sql_query);
        if ($GLOBALS['is_ajax_request']) {
            $response = PMA_Response::getInstance();
            if ($message->isSuccess()) {
                $items = PMA_DBI_get_triggers($db, $table, '');
                $trigger = false;
                foreach ($items as $value) {
                    if ($value['name'] == $_REQUEST['item_name']) {
                        $trigger = $value;
                    }
                }
                $insert = false;
                if (empty($table) || $trigger !== false && $table == $trigger['table']) {
                    $insert = true;
                    $response->addJSON('new_row', PMA_TRI_getRowForList($trigger));
                    $response->addJSON('name', htmlspecialchars(strtoupper($_REQUEST['item_name'])));
                }
                $response->addJSON('insert', $insert);
                $response->addJSON('message', $output);
            } else {
                $response->addJSON('message', $message);
                $response->isSuccess(false);
            }
            exit;
        }
    }
    /**
     * Display a form used to add/edit a trigger, if necessary
     */
    if (count($errors) || empty($_REQUEST['editor_process_add']) && empty($_REQUEST['editor_process_edit']) && (!empty($_REQUEST['add_item']) || !empty($_REQUEST['edit_item']))) {
        // Get the data for the form (if any)
        if (!empty($_REQUEST['add_item'])) {
            $title = PMA_RTE_getWord('add');
            $item = PMA_TRI_getDataFromRequest();
            $mode = 'add';
        } else {
            if (!empty($_REQUEST['edit_item'])) {
                $title = __("Edit trigger");
                if (!empty($_REQUEST['item_name']) && empty($_REQUEST['editor_process_edit'])) {
                    $item = PMA_TRI_getDataFromName($_REQUEST['item_name']);
                    if ($item !== false) {
                        $item['item_original_name'] = $item['item_name'];
                    }
                } else {
                    $item = PMA_TRI_getDataFromRequest();
                }
                $mode = 'edit';
            }
        }
        if ($item !== false) {
            // Show form
            $editor = PMA_TRI_getEditorForm($mode, $item);
            if ($GLOBALS['is_ajax_request']) {
                $response = PMA_Response::getInstance();
                $response->addJSON('message', $editor);
                $response->addJSON('title', $title);
            } else {
                echo "\n\n<h2>{$title}</h2>\n\n{$editor}";
                unset($_POST);
            }
            exit;
        } else {
            $message = __('Error in processing request') . ' : ';
            $message .= sprintf(PMA_RTE_getWord('not_found'), htmlspecialchars(PMA_Util::backquote($_REQUEST['item_name'])), htmlspecialchars(PMA_Util::backquote($db)));
            $message = PMA_message::error($message);
            if ($GLOBALS['is_ajax_request']) {
                $response = PMA_Response::getInstance();
                $response->isSuccess(false);
                $response->addJSON('message', $message);
                exit;
            } else {
                $message->display();
            }
        }
    }
}
 /**
  * Test for PMA_TRI_getEditorForm
  *
  * @param array $data    Data for trigger
  * @param array $matcher Matcher
  *
  * @return void
  *
  * @dataProvider providerAjax
  */
 public function testGetEditorFormAjax($data, $matcher)
 {
     $GLOBALS['is_ajax_request'] = true;
     $GLOBALS['server'] = 1;
     PMA_TRI_setGlobals();
     $this->assertContains($matcher, PMA_TRI_getEditorForm('edit', $data));
 }
 /**
  * Test for PMA_TRI_getEditorForm
  *
  * @param array $data    Data for trigger
  * @param array $matcher Matcher
  *
  * @return void
  *
  * @dataProvider providerAjax
  */
 public function testGetEditorFormAjax($data, $matcher)
 {
     $GLOBALS['server'] = 1;
     Response::getInstance()->setAjax(true);
     PMA_TRI_setGlobals();
     $this->assertContains(
         $matcher,
         PMA_TRI_getEditorForm('edit', $data)
     );
     Response::getInstance()->setAjax(false);
 }