Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
 
4
/**
5
 * Contains the Translation2_Admin_Container_dataobjectsimple class
6
 *
7
 * PHP versions 4 and 5
8
 *
9
 * LICENSE: Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 * 3. The name of the author may not be used to endorse or promote products
17
 *    derived from this software without specific prior written permission.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
20
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22
 * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
23
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * @category  Internationalization
31
 * @package   Translation2
32
 * @author    Alan Knowles <alan@akbkhome.com>
33
 * @copyright 2004-2007 Alan Knowles
34
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
35
 * @version   CVS: $Id: dataobjectsimple.php 305985 2010-12-05 22:55:33Z clockwerx $
36
 * @link      http://pear.php.net/package/Translation2
37
 */
38
 
39
/**
40
 * require Translation2_Container_dataobjectsimple class
41
 */
42
require_once 'Translation2/Container/dataobjectsimple.php';
43
 
44
/**
45
 * Storage driver for storing/fetching data to/from a database
46
 *
47
 * This storage driver can use all databases which are supported
48
 * by PEAR::DB_DataObject to fetch data.
49
 *
50
 * Database Structure:
51
 *
52
 * <pre>
53
 * // meta data etc. not supported
54
 *
55
 * table: translations
56
 *  id          // not null primary key autoincrement..
57
 *  string_id   // translation id
58
 *  page        // indexed varchar eg. (mytemplate.html)
59
 *  lang        // index varchar (eg. en|fr|.....)
60
 *  translation // the translated value in language lang.
61
 * </pre>
62
 *
63
 * @category  Internationalization
64
 * @package   Translation2
65
 * @author    Alan Knowles <alan@akbkhome.com>
66
 * @copyright 2004-2007 Alan Knowles
67
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
68
 * @link      http://pear.php.net/package/Translation2
69
 */
70
class Translation2_Admin_Container_dataobjectsimple extends Translation2_Container_dataobjectsimple
71
{
72
    // {{{ addLang()
73
 
74
    /**
75
     * Creates a new table to store the strings in this language.
76
     * If the table is shared with other langs, it is ALTERed to
77
     * hold strings in this lang too.
78
     *
79
     * @param array $langData array('lang_id'    => 'en',
80
     *                              'table_name' => 'i18n',
81
     *                              'name'       => 'english',
82
     *                              'meta'       => 'some meta info',
83
     *                              'error_text' => 'not available');
84
     * @param array $options  DB_DataObject options
85
     *
86
     * @return true|PEAR_Error
87
     */
88
    function addLang($langData, $options = array())
89
    {
90
        $do = DB_DataObject::factory($this->options['table']);
91
        $do->lang = $langData['lang_id'];
92
        if (!$do->find()) {
93
            $do->insert();
94
        }
95
    }
96
 
97
    // }}}
98
    // {{{ addLangToList()
99
 
100
    /**
101
     * Creates a new entry in the langsAvail table.
102
     * If the table doesn't exist yet, it is created.
103
     *
104
     * @param array $langData array('lang_id'    => 'en',
105
     *                              'table_name' => 'i18n',
106
     *                              'name'       => 'english',
107
     *                              'meta'       => 'some meta info',
108
     *                              'error_text' => 'not available');
109
     *
110
     * @return true|PEAR_Error
111
     */
112
    function addLangToList($langData)
113
    {
114
        return true;
115
    }
116
 
117
    // }}}
118
    // {{{ add()
119
 
120
    /**
121
     * Add a new entry in the strings table.
122
     *
123
     * @param string $string      string
124
     * @param string $pageID      page/group ID
125
     * @param array  $stringArray Associative array with string translations.
126
     *               Sample format:  array('en' => 'sample', 'it' => 'esempio')
127
     *
128
     * @return true|PEAR_Error
129
     */
130
    function add($string, $pageID, $stringArray)
131
    {
132
        //look up the string id first..
133
        $do = DB_DataObject::factory($this->options['table']);
134
        $do->lang = '-';
135
        $do->translation = $string;
136
        $do->page = $page;
137
        if ($do->find(true)) {
138
            $stringID = $do->string_id;
139
        } else {
140
            // insert it and use the 'id' as the string id
141
            $stringID = $do->insert();
142
            $do->string_id = $stringID;
143
            $do->update();
144
        }
145
 
146
        foreach ($stringArray as $lang=>$value) {
147
            $do = DB_DataObject::factory($this->options['table']);
148
            $do->string_id = $stringID;
149
            $do->page      = $pageID;
150
            $do->lang      = $lang;
151
            if ($do->find(true)) {
152
                $do->translation = $value;
153
                $do->update();
154
                continue;
155
            }
156
            $do->translation = $value;
157
            $do->insert();
158
        }
159
 
160
        return true;
161
    }
162
 
163
    // }}}
164
    // {{{ update()
165
 
166
    /**
167
     * Update an existing entry in the strings table.
168
     *
169
     * @param string $stringID    string ID
170
     * @param string $pageID      page/group ID
171
     * @param array  $stringArray Associative array with string translations.
172
     *               Sample format: array('en' => 'sample', 'it' => 'esempio')
173
     *
174
     * @return true|PEAR_Error
175
     */
176
    function update($stringID, $pageID, $stringArray)
177
    {
178
        $this->add($stringID, $pageID, $stringArray);
179
        return true;
180
    }
181
 
182
    // }}}
183
    // {{{ remove()
184
 
185
    /**
186
     * Remove an entry from the strings table.
187
     *
188
     * @param string $stringID string ID
189
     * @param string $pageID   page/group ID
190
     *
191
     * @return true|PEAR_Error
192
     */
193
    function remove($stringID, $pageID)
194
    {
195
        // get the string id
196
        $do = DB_DataObject::factory($this->options['table']);
197
        $do->page = $pageID;
198
        $do->translation = $stringID;
199
        // we don't have the base language translation..
200
        if (!$do->find()) {
201
            return '';
202
        }
203
 
204
        while ($do->fetch()) {
205
            $do2 = DB_DataObject::factory($this->options['table']);
206
            $do2->get($do->id);
207
            $do2->delete();
208
        }
209
        return true;
210
    }
211
 
212
    // }}}
213
    // {{{ removePage()
214
 
215
    /**
216
     * Remove all the strings in the given page/group
217
     *
218
     * @param string $pageID page/group ID
219
     *
220
     * @return mixed true on success, PEAR_Error on failure
221
     */
222
    function removePage($pageID = null)
223
    {
224
        $do = DB_DataObject::factory($this->options['table']);
225
        $do->page = $pageID;
226
        // we don't have the base language translation..
227
        if (!$do->find()) {
228
            return '';
229
        }
230
 
231
        while ($do->fetch()) {
232
            $do2 = DB_DataObject::factory($this->options['table']);
233
            $do2->get($do->id);
234
            $do2->delete();
235
        }
236
        return true;
237
    }
238
 
239
    // }}}
240
}
241
?>