root/afridex/plugins/Flutter/RCCWP_CustomField.php @ 21

Revision 21, 13.3 kB (checked in by admin, 18 years ago)
RevLine 
[21]1<?php
2
3/**
4 * @package FlutterDatabaseObjects
5 */
6
7/**
8 * Create/Edit/Delete custom fields
9 * @package FlutterDatabaseObjects
10 */
11
12class RCCWP_CustomField
13{
14        /**
15         * Create a new custom field
16         *
17         * @param id $customGroupId the id of the group that will contain the field
18         * @param string $name the name of the field, the name is used to uniquely identify the field
19         *                                                      when retrieving its value.
20         * @param string $label the label of the field, the label is displayed beside the field
21         *                                                      in Write tab.
22         * @param integer $order the order of the field when it is displayed in
23         *                                                      the Write tab.
24         * @param integer $required_field whether this field is a required field. Required fields
25         *                                                      doesn't allow users to save a post if they are null.
26         *
27         * @param integer $type the type of the field. Use $FIELD_TYPES defined in RCCWP_Constant.php
28         * @param array $options array of strings that represent the list of the field if
29         *                                                      its type is list.
30         * @param array $default_value array of strings that represent default value(s) of
31         *                                                      of the field if its type is list.
32         * @param array $properties an array containing extra properties of the field.
33         * @return the new field id
34         */
35        function Create($customGroupId, $name, $label, $order = 1, $required_field = 0, $type, $options = null, $default_value = null, $properties = null,$duplicate)
36        {
37                global $wpdb;
38
39                $name = stripslashes(stripslashes($name));
40                $name = addslashes($name);
41
42                $label = stripslashes(stripslashes($label));
43                $label = addslashes($label);
44
45                $sql = sprintf(
46                        "INSERT INTO " . RC_CWP_TABLE_GROUP_FIELDS .
47                        " (group_id, name, description, display_order, required_field, type, CSS, duplicate) values (%d, %s, %s, %d, %d, %d, %s, %d)",
48                        $customGroupId,
49                        RC_Format::TextToSql($name),
50                        RC_Format::TextToSql($label),
51                        $order,
52                        $required_field,
53                        $type,
54                        "'".$_POST['custom-field-css']."'",
55                        $duplicate
56                        );
57                $wpdb->query($sql);
58               
59                $customFieldId = $wpdb->insert_id;
60               
61                $field_type = RCCWP_CustomField::GetCustomFieldTypes($type);
62                if ($field_type->has_options == "true")
63                {
64                        if (!is_array($options)) {
65                                $options = stripslashes($options);
66                                $options = explode("\n", $options);
67                        }
68                        array_walk($options, array(RC_Format, TrimArrayValues));
69                        $options = addslashes(serialize($options));
70                       
71                        if (!is_array($default_value)) {
72                                $default_value = stripslashes($default_value);
73                                $default_value = explode("\n", $default_value);
74                        }
75                        array_walk($default_value, array(RC_Format, TrimArrayValues));
76                        $default_value = addslashes(serialize($default_value));
77                       
78                        $sql = sprintf(
79                                "INSERT INTO " . RC_CWP_TABLE_CUSTOM_FIELD_OPTIONS .
80                                " (custom_field_id, options, default_option) values (%d, %s, %s)",
81                                $customFieldId,
82                                RC_Format::TextToSql($options),
83                                RC_Format::TextToSql($default_value)
84                                );     
85                        $wpdb->query($sql);     
86                }
87               
88                if ($field_type->has_properties == "true")
89                {
90                        $sql = sprintf(
91                                "INSERT INTO " . RC_CWP_TABLE_CUSTOM_FIELD_PROPERTIES .
92                                " (custom_field_id, properties) values (%d, %s)",
93                                $customFieldId,
94                                RC_Format::TextToSql(serialize($properties))
95                                );     
96                        $wpdb->query($sql);     
97                }
98               
99                return $customFieldId;
100        }
101       
102        /**
103         * Delete a field
104         *
105         * @param integer $customFieldId field id
106         */
107        function Delete($customFieldId = null)
108        {
109                global $wpdb;
110               
111                $customField = RCCWP_CustomField::Get($customFieldId);
112               
113                $sql = sprintf(
114                        "DELETE FROM " . RC_CWP_TABLE_GROUP_FIELDS .
115                        " WHERE id = %d",
116                        $customFieldId
117                        );
118                $wpdb->query($sql);
119               
120                if ($customField->has_options == "true")
121                {
122                        $sql = sprintf(
123                                "DELETE FROM " . RC_CWP_TABLE_CUSTOM_FIELD_OPTIONS .
124                                " WHERE custom_field_id = %d",
125                                $customFieldId
126                                );     
127                        $wpdb->query($sql);     
128                }
129        }
130       
131        /**
132         * Get the field information including properties, options and default value(s)
133         *
134         * @param integer $customFieldId field id
135         * @return an object containing information about fields. The object contains
136         *                      3 objects: properties, options and default_value
137         */
138        function Get($customFieldId)
139        {
140                global $wpdb;
141                $sql = "SELECT cf.group_id, cf.id, cf.name, cf.CSS, tt.id AS type_id, tt.name AS type, cf.description, cf.display_order, cf.required_field, co.options, co.default_option AS default_value, tt.has_options, cp.properties, tt.has_properties, tt.allow_multiple_values, duplicate FROM " . RC_CWP_TABLE_GROUP_FIELDS .
142                        " cf LEFT JOIN " . RC_CWP_TABLE_CUSTOM_FIELD_OPTIONS . " co ON cf.id = co.custom_field_id" .
143                        " LEFT JOIN " . RC_CWP_TABLE_CUSTOM_FIELD_PROPERTIES . " cp ON cf.id = cp.custom_field_id" .
144                        " JOIN " . RC_CWP_TABLE_CUSTOM_FIELD_TYPES . " tt ON cf.type = tt.id" . 
145                        " WHERE cf.id = " . $customFieldId;
146                $results = $wpdb->get_row($sql);
147                       
148                $results->options = unserialize($results->options);
149                $results->properties = unserialize($results->properties);
150                $results->default_value = unserialize($results->default_value);
151                return $results;
152        }
153       
154        /**
155         * Retrievies information about a specified type
156         *
157         * @param integer $customFieldTypeId the type id, if null, a list of all types will be returned
158         * @return a list/object containing information about the specified type. The information
159         *                      includes id, name, description, has_options, has_properties, and
160         *                      allow_multiple_values (whether fields of that type can have more than one default value)
161         */
162        function GetCustomFieldTypes($customFieldTypeId = null)
163        {
164                global $wpdb;
165       
166                if (isset($customFieldTypeId))
167                {
168                        $sql = "SELECT id, name, description, has_options, has_properties, allow_multiple_values FROM " . RC_CWP_TABLE_CUSTOM_FIELD_TYPES .
169                                " WHERE id = " . (int)$customFieldTypeId;
170                        $results = $wpdb->get_row($sql);       
171                }
172                else
173                {
174                        $sql = "SELECT id, name, description, has_options, has_properties, allow_multiple_values FROM " . RC_CWP_TABLE_CUSTOM_FIELD_TYPES;
175                        $results = $wpdb->get_results($sql);
176                        if (!isset($results))
177                                $results = array();
178                }
179                return $results;
180        }
181
182        /**
183         * Retrieves the value of a custom field for a specified post
184         *
185         * @param integer $postId post id
186         * @param string $customFieldName field name
187         * @return a string containing field value
188         */
189        /*function GetCustomFieldValue($postId, $customFieldName)
190        {
191                // TODO Get custom field id
192                $customFieldId = 1;
193               
194                // TODO Given customFieldID, $groupIndex and $fieldIndex get meta_id
195                $fieldMetaID = 1;
196               
197                // use get_post_meta_by_id to get meta value
198               
199                return get_post_meta($postId, $customFieldName, true);
200        }*/
201       
202       
203        function GetMetaID($postId, $customFieldName, $groupIndex=1, $fieldIndex=1){
204                global $wpdb;
205               
206                // Given $postId, $customFieldName, $groupIndex and $fieldIndex get meta_id
207                return $wpdb->get_var("SELECT id FROM " . RC_CWP_TABLE_POST_META . 
208                                                " WHERE field_name = '$customFieldName' AND group_count = $groupIndex ". 
209                                                " AND field_count = $fieldIndex AND post_id = $postId" );
210               
211               
212        }
213       
214        /**
215         * Retrieves the value of a custom field for a specified post
216         *
217         * @param boolean $single
218         * @param integer $postId
219         * @param string $customFieldName
220         * @param integer $groupIndex
221         * @param integer $fieldIndex
222         * @return a
223         */
224        function GetCustomFieldValues($single, $postId, $customFieldName, $groupIndex=1, $fieldIndex=1)
225        {
226                global $wpdb;
227               
228                $fieldMetaID = RCCWP_CustomField::GetMetaID($postId, $customFieldName, $groupIndex, $fieldIndex);
229               
230                // for backward compatability, if no accociated row was found, use old method
231                if (!$fieldMetaID){
232                        //$customFieldName =  $wpdb->get_var("SELECT name FROM ". RC_CWP_TABLE_GROUP_FIELDS .
233                        //                                                                      " WHERE id = '$customFieldId'");
234                        return get_post_meta($postId, $customFieldName, $single);
235                }
236               
237                // Get meta value
238                $mid = (int) $fieldMetaID;
239                $meta = $wpdb->get_row( "SELECT * FROM $wpdb->postmeta WHERE meta_id = '$mid'" );
240                if (!$single) return unserialize($meta->meta_value);
241                return $meta->meta_value;
242        }
243       
244        /**
245         * Get number of group duplicates given field name. The function returns 1
246         * if there are no duplicates (just he original group), 2 if there is one
247         * duplicate and so on.
248         *
249         * @param integer $postId post id
250         * @param integer $fieldID the name of any field in the group
251         * @return number of groups
252         */
253        function GetFieldGroupDuplicates($postId, $fieldName){
254                global $wpdb;
255                return $wpdb->get_var("SELECT count(DISTINCT group_count) FROM " . RC_CWP_TABLE_POST_META . 
256                                                " WHERE field_name = '$fieldName' AND post_id = $postId");
257               
258        }
259
260        /**
261         * Get number of group duplicates given field name. The function returns 1
262         * if there are no duplicates (just he original group), 2 if there is one
263         * duplicate and so on.
264         *
265         * @param integer $postId post id
266         * @param integer $fieldID the name of any field in the group
267         * @return number of groups
268         */
269        function GetFieldDuplicates($postId, $fieldName, $groupIndex){
270                global $wpdb;
271                return $wpdb->get_var("SELECT count(DISTINCT field_count) FROM " . RC_CWP_TABLE_POST_META . 
272                                                " WHERE field_name = '$fieldName' AND post_id = $postId AND group_count = $groupIndex");
273               
274        }
275       
276        /**
277         * Retrieves the id of a custom field given field name for the current post.
278         *
279         * @param string $customFieldName
280         * @return custom field id
281         */
282        function GetIDByName($customFieldName)
283        {
284                global $wpdb, $post;
285               
286                // Get Panel ID
287                $customWritePanelId = get_post_meta($post->ID, RC_CWP_POST_WRITE_PANEL_ID_META_KEY, true);
288                if (empty($customWritePanelId)) return false;
289               
290                $customFieldId = $wpdb->get_var("SELECT cf.id FROM ". RC_CWP_TABLE_GROUP_FIELDS . " cf" .
291                                                                                " WHERE cf.name = '$customFieldName' AND ".
292                                                                                " cf.group_id in (SELECT mg.id FROM ". RC_CWP_TABLE_PANEL_GROUPS . " mg ".
293                                                                                                                "  WHERE mg.panel_id = $customWritePanelId)");
294                return $customFieldId;
295        }
296       
297       
298        /**
299         * @access private
300         */
301        function GetDefaultCustomFieldType()
302        {
303                return 'Textbox';
304        }
305       
306
307        /**
308         * Updates the properties of a custom field.
309         *
310         * @param integer $customFieldId the id of the field to be updated
311         * @param string $name the name of the field, the name is used to uniquely identify the field
312         *                                                      when retrieving its value.
313         * @param string $label the label of the field, the label is displayed beside the field
314         *                                                      in Write tab.
315         * @param integer $order the order of the field when it is displayed in
316         *                                                      the Write tab.
317         * @param integer $required_field whether this field is a required field. Required fields
318         *                                                      doesn't allow users to save a post if they are null.
319         * @param integer $type the type of the field. Use $FIELD_TYPES defined in RCCWP_Constant.php
320         * @param array $options array of strings that represent the list of the field if
321         *                                                      its type is list.
322         * @param array $default_value array of strings that represent default value(s) of
323         *                                                      of the field if its type is list.
324         * @param array $properties an array containing extra properties of the field.
325         */
326
327        function Update($customFieldId, $name, $label, $order = 1, $required_field = 0, $type, $options = null, $default_value = null, $properties = null, $duplicate)
328        {
329                global $wpdb;
330               
331                $oldCustomField = RCCWP_CustomField::Get($customFieldId);
332               
333                if ($oldCustomField->name != $name)
334                {
335                        $sql = sprintf(
336                                "UPDATE $wpdb->postmeta" .
337                                " SET meta_key = %s" .
338                                " WHERE meta_key = %s",
339                                RC_Format::TextToSql($name),
340                                RC_Format::TextToSql($oldCustomField->name)
341                                );
342                       
343                        $wpdb->query($sql);
344                }
345               
346                $sql = sprintf(
347                        "UPDATE " . RC_CWP_TABLE_GROUP_FIELDS .
348                        " SET name = %s" .
349                        " , description = %s" .
350                        " , display_order = %d" .
351                        " , required_field = %d" .
352                        " , type = %d" .
353                        " , CSS = '%s'" .
354                        " , duplicate = %d" .
355                        " WHERE id = %d",
356                        RC_Format::TextToSql($name),
357                        RC_Format::TextToSql($label),
358                        $order,
359                        $required_field,
360                        $type,
361                        $_POST['custom-field-css'],
362                        $duplicate,
363                        $customFieldId
364                        );
365                $wpdb->query($sql);
366
367
368                $field_type = RCCWP_CustomField::GetCustomFieldTypes($type);
369                if ($field_type->has_options == "true")
370                {
371                        if (!is_array($options)) {
372                                $options = stripslashes($options);
373                                $options = explode("\n", $options);
374                        }
375                        array_walk($options, array(RC_Format, TrimArrayValues));
376                        $options = addslashes(serialize($options));
377                       
378                        if (!is_array($default_value)) {
379                                $default_value = stripslashes($default_value);
380                                $default_value = explode("\n", $default_value);
381                        }
382                        array_walk($default_value, array(RC_Format, TrimArrayValues));
383                        $default_value = addslashes(serialize($default_value));
384                       
385                        $sql = sprintf(
386                                "INSERT INTO " . RC_CWP_TABLE_CUSTOM_FIELD_OPTIONS .
387                                " (custom_field_id, options, default_option) values (%d, %s, %s)" . 
388                                " ON DUPLICATE KEY UPDATE options = %s, default_option = %s",
389                                $customFieldId,
390                                RC_Format::TextToSql($options),
391                                RC_Format::TextToSql($default_value),
392                                RC_Format::TextToSql($options),
393                                RC_Format::TextToSql($default_value)
394                                );     
395                        $wpdb->query($sql);     
396                }
397                else
398                {
399                        $sql = sprintf(
400                                "DELETE FROM " . RC_CWP_TABLE_CUSTOM_FIELD_OPTIONS .
401                                " WHERE custom_field_id = %d",
402                                $customFieldId
403                                );
404                        $wpdb->query($sql);     
405                }
406               
407                if ($field_type->has_properties == "true")
408                {
409                        $sql = sprintf(
410                                "INSERT INTO " . RC_CWP_TABLE_CUSTOM_FIELD_PROPERTIES .
411                                " (custom_field_id, properties) values (%d, %s)" .
412                                " ON DUPLICATE KEY UPDATE properties = %s",
413                                $customFieldId,
414                                RC_Format::TextToSql(serialize($properties)),
415                                RC_Format::TextToSql(serialize($properties))
416                                );     
417                        $wpdb->query($sql);     
418                }
419                else
420                {
421                        $sql = sprintf(
422                                "DELETE FROM " . RC_CWP_TABLE_CUSTOM_FIELD_PROPERTIES .
423                                " WHERE custom_field_id = %d",
424                                $customFieldId
425                                );
426                        $wpdb->query($sql);     
427                }
428        }
429}
430?>
Note: See TracBrowser for help on using the browser.