Exemplo n.º 1
0
/**
 * Import canned messages from a YAML file.
 *
 * @param string $locale Locale code.
 * @param string $file Full path to the file that should be imported.
 */
function import_canned_messages($locale, $file)
{
    // Get new canned messages that should be imported.
    $parser = new YamlParser();
    $new_messages = $parser->parse(file_get_contents($file));
    if (empty($new_messages)) {
        // Nothing to import.
        return;
    }
    // Get list of existing canned messages for specified locale an key it by
    // message value.
    $loaded_messages = load_canned_messages($locale, false);
    $existing_messages = array();
    if (!empty($loaded_messages)) {
        foreach ($loaded_messages as $message) {
            $existing_messages[$message['vcvalue']] = $message;
        }
    }
    // Save only new messages to avoid duplication
    foreach ($new_messages as $message) {
        if (array_key_exists($message, $existing_messages)) {
            continue;
        }
        add_canned_message($locale, null, cut_string($message, 97, '...'), $message);
    }
}
    /**
     * Processes submitting of the forms which is generated in
     * {@link \Mibew\Controller\CannedMessageController::showEditFormAction()}
     * method.
     *
     * @param Request $request
     * @return string Rendered page content
     */
    public function submitEditFormAction(Request $request)
    {
        csrf_check_token($request);

        $operator = $this->getOperator();
        $message_id = $request->attributes->getInt('message_id');
        $errors = array();

        $title = $request->request->get('title');
        if (!$title) {
            $errors[] = no_field("Title");
        }

        $message = $request->request->get('message');
        if (!$message) {
            $errors[] = no_field("Message");
        }

        if (count($errors) != 0) {
            $request->attributes->set('errors', $errors);

            // The form should be rebuild. Invoke appropriate action.
            return $this->showEditFormAction($request);
        }

        if ($message_id) {
            save_canned_message($message_id, $title, $message);
        } else {
            $locale = $this->extractLocale($request);
            $group_id = $this->extractGroupId($request);
            add_canned_message($locale, $group_id, $title, $message);
        }
        $page['saved'] = true;
        $page = array_merge($page, prepare_menu($operator, false));

        return $this->render('canned_message_edit', $page);
    }