1<?php
2/**
3 * Imple to allow in-place editing of a HTML element.
4 *
5 * Copyright 2012-2017 Horde LLC (http://www.horde.org/)
6 *
7 * See the enclosed file COPYING for license information (LGPL). If you
8 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
9 *
10 * @author   Michael J. Rubinsky <mrubinsk@horde.org>
11 * @author   Michael Slusarz <slusarz@horde.org>
12 * @category Horde
13 * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
14 * @package  Core
15 */
16abstract class Horde_Core_Ajax_Imple_InPlaceEditor extends Horde_Core_Ajax_Imple
17{
18    /**
19     * @param array $params  Configuration parameters:
20     *   - cols: (integer) Number of columns.
21     *   - dataid: (string) Data ID passed to the handler.
22     *   - rows: (integer) Number of rows.
23     */
24    public function __construct(array $params = array())
25    {
26        /* Set up some defaults */
27        $params = array_merge(array(
28            'cols' => 20,
29            'dataid' => '',
30            'rows' => 2
31        ), $params);
32
33        parent::__construct($params);
34    }
35
36    /**
37     */
38    protected function _attach($init)
39    {
40        global $page_output;
41
42        if ($init) {
43            $page_output->addScriptFile('scriptaculous/effects.js', 'horde');
44            $page_output->addScriptFile('inplaceeditor.js', 'horde');
45
46            $value_url = $this->getImpleUrl()->add(array(
47                'id' => $this->_params['dataid'],
48                'input' => 'value'
49            ))->setRaw(true);
50            $load_url = $value_url->copy()->add(array(
51                'action' => 'load'
52            ))->setRaw(true);
53            $config = new stdClass;
54            $config->config = array(
55                'cancelClassName' => '',
56                'cancelText' => Horde_Core_Translation::t("Cancel"),
57                'emptyText' => Horde_Core_Translation::t("Click to add caption..."),
58                'okText' => Horde_Core_Translation::t("Ok")
59            );
60            $config->ids = new stdClass;
61            $config->ids->{$this->getDomId()} = array(
62                'load_url' => (string)$load_url,
63                'rows' => $this->_params['rows'],
64                'value_url' => (string)$value_url
65            );
66
67            if (!empty($this->_params['width'])) {
68                $config->ids->{$this->getDomId()}['width'] = $this->_params['width'];
69            }
70
71            $page_output->addInlineJsVars(array(
72                'HordeImple.InPlaceEditor' . $this->getDomId() => $config
73            ));
74            $page_output->addInlineScript(array(
75                '$H(HordeImple.InPlaceEditor' . $this->getDomId() . '.ids).each(function(pair) {
76                     new InPlaceEditor(pair.key, pair.value.value_url, Object.extend(HordeImple.InPlaceEditor' . $this->getDomId() . '.config, {
77                         htmlResponse: false,
78                         callback: function(form, value) {
79                             return "value=" + encodeURIComponent(value);
80                         },
81                         onComplete: function(ipe, opts) {
82                            if (opts) {
83                                $("' . $this->getDomId() . '").update(opts.responseJSON)
84                            }
85                             ipe.checkEmpty()
86                         },
87                         loadTextURL: pair.value.load_url,
88                         rows: pair.value.rows,
89                         autoWidth: true
90                     }));
91                 })'
92            ), true);
93        }
94
95        return false;
96    }
97
98    /**
99     */
100    protected function _handle(Horde_Variables $vars)
101    {
102        $data = (!$vars->load && (!isset($vars->input) || !isset($vars->id)))
103            ? ''
104            : $this->_handleEdit($vars);
105
106        return new Horde_Core_Ajax_Response_Prototypejs($data);
107    }
108
109    /**
110     * @return mixed  Raw data to return to in-place-editor.
111     */
112    abstract protected function _handleEdit(Horde_Variables $vars);
113
114}
115