File: Util.php

package info (click to toggle)
php-horde-ingo 3.2.16-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 5,428 kB
  • sloc: php: 9,583; xml: 4,072; javascript: 184; makefile: 19; sh: 3
file content (124 lines) | stat: -rw-r--r-- 4,033 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
<?php
/**
 * Copyright 2014-2017 Horde LLC (http://www.horde.org/)
 *
 * See the enclosed file LICENSE for license information (ASL).  If you
 * did not receive this file, see http://www.horde.org/licenses/apache.
 *
 * @category  Horde
 * @copyright 2014-2017 Horde LLC
 * @license   http://www.horde.org/licenses/apache ASL
 * @package   Ingo
 */

/**
 * Provides utility functions for manipulating Ingo scripts.
 *
 * @author    Michael Slusarz <slusarz@horde.org>
 * @category  Horde
 * @copyright 2014-2017 Horde LLC
 * @license   http://www.horde.org/licenses/apache ASL
 * @package   Ingo
 */
class Ingo_Script_Util
{
    /**
     * Connects to the backend, uploads the scripts and sets them active.
     *
     * @param array $scripts       A list of scripts to set active.
     * @param boolean $deactivate  If true, notification will identify the
     *                             script as deactivated instead of activated.
     *
     * @throws Ingo_Exception
     */
    static public function activate($scripts, $deactivate = false)
    {
        global $injector, $notification;

        foreach ($scripts as $script) {
            if ($deactivate) {
                $script['script'] = '';
            }
            try {
                $injector->getInstance('Ingo_Factory_Transport')
                    ->create($script['transport'])
                    ->setScriptActive($script);
            } catch (Ingo_Exception $e) {
                $msg = $deactivate
                  ? _("There was an error deactivating the script.")
                  : _("There was an error activating the script.");
                throw new Ingo_Exception(
                    sprintf(_("%s The driver said: %s"), $msg, $e->getMessage())
                );
            }
        }

        $msg = $deactivate
            ? _("Script successfully deactivated.")
            : _("Script successfully activated.");
        $notification->push($msg, 'horde.success');
    }

    /**
     * Does all the work in updating the script on the server.
     *
     * @param boolean $auto_update  Only update if auto_update is active?
     *
     * @throws Ingo_Exception
     */
    static public function update($auto_update = true)
    {
        global $injector, $prefs;

        if ($auto_update && !$prefs->getValue('auto_update')) {
            return;
        }

        foreach ($injector->getInstance('Ingo_Factory_Script')->createAll() as $script) {
            if ($script->hasFeature('script_file')) {
                try {
                    /* Generate and activate the script. */
                    self::activate($script->generate());
                } catch (Ingo_Exception $e) {
                    throw new Ingo_Exception(
                        sprintf(_("Script not updated: %s"), $e->getMessage())
                    );
                }
            }
        }
    }

    /**
     * Returns the vacation reason with all placeholder replaced.
     *
     * @param string $reason  The vacation reason including placeholders.
     * @param integer $start  The vacation start timestamp.
     * @param integer $end    The vacation end timestamp.
     *
     * @return string  The vacation reason suitable for usage in the filter
     *                 scripts.
     */
    static public function vacationReason($reason, $start, $end)
    {
        global $injector, $prefs;

        $format = $prefs->getValue('date_format');
        $identity = $injector->getInstance('Horde_Core_Factory_Identity')
            ->create(Ingo::getUser());

        $replace = array(
            '%NAME%' => $identity->getName(),
            '%EMAIL%' => $identity->getDefaultFromAddress(),
            '%SIGNATURE%' => $identity->getValue('signature'),
            '%STARTDATE%' => $start ? strftime($format, $start) : '',
            '%ENDDATE%' => $end ? strftime($format, $end) : ''
        );

        return str_replace(
            array_keys($replace),
            array_values($replace),
            $reason
        );
    }

}