| 1 |
|
<?php |
| 2 |
|
|
| 3 |
|
/** |
| 4 |
|
* @file |
| 5 |
|
* buddylist_ui.admin include file |
| 6 |
|
* |
| 7 |
|
* contains functions for admin area |
| 8 |
|
*/ |
| 9 |
|
|
| 10 |
|
/** |
| 11 |
|
* Generates overview of relationtypes |
| 12 |
|
*/ |
| 13 |
|
function buddylist_ui_admin_overview() { |
| 14 |
|
$form = array(); |
| 15 |
|
|
| 16 |
|
//active relationtypes |
| 17 |
|
$form['active'] = array('#value' => '<h2>'. t('Active relationtypes'). '</h2>'); |
| 18 |
|
$form['active_table'] = buddylist_ui_admin_get_rtypes(1); |
| 19 |
|
|
| 20 |
|
//inactive relationtypes |
| 21 |
|
$form['inactive'] = array('#value' => '<h2>'. t('Inactive relationtypes'). '</h2>'); |
| 22 |
|
$form['inactive_table'] = buddylist_ui_admin_get_rtypes(0); |
| 23 |
|
|
| 24 |
|
return $form; |
| 25 |
|
} |
| 26 |
|
|
| 27 |
|
/** |
| 28 |
|
* Generates table with active/inactive relationtypes and operation links |
| 29 |
|
*/ |
| 30 |
|
function buddylist_ui_admin_get_rtypes($active_state) { |
| 31 |
|
// load all types |
| 32 |
|
$rtypes = buddylist_api_rtypes_load(); |
| 33 |
|
$final_types = array(); |
| 34 |
|
|
| 35 |
|
foreach($rtypes as $rtype) { |
| 36 |
|
// only add to table if the state matches |
| 37 |
|
if ($rtype->active == $active_state) { |
| 38 |
|
$path = 'admin/buddylist/rtypes/'. $rtype->rtid; |
| 39 |
|
$id = $rtype->rtid; |
| 40 |
|
$final_types[$id] = (array)$rtype; |
| 41 |
|
$ops = array(); |
| 42 |
|
$ops[] = l(t('edit'), $path .'/edit'); |
| 43 |
|
|
| 44 |
|
// only type 1 relationtypes can be deleted |
| 45 |
|
if($rtype->type == 1) |
| 46 |
|
$ops[] = l(t('delete'), $path .'/delete'); |
| 47 |
|
|
| 48 |
|
$final_types[$id]['oneway'] = ($final_types[$id]['oneway'] == 1) ? t('Yes') : t('No'); |
| 49 |
|
$final_types[$id]['type'] = ($final_types[$id]['type'] == 1) ? t('User') : t('Module'); |
| 50 |
|
|
| 51 |
|
unset($final_types[$id]['active']); |
| 52 |
|
|
| 53 |
|
$final_types[$id][] = implode(' ', $ops); |
| 54 |
|
} |
| 55 |
|
} |
| 56 |
|
|
| 57 |
|
// no relationtypes found |
| 58 |
|
if (!count($final_types)) { |
| 59 |
|
return array('#value' => '<p>'. t('None') .'</p>'); |
| 60 |
|
} |
| 61 |
|
|
| 62 |
|
$header = array(t('Relation ID'), t('Name'), t('Oneway'), t('Type'), t('Operations')); |
| 63 |
|
return array('#value' => theme('table', $header, $final_types)); |
| 64 |
|
} |
| 65 |
|
|
| 66 |
|
/** |
| 67 |
|
* Returns the form for editing/adding a rtype |
| 68 |
|
*/ |
| 69 |
|
function buddylist_ui_admin_form_edit(&$form_state, $rtid = NULL) { |
| 70 |
|
$form = array(); |
| 71 |
|
|
| 72 |
|
$form['name'] = array( |
| 73 |
|
'#type' => 'textfield', |
| 74 |
|
'#title' => t('Name'), |
| 75 |
|
'#maxlength' => 255, |
| 76 |
|
'#default_value' => $rtid->name, |
| 77 |
|
'#description' => t("Example: buddy, friend, colleague."), |
| 78 |
|
'#required' => TRUE, |
| 79 |
|
); |
| 80 |
|
$form['oneway'] = array( |
| 81 |
|
'#type' => 'checkbox', |
| 82 |
|
'#title' => t('This is a oneway relationship'), |
| 83 |
|
'#default_value' => $rtid->oneway, |
| 84 |
|
'#description' => t('Check this if this relationship should only go one way (Example: Fan, Subscriber)'), |
| 85 |
|
); |
| 86 |
|
$form['active'] = array( |
| 87 |
|
'#type' => 'checkbox', |
| 88 |
|
'#title' => t('Relationtype is active'), |
| 89 |
|
'#default_value' => $rtid->active, |
| 90 |
|
'#description' => t('If checked, users can use this relationship'), |
| 91 |
|
); |
| 92 |
|
$form['submit'] = array( |
| 93 |
|
'#type' => 'submit', |
| 94 |
|
'#value' => 'Save', |
| 95 |
|
); |
| 96 |
|
|
| 97 |
|
// extra for edit process |
| 98 |
|
if($rtid != NULL ) { |
| 99 |
|
$form['rtid'] = array( |
| 100 |
|
'#type' => 'hidden', |
| 101 |
|
'#default_value' => $rtid->rtid, |
| 102 |
|
); |
| 103 |
|
|
| 104 |
|
// if relation type comes from module, admin should not rename rtype |
| 105 |
|
if($rtid->type == 0) { |
| 106 |
|
unset($form['oneway']); |
| 107 |
|
unset($form['name']); |
| 108 |
|
} |
| 109 |
|
} |
| 110 |
|
|
| 111 |
|
return $form; |
| 112 |
|
} |
| 113 |
|
|
| 114 |
|
/** |
| 115 |
|
* Submit function for edit/delete a relationtype |
| 116 |
|
*/ |
| 117 |
|
function buddylist_ui_admin_form_edit_submit($form, &$form_state) { |
| 118 |
|
$res = new stdClass(); |
| 119 |
|
foreach(array('name', 'oneway', 'active', 'rtid') as $key) { |
| 120 |
|
$res->{$key} = $form_state['values'][$key]; |
| 121 |
|
} |
| 122 |
|
|
| 123 |
|
if(is_numeric($res->rtid)) |
| 124 |
|
drupal_set_message(t('Relation type has been updated')); |
| 125 |
|
else { |
| 126 |
|
drupal_set_message(t('Relation type has been saved')); |
| 127 |
|
|
| 128 |
|
// 1 means user generated |
| 129 |
|
$res->type = 1; |
| 130 |
|
} |
| 131 |
|
|
| 132 |
|
buddylist_api_rtype_save($res); |
| 133 |
|
|
| 134 |
|
$form_state['redirect'] = 'admin/buddylist/rtypes'; |
| 135 |
|
} |
| 136 |
|
|
| 137 |
|
|
| 138 |
|
/** |
| 139 |
|
* Returns the form for deleting a rtype |
| 140 |
|
*/ |
| 141 |
|
function buddylist_ui_admin_form_delete(&$form_state, $rtid = NULL) { |
| 142 |
|
if ($rtid) { |
| 143 |
|
$form['rtid'] = array( |
| 144 |
|
'#type' => 'value', |
| 145 |
|
'#value' => (int)$rtid->rtid, |
| 146 |
|
); |
| 147 |
|
|
| 148 |
|
return confirm_form( |
| 149 |
|
$form, |
| 150 |
|
t('Are you sure you want to delete %name?', array('%name' => $rtid->name)), |
| 151 |
|
'admin/buddylist/rtypes', |
| 152 |
|
t('This action cannot be undone.'), |
| 153 |
|
t('Delete'), t('Cancel') |
| 154 |
|
); |
| 155 |
|
} |
| 156 |
|
else { |
| 157 |
|
drupal_set_message(t('This relationtype does not exist.')); |
| 158 |
|
drupal_goto('admin/buddylist/rtypes'); |
| 159 |
|
} |
| 160 |
|
} |
| 161 |
|
|
| 162 |
|
/** |
| 163 |
|
* Submit function for deleting a relationtype |
| 164 |
|
*/ |
| 165 |
|
function buddylist_ui_admin_form_delete_submit($form, &$form_state) { |
| 166 |
|
$rtype = buddylist_api_rtype_load($form_state['values']['rtid']); |
| 167 |
|
buddylist_api_rtype_delete($rtype); |
| 168 |
|
drupal_set_message(t('Relationship has been deleted.')); |
| 169 |
|
$form_state['redirect'] = 'admin/buddylist/rtypes'; |
| 170 |
|
} |