Translation update done using Pootle.
[phpmyadmin/ammaryasirr.git] / libraries / transformations.lib.php
bloba5460ab165250a66eccbe7e405fa4935858c4a5c
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Set of functions used with the relation and pdf feature
6 * @package phpMyAdmin
7 */
9 /**
10 * returns array of options from string with options separated by comma, removes quotes
12 * <code>
13 * PMA_transformation_getOptions("'option ,, quoted',abd,'2,3',");
14 * // array {
15 * // 'option ,, quoted',
16 * // 'abc',
17 * // '2,3',
18 * // '',
19 * // }
20 * </code>
22 * @param string $option_string comma separated options
23 * @return array options
25 function PMA_transformation_getOptions($option_string)
27 $result = array();
29 if (! strlen($option_string)
30 || ! $transform_options = preg_split('/,/', $option_string)) {
31 return $result;
34 while (($option = array_shift($transform_options)) !== null) {
35 $trimmed = trim($option);
36 if (strlen($trimmed) > 1
37 && $trimmed[0] == "'"
38 && $trimmed[strlen($trimmed) - 1] == "'") {
39 // '...'
40 $option = substr($trimmed, 1, -1);
41 } elseif (isset($trimmed[0]) && $trimmed[0] == "'") {
42 // '...,
43 $trimmed = ltrim($option);
44 while (($option = array_shift($transform_options)) !== null) {
45 // ...,
46 $trimmed .= ',' . $option;
47 $rtrimmed = rtrim($trimmed);
48 if ($rtrimmed[strlen($rtrimmed) - 1] == "'") {
49 // ,...'
50 break;
53 $option = substr($rtrimmed, 1, -1);
55 $result[] = stripslashes($option);
58 return $result;
61 /**
62 * Gets all available MIME-types
64 * @access public
65 * @staticvar array mimetypes
66 * @return array array[mimetype], array[transformation]
68 function PMA_getAvailableMIMEtypes()
70 static $stack = null;
72 if (null !== $stack) {
73 return $stack;
76 $stack = array();
77 $filestack = array();
79 $handle = opendir('./libraries/transformations');
81 if (! $handle) {
82 return $stack;
85 while ($file = readdir($handle)) {
86 $filestack[] = $file;
89 closedir($handle);
90 sort($filestack);
92 foreach ($filestack as $file) {
93 if (preg_match('|^.*__.*\.inc\.php$|', $file)) {
94 // File contains transformation functions.
95 $base = explode('__', str_replace('.inc.php', '', $file));
96 $mimetype = str_replace('_', '/', $base[0]);
97 $stack['mimetype'][$mimetype] = $mimetype;
99 $stack['transformation'][] = $mimetype . ': ' . $base[1];
100 $stack['transformation_file'][] = $file;
102 } elseif (preg_match('|^.*\.inc\.php$|', $file)) {
103 // File is a plain mimetype, no functions.
104 $base = str_replace('.inc.php', '', $file);
106 if ($base != 'global') {
107 $mimetype = str_replace('_', '/', $base);
108 $stack['mimetype'][$mimetype] = $mimetype;
109 $stack['empty_mimetype'][$mimetype] = $mimetype;
114 return $stack;
118 * Gets the mimetypes for all columns of a table
120 * @access public
121 * @param string $db the name of the db to check for
122 * @param string $table the name of the table to check for
123 * @param string $strict whether to include only results having a mimetype set
124 * @return array [field_name][field_key] = field_value
126 function PMA_getMIME($db, $table, $strict = false)
128 $cfgRelation = PMA_getRelationsParam();
130 if (! $cfgRelation['commwork']) {
131 return false;
134 $com_qry = '
135 SELECT `column_name`,
136 `mimetype`,
137 `transformation`,
138 `transformation_options`
139 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
140 WHERE `db_name` = \'' . PMA_sqlAddSlashes($db) . '\'
141 AND `table_name` = \'' . PMA_sqlAddSlashes($table) . '\'
142 AND ( `mimetype` != \'\'' . (!$strict ? '
143 OR `transformation` != \'\'
144 OR `transformation_options` != \'\'' : '') . ')';
145 return PMA_DBI_fetch_result($com_qry, 'column_name', null, $GLOBALS['controllink']);
146 } // end of the 'PMA_getMIME()' function
149 * Set a single mimetype to a certain value.
151 * @access public
152 * @param string $db the name of the db
153 * @param string $table the name of the table
154 * @param string $key the name of the column
155 * @param string $mimetype the mimetype of the column
156 * @param string $transformation the transformation of the column
157 * @param string $transformation_options the transformation options of the column
158 * @param string $forcedelete force delete, will erase any existing comments for this column
159 * @return boolean true, if comment-query was made.
161 function PMA_setMIME($db, $table, $key, $mimetype, $transformation,
162 $transformation_options, $forcedelete = false)
164 $cfgRelation = PMA_getRelationsParam();
166 if (! $cfgRelation['commwork']) {
167 return false;
170 $test_qry = '
171 SELECT `mimetype`,
172 `comment`
173 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
174 WHERE `db_name` = \'' . PMA_sqlAddSlashes($db) . '\'
175 AND `table_name` = \'' . PMA_sqlAddSlashes($table) . '\'
176 AND `column_name` = \'' . PMA_sqlAddSlashes($key) . '\'';
177 $test_rs = PMA_query_as_controluser($test_qry, true, PMA_DBI_QUERY_STORE);
179 if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) {
180 $row = @PMA_DBI_fetch_assoc($test_rs);
181 PMA_DBI_free_result($test_rs);
183 if (! $forcedelete
184 && (strlen($mimetype) || strlen($transformation)
185 || strlen($transformation_options) || strlen($row['comment']))) {
186 $upd_query = '
187 UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
188 SET `mimetype` = \'' . PMA_sqlAddSlashes($mimetype) . '\',
189 `transformation` = \'' . PMA_sqlAddSlashes($transformation) . '\',
190 `transformation_options` = \'' . PMA_sqlAddSlashes($transformation_options) . '\'';
191 } else {
192 $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']);
194 $upd_query .= '
195 WHERE `db_name` = \'' . PMA_sqlAddSlashes($db) . '\'
196 AND `table_name` = \'' . PMA_sqlAddSlashes($table) . '\'
197 AND `column_name` = \'' . PMA_sqlAddSlashes($key) . '\'';
198 } elseif (strlen($mimetype) || strlen($transformation)
199 || strlen($transformation_options)) {
200 $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info'])
201 . ' (db_name, table_name, column_name, mimetype, transformation, transformation_options) '
202 . ' VALUES('
203 . '\'' . PMA_sqlAddSlashes($db) . '\','
204 . '\'' . PMA_sqlAddSlashes($table) . '\','
205 . '\'' . PMA_sqlAddSlashes($key) . '\','
206 . '\'' . PMA_sqlAddSlashes($mimetype) . '\','
207 . '\'' . PMA_sqlAddSlashes($transformation) . '\','
208 . '\'' . PMA_sqlAddSlashes($transformation_options) . '\')';
211 if (isset($upd_query)) {
212 return PMA_query_as_controluser($upd_query);
213 } else {
214 return false;
216 } // end of 'PMA_setMIME()' function