| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * @package FlutterDatabaseObjects |
|---|
| 4 | */ |
|---|
| 5 | |
|---|
| 6 | /** |
|---|
| 7 | * Create/Edit/Delete groups. Groups are just a collection of fields. |
|---|
| 8 | * @package FlutterDatabaseObjects |
|---|
| 9 | */ |
|---|
| 10 | |
|---|
| 11 | class RCCWP_CustomGroup |
|---|
| 12 | { |
|---|
| 13 | |
|---|
| 14 | /** |
|---|
| 15 | * Create a new group in a write panel |
|---|
| 16 | * |
|---|
| 17 | * @param unknown_type $customWritePanelId |
|---|
| 18 | * @param unknown_type $name group name |
|---|
| 19 | * @param unknown_type $duplicate a boolean indicating whether the group can be duplicated |
|---|
| 20 | * @param unknown_type $at_right a boolean indicating whether the group should be placed at right side. |
|---|
| 21 | * @return the id of the new group |
|---|
| 22 | */ |
|---|
| 23 | function Create($customWritePanelId, $name, $duplicate, $at_right) |
|---|
| 24 | { |
|---|
| 25 | require_once('RC_Format.php'); |
|---|
| 26 | global $wpdb; |
|---|
| 27 | $sql = sprintf( |
|---|
| 28 | "INSERT INTO " . RC_CWP_TABLE_PANEL_GROUPS . |
|---|
| 29 | " (panel_id, name, duplicate, at_right) values (%d, %s, %d, %d)", |
|---|
| 30 | $customWritePanelId, |
|---|
| 31 | RC_Format::TextToSql($name), |
|---|
| 32 | $duplicate, |
|---|
| 33 | $at_right |
|---|
| 34 | ); |
|---|
| 35 | $wpdb->query($sql); |
|---|
| 36 | |
|---|
| 37 | $customGroupId = $wpdb->insert_id; |
|---|
| 38 | return $customGroupId; |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | /** |
|---|
| 42 | * Delete a group given id |
|---|
| 43 | * |
|---|
| 44 | * @param integer $customGroupId |
|---|
| 45 | */ |
|---|
| 46 | function Delete($customGroupId = null) |
|---|
| 47 | { |
|---|
| 48 | include_once ('RCCWP_CustomField.php'); |
|---|
| 49 | if (isset($customGroupId)) |
|---|
| 50 | { |
|---|
| 51 | global $wpdb; |
|---|
| 52 | |
|---|
| 53 | $customFields = RCCWP_CustomGroup::GetCustomFields($customGroupId); |
|---|
| 54 | foreach ($customFields as $field) |
|---|
| 55 | { |
|---|
| 56 | RCCWP_CustomField::Delete($field->id); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | $sql = sprintf( |
|---|
| 60 | "DELETE FROM " . RC_CWP_TABLE_PANEL_GROUPS . |
|---|
| 61 | " WHERE id = %d", |
|---|
| 62 | $customGroupId |
|---|
| 63 | ); |
|---|
| 64 | $wpdb->query($sql); |
|---|
| 65 | } |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | /** |
|---|
| 69 | * Get group properties |
|---|
| 70 | * |
|---|
| 71 | * @param integer $groupId |
|---|
| 72 | * @return an object representing the group |
|---|
| 73 | */ |
|---|
| 74 | |
|---|
| 75 | function Get($groupId) |
|---|
| 76 | { |
|---|
| 77 | global $wpdb; |
|---|
| 78 | |
|---|
| 79 | $sql = "SELECT * FROM " . RC_CWP_TABLE_PANEL_GROUPS; |
|---|
| 80 | $sql .= " WHERE id = " . (int)$groupId; |
|---|
| 81 | $results = $wpdb->get_row($sql); |
|---|
| 82 | return $results; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | /** |
|---|
| 86 | * Get a list of the custom fields of a group |
|---|
| 87 | * |
|---|
| 88 | * @param integer $customGroupId the group id |
|---|
| 89 | * @return an array of objects containing information about fields. Each object contains |
|---|
| 90 | * 3 objects: properties, options and default_value |
|---|
| 91 | */ |
|---|
| 92 | function GetCustomFields($customGroupId) |
|---|
| 93 | { |
|---|
| 94 | global $wpdb; |
|---|
| 95 | $sql = "SELECT cf.id, cf.name, tt.name AS type, cf.description, cf.display_order, cf.required_field,cf.css, co.options, co.default_option AS default_value, tt.has_options, cp.properties, tt.has_properties, tt.allow_multiple_values, cf.duplicate FROM " . RC_CWP_TABLE_GROUP_FIELDS . |
|---|
| 96 | " cf LEFT JOIN " . RC_CWP_TABLE_CUSTOM_FIELD_OPTIONS . " co ON cf.id = co.custom_field_id" . |
|---|
| 97 | " LEFT JOIN " . RC_CWP_TABLE_CUSTOM_FIELD_PROPERTIES . " cp ON cf.id = cp.custom_field_id" . |
|---|
| 98 | " JOIN " . RC_CWP_TABLE_CUSTOM_FIELD_TYPES . " tt ON cf.type = tt.id" . |
|---|
| 99 | " WHERE group_id = " . $customGroupId . |
|---|
| 100 | " ORDER BY cf.display_order"; |
|---|
| 101 | $results =$wpdb->get_results($sql); |
|---|
| 102 | if (!isset($results)) |
|---|
| 103 | $results = array(); |
|---|
| 104 | |
|---|
| 105 | for ($i = 0; $i < $wpdb->num_rows; ++$i) |
|---|
| 106 | { |
|---|
| 107 | $results[$i]->options = unserialize($results[$i]->options); |
|---|
| 108 | $results[$i]->properties = unserialize($results[$i]->properties); |
|---|
| 109 | $results[$i]->default_value = unserialize($results[$i]->default_value); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | return $results; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | /** |
|---|
| 116 | * Update the group |
|---|
| 117 | * |
|---|
| 118 | * @param unknown_type $customWritePanelId |
|---|
| 119 | * @param unknown_type $name group name |
|---|
| 120 | * @param unknown_type $duplicate a boolean indicating whether the group can be duplicated |
|---|
| 121 | * @param unknown_type $at_right a boolean indicating whether the group should be placed at right side. |
|---|
| 122 | */ |
|---|
| 123 | function Update($customGroupId, $name, $duplicate, $at_right) |
|---|
| 124 | { |
|---|
| 125 | require_once('RC_Format.php'); |
|---|
| 126 | global $wpdb; |
|---|
| 127 | //$capabilityName = RCCWP_CustomWriteModule::GetCapabilityName($name); |
|---|
| 128 | |
|---|
| 129 | $sql = sprintf( |
|---|
| 130 | "UPDATE " . RC_CWP_TABLE_PANEL_GROUPS . |
|---|
| 131 | " SET name = %s , duplicate = %d, at_right = %d". |
|---|
| 132 | " where id = %d", |
|---|
| 133 | RC_Format::TextToSql($name), |
|---|
| 134 | $duplicate, |
|---|
| 135 | $at_right, |
|---|
| 136 | $customGroupId ); |
|---|
| 137 | $wpdb->query($sql); |
|---|
| 138 | |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | ?> |
|---|