summaryrefslogtreecommitdiff
path: root/app/functions.php
blob: c39eaf98b9dc040d9615aee82a89f745896dced5 (plain)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php

use Core\Event;
use Core\Translator;
use PicoDb\Database;

/**
 * Send a debug message to the log files
 *
 * @param mixed $message Variable or string
 */
function debug($message)
{
    if (! is_string($message)) {
        $message = var_export($message, true);
    }

    error_log($message.PHP_EOL, 3, 'data/debug.log');
}

/**
 * Setup events
 *
 * @return Core\Event
 */
function setup_events()
{
    return new Event;
}

/**
 * Setup the mailer according to the configuration
 *
 * @return Swift_SmtpTransport
 */
function setup_mailer()
{
    require_once __DIR__.'/../vendor/swiftmailer/swift_required.php';

    switch (MAIL_TRANSPORT) {
        case 'smtp':
            $transport = Swift_SmtpTransport::newInstance(MAIL_SMTP_HOSTNAME, MAIL_SMTP_PORT);
            $transport->setUsername(MAIL_SMTP_USERNAME);
            $transport->setPassword(MAIL_SMTP_PASSWORD);
            $transport->setEncryption(MAIL_SMTP_ENCRYPTION);
            break;
        case 'sendmail':
            $transport = Swift_SendmailTransport::newInstance(MAIL_SENDMAIL_COMMAND);
            break;
        default:
            $transport = Swift_MailTransport::newInstance();
    }

    return $transport;
}

/**
 * Setup the database driver and execute schema migration
 *
 * @return PicoDb\Database
 */
function setup_db()
{
    switch (DB_DRIVER) {
        case 'sqlite':
            $db = setup_sqlite();
            break;

        case 'mysql':
            $db = setup_mysql();
            break;

        case 'postgres':
            $db = setup_postgres();
            break;

        default:
            die('Database driver not supported');
    }

    if ($db->schema()->check(Schema\VERSION)) {
        return $db;
    }
    else {
        $errors = $db->getLogMessages();
        die('Unable to migrate database schema: <br/><br/><strong>'.(isset($errors[0]) ? $errors[0] : 'Unknown error').'</strong>');
    }
}

/**
 * Setup the Sqlite database driver
 *
 * @return PicoDb\Database
 */
function setup_sqlite()
{
    require_once __DIR__.'/Schema/Sqlite.php';

    return new Database(array(
        'driver' => 'sqlite',
        'filename' => DB_FILENAME
    ));
}

/**
 * Setup the Mysql database driver
 *
 * @return PicoDb\Database
 */
function setup_mysql()
{
    require_once __DIR__.'/Schema/Mysql.php';

    return new Database(array(
        'driver'   => 'mysql',
        'hostname' => DB_HOSTNAME,
        'username' => DB_USERNAME,
        'password' => DB_PASSWORD,
        'database' => DB_NAME,
        'charset'  => 'utf8',
    ));
}

/**
 * Setup the Postgres database driver
 *
 * @return PicoDb\Database
 */
function setup_postgres()
{
    require_once __DIR__.'/Schema/Postgres.php';

    return new Database(array(
        'driver'   => 'postgres',
        'hostname' => DB_HOSTNAME,
        'username' => DB_USERNAME,
        'password' => DB_PASSWORD,
        'database' => DB_NAME,
    ));
}

/**
 * Translate a string
 *
 * @return string
 */
function t()
{
    $t = new Translator;
    return call_user_func_array(array($t, 'translate'), func_get_args());
}

/**
 * Translate a string with no HTML escaping
 *
 * @return string
 */
function e()
{
    $t = new Translator;
    return call_user_func_array(array($t, 'translateNoEscaping'), func_get_args());
}

/**
 * Translate a currency
 *
 * @return string
 */
function c($value)
{
    $t = new Translator;
    return $t->currency($value);
}

/**
 * Translate a number
 *
 * @return string
 */
function n($value)
{
    $t = new Translator;
    return $t->number($value);
}

/**
 * Translate a date
 *
 * @return string
 */
function dt($format, $timestamp)
{
    $t = new Translator;
    return $t->datetime($format, $timestamp);
}

/**
 * Handle plurals, return $t2 if $value > 1
 *
 * @todo   Improve this function
 * @return mixed
 */
function p($value, $t1, $t2) {
    return $value > 1 ? $t2 : $t1;
}