root/afridex/plugins/Flutter/RCCWP_CustomWritePanel.php @ 23

Revision 21, 15.9 kB (checked in by admin, 18 years ago)
Line 
1<?php
2
3/**
4 * @package FlutterDatabaseObjects
5 */
6
7/**
8 * Create/edit/delete write panels.
9 * @package FlutterDatabaseObjects
10 */
11class RCCWP_CustomWritePanel
12{
13
14        /**
15         * Get all Write Panels.
16         *
17         * @return array of objects containing all write panels. Each object contains
18         *                      id, name, description, display_order, capability_name, type, always_show
19         */
20        function GetCustomWritePanels()
21        {
22                global $wpdb;
23       
24                $sql = "SELECT id, name, description, display_order, capability_name, type FROM " . RC_CWP_TABLE_PANELS;
25                       
26                $sql .= " ORDER BY display_order ASC";
27                $results = $wpdb->get_results($sql);
28                if (!isset($results)) 
29                        $results = array();
30       
31                return $results;
32        }
33       
34        /**
35         * Assign a specified write panel to a role.
36         *
37         * @param integer $customWritePanelId panel id
38         * @param string $roleName role name (see roles in wordpress)
39         */
40        function AssignToRole($customWritePanelId, $roleName)
41        {
42                $customWritePanel = RCCWP_CustomWritePanel::Get($customWritePanelId);
43                $capabilityName = $customWritePanel->capability_name;
44                $role = get_role($roleName);
45                $role->add_cap($capabilityName);       
46        }
47       
48        /**
49         * Create a new write panel.
50         *
51         * @param string $name write panel name
52         * @param string $description write panel description
53         * @param array $standardFields a list of standard fields ids that are to be displayed in
54         *                                                      in the panel. Use $STANDARD_FIELDS defined in RCCWP_Constant.php
55         * @param array $categories array of category ids that are checked by default when the user
56         *                                                      opens Write tab for that panel.
57         * @param integer $display_order the order of the panel in Flutter > Write Panels tab
58         * @param string $type 'post' or 'page'
59         * @param boolean $createDefaultGroup indicates whether to create a default group.
60         * @return the id of the write panel
61         */
62        function Create($name, $description = '', $standardFields = array(), $categories = array(), $display_order = 1, $type = FALSE, $createDefaultGroup=true)
63        {
64                include_once('RC_Format.php');
65                global $wpdb;
66
67                $capabilityName = RCCWP_CustomWritePanel::GetCapabilityName($name);
68                if (!$type) $type = $_POST['radPostPage'];
69                $sql = sprintf(
70                        "INSERT INTO " . RC_CWP_TABLE_PANELS .
71                        " (name, description, display_order, capability_name, type)" .
72                        " values" .
73                        " (%s, %s, %d, %s, %s)", 
74                        RC_Format::TextToSql($name), 
75                        RC_Format::TextToSql($description),
76                        $display_order,
77                        RC_Format::TextToSql($capabilityName),
78                        RC_Format::TextToSql($type)     
79                );
80               
81                $wpdb->query($sql);
82                $customWritePanelId = $wpdb->insert_id;
83               
84                if (!isset($categories))
85                        $categories = array();
86                foreach ($categories as $cat_id)
87                {
88                        $sql = sprintf(
89                                "INSERT INTO " . RC_CWP_TABLE_PANEL_CATEGORY .
90                                " (panel_id, cat_id)" .
91                                " values (%d, %d)",
92                                $customWritePanelId,
93                                $cat_id 
94                                );
95                        $wpdb->query($sql);
96                }
97               
98                if (!isset($standardFields))
99                        $standardFields = array();
100                foreach ($standardFields as $standard_field_id)
101                {
102                        $sql = sprintf(
103                                "INSERT INTO " . RC_CWP_TABLE_PANEL_STANDARD_FIELD .
104                                " (panel_id, standard_field_id)" .
105                                " values (%d, %d)",
106                                $customWritePanelId,
107                                $standard_field_id 
108                                );
109                        $wpdb->query($sql);
110                }
111                               
112                // Create default group
113                if ($createDefaultGroup){
114                        include_once('RCCWP_CustomGroup.php');
115                        RCCWP_CustomGroup::Create($customWritePanelId, '__default', false, false);
116                }
117               
118                RCCWP_CustomWritePanel::AssignToRole($customWritePanelId, 'administrator');
119               
120                return $customWritePanelId;
121        }
122       
123        /**
124         * Delete a write panel without deleting its modules
125         *
126         * @param integer $customWritePanelId write panel id
127         */
128        function Delete($customWritePanelId = null)
129        {
130                if (isset($customWritePanelId))
131                {
132                        global $wpdb;
133                       
134                        $customWritePanel = RCCWP_CustomWritePanel::Get($customWritePanelId);
135                                               
136                        $sql = sprintf(
137                                "DELETE FROM " . RC_CWP_TABLE_PANELS .
138                                " WHERE id = %d",
139                                $customWritePanel->id
140                                );
141                        $wpdb->query($sql);
142                       
143                        $sql = sprintf(
144                                "DELETE FROM " . RC_CWP_TABLE_PANEL_CATEGORY .
145                                " WHERE panel_id = %d",
146                                $customWritePanel->id
147                                );
148                        $wpdb->query($sql);
149                       
150                        $sql = sprintf(
151                                "DELETE FROM " . RC_CWP_TABLE_PANEL_STANDARD_FIELD .
152                                " WHERE panel_id = %d",
153                                $customWritePanelId
154                                );
155                        $wpdb->query($sql);
156                       
157                }
158        }
159       
160        /**
161         * Get the properties of a write panel
162         *
163         * @param unknown_type $customWritePanelId
164         * @return an object containing the properties of the write panel which are
165         *                      id, name, description, display_order, capability_name, type
166         */
167        function Get($customWritePanelId)
168        {
169                global $wpdb;
170       
171                $sql = "SELECT id, name, description, display_order, capability_name, type FROM " . RC_CWP_TABLE_PANELS .
172                        " WHERE id = " . (int)$customWritePanelId;
173               
174                $results = $wpdb->get_row($sql);
175               
176                return $results;
177        }
178       
179        /**
180         * Get a list of the ids of teh categories assigned to  a write panel
181         *
182         * @param integer $customWritePanelId write panel id
183         * @return array of ids
184         */
185        function GetAssignedCategoryIds($customWritePanelId)
186        {
187                $results = RCCWP_CustomWritePanel::GetAssignedCategories($customWritePanelId);
188                $ids = array();
189                foreach ($results as $r)
190                {
191                        $ids[] = $r->cat_id;
192                }
193               
194                return $ids;
195        }
196       
197        /**
198         * Get a list of categories assigned to a write panel
199         *
200         * @param integer $customWritePanelId write panel id
201         * @return array of objects, each object contains cat_id and cat_name
202         */
203        function GetAssignedCategories($customWritePanelId)
204        {
205                global $wpdb;
206                /*
207                        $sql = "SELECT rc.cat_id, cat_name FROM " . RC_CWP_TABLE_PANEL_CATEGORY .
208                        " rc JOIN $wpdb->categories wp ON rc.cat_ID = wp.cat_ID" .
209                        " WHERE panel_id = " . $customWritePanelId;
210                */
211               
212                if( $wpdb->terms != '' )
213                {
214                        $sql = "SELECT rc.cat_id, wp.name AS cat_name FROM " . 
215                                RC_CWP_TABLE_PANEL_CATEGORY . "
216                                rc JOIN $wpdb->terms wp ON rc.cat_ID = wp.term_id" . "
217                                WHERE panel_id = " . $customWritePanelId;
218                }
219                else
220                {
221                        $sql = "SELECT rc.cat_id, cat_name FROM " . 
222                                RC_CWP_TABLE_PANEL_CATEGORY . "
223                                rc JOIN $wpdb->categories wp ON rc.cat_ID = wp.cat_ID
224                                WHERE panel_id = " . $customWritePanelId;
225                }
226               
227
228                $results = $wpdb->get_results($sql);
229                if (!isset($results))
230                        $results = array();
231               
232                return $results;
233        }
234       
235        /**
236         * Create a capability name for a write panel given its name. This function is
237         * copied from WP's sanitize_title_with_dashes($title) (formatting.php)
238         *
239         * @param string $customWritePanelName panel name
240         * @return string capability name
241         */
242        function GetCapabilityName($customWritePanelName)
243        {
244                // copied from WP's sanitize_title_with_dashes($title) (formatting.php)
245                $capabilityName = strip_tags($customWritePanelName);
246                // Preserve escaped octets.
247                $capabilityName = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $capabilityName);
248                // Remove percent signs that are not part of an octet.
249                $capabilityName = str_replace('%', '', $capabilityName);
250                // Restore octets.
251                $capabilityName = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $capabilityName);
252   
253                $capabilityName = remove_accents($capabilityName);
254                if (seems_utf8($capabilityName)) 
255                {
256                        if (function_exists('mb_strtolower')) 
257                        {
258                                $capabilityName = mb_strtolower($capabilityName, 'UTF-8');
259                        }
260                        $capabilityName = utf8_uri_encode($capabilityName, 200);
261                }
262   
263                $capabilityName = strtolower($capabilityName);
264                $capabilityName = preg_replace('/&.+?;/', '', $capabilityName); // kill entities
265                $capabilityName = preg_replace('/[^%a-z0-9 _-]/', '', $capabilityName);
266                $capabilityName = preg_replace('/\s+/', '_', $capabilityName);
267                $capabilityName = preg_replace('|-+|', '_', $capabilityName);
268                $capabilityName = trim($capabilityName, '_');
269   
270                return $capabilityName;
271        }
272
273
274
275               
276
277        /**
278         * Get a list of the standard fields of a the write panel
279         *
280         * @param integer $customWritePanelId panel id
281         * @return array of ids of the standard fields (see $STANDARD_FIELDS defined in RCCWP_Constant.php)
282         */
283        function GetStandardFields($customWritePanelId)
284        {
285                global $wpdb;
286                $sql = "SELECT standard_field_id FROM " . RC_CWP_TABLE_PANEL_STANDARD_FIELD . 
287                                " WHERE panel_id = " . $customWritePanelId;
288                $results = $wpdb->get_col($sql);
289                if (!isset($results))
290                        $results = array();
291               
292                return $results;
293        }
294       
295        /**
296         * Updates the properties of a write panel
297         *
298         * @param integer $customWritePanelId panel id
299         * @param string $name write panel name
300         * @param string $description write panel description
301         * @param array $standardFields a list of standard fields ids that are to be displayed in
302         *                                                      in the panel. Use $STANDARD_FIELDS defined in RCCWP_Constant.php
303         * @param array $categories array of category ids that are checked by default when the user
304         *                                                      opens Write tab for that panel.
305         * @param integer $display_order the order of the panel in Flutter > Write Panels tab
306         * @param string $type 'post' or 'page'
307         */
308        function Update($customWritePanelId, $name, $description = '', $standardFields = array(), $categories = array(), $display_order = 1)
309        {
310                include_once('RC_Format.php');
311                global $wpdb;
312               
313                $capabilityName = RCCWP_CustomWritePanel::GetCapabilityName($name);
314       
315                $sql = sprintf(
316                        "UPDATE " . RC_CWP_TABLE_PANELS .
317                        " SET name = %s" .
318                        " , description = %s" .
319                        " , display_order = %d" .
320                        " , capability_name = %s" .
321                        " , type = %s" .
322                        " where id = %d",
323                        RC_Format::TextToSql($name), 
324                        RC_Format::TextToSql($description),
325                        $display_order,
326                        RC_Format::TextToSql($capabilityName),
327                        RC_Format::TextToSql($_POST['radPostPage']),
328                        $customWritePanelId );
329               
330                $wpdb->query($sql);
331               
332                if (!isset($categories) || empty($categories))
333                {
334                        $sql = sprintf(
335                                "DELETE FROM " . RC_CWP_TABLE_PANEL_CATEGORY .
336                                " WHERE panel_id = %d",
337                                $customWritePanelId
338                                );
339                       
340                        $wpdb->query($sql);
341                }
342                else
343                {
344                        $currentCategoryIds = array();
345                        $currentCategoryIds = RCCWP_CustomWritePanel::GetAssignedCategoryIds($customWritePanelId);
346                       
347                        $keepCategoryIds = array_intersect($currentCategoryIds, $categories);
348                        $deleteCategoryIds = array_diff($currentCategoryIds, $keepCategoryIds);
349                        $insertCategoryIds = array_diff($categories, $keepCategoryIds);
350                       
351                        foreach ($insertCategoryIds as $cat_id)
352                        {
353                                $sql = sprintf(
354                                        "INSERT INTO " . RC_CWP_TABLE_PANEL_CATEGORY .
355                                        " (panel_id, cat_id)" .
356                                        " values (%d, %d)",
357                                        $customWritePanelId,
358                                        $cat_id 
359                                        );
360                                $wpdb->query($sql);
361                        }
362                       
363                        if (!empty($deleteCategoryIds))
364                        {
365                                $sql = sprintf(
366                                        "DELETE FROM " . RC_CWP_TABLE_PANEL_CATEGORY .
367                                        " WHERE panel_id = %d" .
368                                        " AND cat_id IN (%s)",
369                                        $customWritePanelId,
370                                        implode(',', $deleteCategoryIds)
371                                        );
372                               
373                                $wpdb->query($sql);
374                        }
375                }
376               
377                if (!isset($standardFields) || empty($standardFields))
378                {                       
379                        $sql = sprintf(
380                                "DELETE FROM " . RC_CWP_TABLE_PANEL_STANDARD_FIELD .
381                                " WHERE panel_id = %d",
382                                $customWritePanelId
383                                );
384                        $wpdb->query($sql);
385                }
386                else
387                {
388                        $currentStandardFieldIds = array();
389                        $currentStandardFieldIds = RCCWP_CustomWritePanel::GetStandardFields($customWritePanelId);
390                       
391                        $keepStandardFieldIds = array_intersect($currentStandardFieldIds, $standardFields);
392                        $deleteStandardFieldIds = array_diff($currentStandardFieldIds, $keepStandardFieldIds);
393                        $insertStandardFieldIds = array_diff($standardFields, $keepStandardFieldIds);
394                       
395                        foreach ($insertStandardFieldIds as $standard_field_id)
396                        {
397                                $sql = sprintf(
398                                        "INSERT INTO " . RC_CWP_TABLE_PANEL_STANDARD_FIELD .
399                                        " (panel_id, standard_field_id)" .
400                                        " values (%d, %d)",
401                                        $customWritePanelId,
402                                        $standard_field_id 
403                                        );
404                                $wpdb->query($sql);
405                        }
406                       
407                        if (!empty($deleteStandardFieldIds))
408                        {
409                                $sql = sprintf(
410                                        "DELETE FROM " . RC_CWP_TABLE_PANEL_STANDARD_FIELD .
411                                        " WHERE panel_id = %d" .
412                                        " AND standard_field_id IN (%s)",
413                                        $customWritePanelId,
414                                        implode(',', $deleteStandardFieldIds)
415                                        );
416                               
417                                $wpdb->query($sql);
418                        }
419                }
420       
421        }
422       
423        /**
424         * Retrieves the groups of a module
425         *
426         * @param integer $customWriteModuleId module id
427         * @return array of objects representing basic information of the group,
428         *                              each object contains id, name and module_id   
429         */
430        function GetCustomGroups($customWritePanelId)
431        {
432                global $wpdb;
433                $sql = "SELECT * FROM " . RC_CWP_TABLE_PANEL_GROUPS .
434                        " WHERE panel_id = " . $customWritePanelId .
435                        " ORDER BY name";
436
437                $results =$wpdb->get_results($sql);
438                if (!isset($results))
439                        $results = array();
440               
441                return $results;
442        }
443       
444       
445        /**
446         * Import a write panel given the file path.
447         * @param string $panelFilePath the full path of the panel file
448         * @param string $writePanelName the write panel name, if this value if false, the function will
449         *                                                      use the pnl filename as the write panel name. The default value is false
450         * @return the panel id, or false in case of error.
451         */
452        function Import($panelFilePath, $writePanelName = false)
453        {
454                include_once('RCCWP_CustomGroup.php');
455                include_once('RCCWP_CustomField.php');
456                include_once('RCCWP_Application.php');
457
458                if (!$writePanelName)
459                        //use filename
460                        $writePanelName = basename($panelFilePath, ".pnl");
461
462                if ($writePanelName == '') return false;
463
464                // Append a number if the panel already exists,
465                $i = 1;
466                $newWritePanelName = $writePanelName;
467                //while (file_exists(CANVASPATH.'/modules/'.$newModuleName)){
468                //      $newModuleName = $moduleName. "_" . $i++;
469                //}
470                //$moduleName = $newModuleName;
471
472                // Unserialize file
473                $imported_data = unserialize(file_get_contents($panelFilePath));
474                $types_results = RCCWP_CustomField::GetCustomFieldTypes();
475                $types = array();
476                foreach($types_results as $types_result){
477                        $types[$types_result->name] = $types_result->id;
478                }
479               
480                // Prepare categories list
481                $assignedCategories = array();
482                foreach($imported_data['panel']->assignedCategories as $cat_name){
483                        $assignedCategories[] = wp_create_category($cat_name);
484                }
485               
486                //Create write panel
487                $writePanelID = RCCWP_CustomWritePanel::Create($writePanelName, $imported_data['panel']->description, $imported_data['panel']->standardFieldsIDs, $assignedCategories,$imported_data['panel']->display_order, $imported_data['panel']->type, false);
488               
489                foreach($imported_data['fields'] as $groupName => $group){
490                        // For backward compatability
491                        if (!isset($group->fields)) {
492                                $newGroup->fields = $group;
493                                $group = $newGroup; 
494                        }
495                       
496                        // Import group
497                        $groupID = RCCWP_CustomGroup::Create($writePanelID, $groupName, $group->duplicate, $group->at_right);
498                       
499                        // Import group fields
500                        foreach ($group->fields as $field){
501                                $fieldOptions = @implode("\n", $field->options);
502                                $fieldDefault = @implode("\n", $field->default_value);
503                                RCCWP_CustomField::Create($groupID, $field->name, $field->description, $field->display_order, $field->required_field, $types[$field->type], $fieldOptions, $fieldDefault, $field->properties, $field->duplicate);
504                        }
505                }
506
507
508                return $writePanelID;
509        }
510       
511
512        /**
513         * Export a write panel to file
514         *
515         * @param integer $panelID
516         * @param string $exportedFilename the full path of the file to which the panel will be exported
517         */
518        function Export($panelID, $exportedFilename){
519                include_once('RCCWP_CustomWriteModule.php');
520                include_once('RCCWP_CustomGroup.php');
521                include_once('RCCWP_CustomField.php');
522       
523                $exported_data = array();
524
525                $writePanel = RCCWP_CustomWritePanel::Get($panelID);
526                $writePanel->standardFieldsIDs = RCCWP_CustomWritePanel::GetStandardFields($panelID);
527                $writePanel->assignedCategories = array(); 
528                $assignedCategories = RCCWP_CustomWritePanel::GetAssignedCategories($panelID);
529                foreach($assignedCategories as $assignedCategory){
530                        $writePanel->assignedCategories[] = $assignedCategory->cat_name;
531                }
532               
533                $moduleGroups = RCCWP_CustomWritePanel::GetCustomGroups($panelID);
534                foreach( $moduleGroups as $moduleGroup){
535                        $groupFields[$moduleGroup->name]->fields = RCCWP_CustomGroup::GetCustomFields($moduleGroup->id);
536                        $groupFields[$moduleGroup->name]->duplicate = $moduleGroup->duplicate;
537                        $groupFields[$moduleGroup->name]->at_right = $moduleGroup->at_right;
538                }
539
540                $exported_data['panel'] = $writePanel;
541                $exported_data['fields'] = $groupFields;
542               
543                $handle = fopen($exportedFilename, "w");
544                $result = fwrite($handle, serialize($exported_data));
545                @fclose($handle);
546        }
547       
548       
549}
550?>
Note: See TracBrowser for help on using the browser.