| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
// $Name$
|
| 4 |
|
| 5 |
/**
|
| 6 |
* @file
|
| 7 |
* contains the functions for group management
|
| 8 |
*/
|
| 9 |
|
| 10 |
/**
|
| 11 |
* groups
|
| 12 |
* from imagepicker groups in my account
|
| 13 |
*/
|
| 14 |
function imagepicker_user_groups($mode="", $gid=0) {
|
| 15 |
// $mode is edit, delete or empty
|
| 16 |
if (empty($mode)) {
|
| 17 |
$content = imagepicker_groups_list(TRUE);
|
| 18 |
$content .= drupal_get_form('imagepicker_groups_form');
|
| 19 |
}
|
| 20 |
elseif ($mode == 'edit') {
|
| 21 |
$record = imagepicker_get_user_group($gid);
|
| 22 |
$content .= drupal_get_form('imagepicker_groups_form', $record);
|
| 23 |
}
|
| 24 |
elseif ($mode == 'delete') {
|
| 25 |
$content .= drupal_get_form('imagepicker_group_delete_form', $gid);
|
| 26 |
}
|
| 27 |
return $content;
|
| 28 |
}
|
| 29 |
|
| 30 |
function imagepicker_groups_form(&$form_state, $record=0, $account=FALSE) {
|
| 31 |
$form['groupsave'] = array(
|
| 32 |
'#type' => 'fieldset',
|
| 33 |
'#title' => ( $record ? t('Edit group') : t('Add group')),
|
| 34 |
'#description' => t('Give your group a brief name and optionally put any additional information in the group description box'),
|
| 35 |
'#collapsible' => TRUE,
|
| 36 |
'#collapsed' => FALSE,
|
| 37 |
);
|
| 38 |
$form['groupsave']['group_name'] = array(
|
| 39 |
'#type' => 'textfield',
|
| 40 |
'#title' => t('Group name'),
|
| 41 |
'#size' => 20,
|
| 42 |
'#default_value' => ($record ? $record->group_name : ''),
|
| 43 |
'#description' => t(''),
|
| 44 |
'#required' => TRUE
|
| 45 |
);
|
| 46 |
$form['groupsave']['group_description'] = array(
|
| 47 |
'#type' => 'textfield',
|
| 48 |
'#title' => t('group description'),
|
| 49 |
'#size' => 50,
|
| 50 |
'#maxlength' => 50,
|
| 51 |
'#default_value' => ($record ? $record->group_description : ''),
|
| 52 |
'#description' => t('Maximum 50 characters'),
|
| 53 |
'#required' => FALSE
|
| 54 |
);
|
| 55 |
if ($account == TRUE) {
|
| 56 |
$form['groupsave']['account'] = array(
|
| 57 |
'#type' => 'hidden',
|
| 58 |
'#value' => 1,
|
| 59 |
);
|
| 60 |
}
|
| 61 |
if ($record) {
|
| 62 |
$form['groupsave']['gid'] = array(
|
| 63 |
'#type' => 'hidden',
|
| 64 |
'#value' => $record->gid,
|
| 65 |
);
|
| 66 |
}
|
| 67 |
$form['groupsave']['submit'] = array(
|
| 68 |
'#type' => 'submit',
|
| 69 |
'#value' => t('Save group'),
|
| 70 |
);
|
| 71 |
return $form;
|
| 72 |
}
|
| 73 |
|
| 74 |
function imagepicker_groups_form_submit($form, &$form_state) {
|
| 75 |
global $user;
|
| 76 |
$record['group_name'] = $form_state['values']['group_name'];
|
| 77 |
$record['group_description'] = $form_state['values']['group_description'];
|
| 78 |
$record['uid'] = $user->uid;
|
| 79 |
if ($form_state['values']['gid']) {
|
| 80 |
$record['gid'] = $form_state['values']['gid'];
|
| 81 |
imagepicker_update_user_group($record);
|
| 82 |
}
|
| 83 |
else {
|
| 84 |
imagepicker_insert_user_group($record);
|
| 85 |
}
|
| 86 |
}
|
| 87 |
|
| 88 |
// imagepicker_user_groups functions
|
| 89 |
function imagepicker_insert_user_group($record) {
|
| 90 |
if (db_query(
|
| 91 |
"INSERT INTO {imagepicker_user_groups} (uid, group_name, group_description) VALUES (%d, '%s', '%s')",
|
| 92 |
array($record['uid'], $record['group_name'], $record['group_description']))) {
|
| 93 |
drupal_set_message(t('Group was successfully inserted'));
|
| 94 |
}
|
| 95 |
else {
|
| 96 |
drupal_set_message(t('Error while trying to insert your group.'));
|
| 97 |
}
|
| 98 |
}
|
| 99 |
|
| 100 |
function imagepicker_update_user_group($record) {
|
| 101 |
if (db_query(
|
| 102 |
"UPDATE {imagepicker_user_groups} SET group_name='%s', group_description='%s' WHERE gid = %d",
|
| 103 |
array($record['group_name'], $record['group_description'], $record['gid']))) {
|
| 104 |
drupal_set_message(t('Group was successfully updated'));
|
| 105 |
}
|
| 106 |
else {
|
| 107 |
drupal_set_message(t('Error while trying to update your group.'));
|
| 108 |
}
|
| 109 |
}
|
| 110 |
|
| 111 |
function imagepicker_delete_user_group($gid) {
|
| 112 |
if (db_query("DELETE FROM {imagepicker_user_groups} WHERE gid = %d", array($gid))
|
| 113 |
&& db_query("DELETE FROM {imagepicker_group_images} WHERE gid=%d", array($gid)) ) {
|
| 114 |
drupal_set_message(t('Group was successfully deleted'));
|
| 115 |
}
|
| 116 |
else {
|
| 117 |
drupal_set_message(t('Error while trying to delete group.'));
|
| 118 |
}
|
| 119 |
}
|
| 120 |
|
| 121 |
function imagepicker_get_user_group($gid, $obj=TRUE) {
|
| 122 |
$result = db_query("SELECT * FROM {imagepicker_user_groups} WHERE gid = %d", array($gid));
|
| 123 |
if ($obj) {
|
| 124 |
return db_fetch_object($result);
|
| 125 |
}
|
| 126 |
return db_fetch_array($result);
|
| 127 |
}
|
| 128 |
|
| 129 |
function imagepicker_get_user_groups_by_user($uid, $obj=TRUE) {
|
| 130 |
$result = db_query("SELECT * FROM {imagepicker_user_groups} WHERE uid = %d", array($uid));
|
| 131 |
if ($obj) {
|
| 132 |
return db_fetch_object($result);
|
| 133 |
}
|
| 134 |
return db_fetch_array($result);
|
| 135 |
}
|
| 136 |
|
| 137 |
// imagepicker_group_images
|
| 138 |
// gid img_id
|
| 139 |
|
| 140 |
// build a table
|
| 141 |
function imagepicker_groups_list($myaccount=FALSE, $account=FALSE) {
|
| 142 |
// show a table of the lists belonging to the current user
|
| 143 |
if ($account) {
|
| 144 |
$user = $account;
|
| 145 |
}
|
| 146 |
else {
|
| 147 |
global $user;
|
| 148 |
}
|
| 149 |
if ($myaccount) {
|
| 150 |
$editpath = 'user/'. $user->uid .'/imagepicker/groups/edit/';
|
| 151 |
$deletepath = 'user/'. $user->uid .'/imagepicker/groups/delete/';
|
| 152 |
}
|
| 153 |
else {
|
| 154 |
$editpath = "imagepicker/groups/edit/";
|
| 155 |
$deletepath = "imagepicker/groups/delete/";
|
| 156 |
}
|
| 157 |
$output = "";
|
| 158 |
$how_many = variable_get('imagepicker_rows_per_page', 25);
|
| 159 |
$sql = "SELECT * FROM {imagepicker_user_groups} WHERE uid=". $user->uid ." ORDER BY group_name";
|
| 160 |
$result = pager_query($sql, $how_many);
|
| 161 |
$rows = array();
|
| 162 |
$totcount = 0;
|
| 163 |
$enabledlist = imagepicker_get_enabled_group();
|
| 164 |
while ($row = db_fetch_array($result)) {
|
| 165 |
$count = imagepicker_get_group_images_count($row['gid']);
|
| 166 |
$totcount += $count;
|
| 167 |
$row_data = array(
|
| 168 |
check_plain($row['group_name']),
|
| 169 |
check_plain($row['group_description']),
|
| 170 |
$count,
|
| 171 |
(($enabledlist && in_array($row['gid'], $enabledlist)) ? t('selected') : ''),
|
| 172 |
l(t('Edit'), $editpath . $row['gid']),
|
| 173 |
l(t('Delete'), $deletepath . $row['gid']),
|
| 174 |
);
|
| 175 |
$rows[] = $row_data;
|
| 176 |
}
|
| 177 |
|
| 178 |
if (count($rows)) {
|
| 179 |
$header = array(
|
| 180 |
t('Group name'),
|
| 181 |
t('Description'),
|
| 182 |
t('No. images'),
|
| 183 |
t('State'),
|
| 184 |
array('data' => t('Actions'), 'colspan' => 2),
|
| 185 |
);
|
| 186 |
$output .= '<div class="imgp_groups_list">'. theme('table', $header, $rows) . theme('pager', NULL, $how_many) ."</div>";
|
| 187 |
|
| 188 |
$allcount = _imagepicker_user_has_img();
|
| 189 |
$output .= '<div class="imgp_groups_info">';
|
| 190 |
$output .= t('Total number of images: %allcount', array('%allcount' => $allcount)) .'<br/>';
|
| 191 |
$output .= t('Total number of grouped images: %totcount', array('%totcount' => $totcount)) .'<br/>';
|
| 192 |
$output .= t('Total number of ungrouped images: %lcount', array('%lcount' => $allcount-$totcount)) .'<br/>';
|
| 193 |
$output .= '</div>';
|
| 194 |
|
| 195 |
return $output;
|
| 196 |
}
|
| 197 |
else {
|
| 198 |
return '<div class="messages">'. t('No groups found.') .'</div>';
|
| 199 |
}
|
| 200 |
}
|
| 201 |
function imagepicker_get_group_images_count($gid) {
|
| 202 |
$result = db_query("SELECT count(gid) as gidct FROM {imagepicker_group_images} WHERE gid = %d", array($gid));
|
| 203 |
$row = db_fetch_array($result);
|
| 204 |
return $row['gidct'];
|
| 205 |
|
| 206 |
}
|
| 207 |
|
| 208 |
function imagepicker_group_edit($gid) {
|
| 209 |
$record = imagepicker_get_user_group($gid);
|
| 210 |
$content .= drupal_get_form('imagepicker_groups_form', $record);
|
| 211 |
return $content;
|
| 212 |
}
|
| 213 |
|
| 214 |
function imagepicker_group_delete_form(&$form_state, $gid) {
|
| 215 |
$record = imagepicker_get_user_group($gid);
|
| 216 |
if ($record) {
|
| 217 |
$form['groupname'] = array(
|
| 218 |
'#value' => '<p>'. $record->group_name .'</p>',
|
| 219 |
);
|
| 220 |
$form['mode'] = array(
|
| 221 |
'#type' => 'hidden',
|
| 222 |
'#value' => 'reallydelete',
|
| 223 |
);
|
| 224 |
$form['gid'] = array(
|
| 225 |
'#type' => 'hidden',
|
| 226 |
'#value' => $gid,
|
| 227 |
);
|
| 228 |
$form['delete'] = array(
|
| 229 |
'#type' => 'submit',
|
| 230 |
'#value' => t('Really Delete record'),
|
| 231 |
);
|
| 232 |
return $form;
|
| 233 |
}
|
| 234 |
else {
|
| 235 |
|
| 236 |
}
|
| 237 |
}
|
| 238 |
function imagepicker_group_delete_form_submit($form, &$form_state) {
|
| 239 |
imagepicker_delete_user_group($form_state['values']['gid']);
|
| 240 |
}
|
| 241 |
|
| 242 |
// not in use
|
| 243 |
function imagepicker_get_grouped_images() {
|
| 244 |
global $user;
|
| 245 |
// now the enabled ones
|
| 246 |
$result = db_query("
|
| 247 |
SELECT i.img_id
|
| 248 |
FROM {imagepicker_user_groups} g, {imagepicker_group_images} i
|
| 249 |
WHERE g.uid=%d AND g.gid = i.gid AND g.state=1", array($user->uid));
|
| 250 |
$ct = 0;
|
| 251 |
while ($row = db_fetch_array($result)) {
|
| 252 |
$data[] = $row['img_id'];
|
| 253 |
$ct++;
|
| 254 |
}
|
| 255 |
if ($ct) {
|
| 256 |
return $data;
|
| 257 |
}
|
| 258 |
return FALSE;
|
| 259 |
}
|
| 260 |
|