| 1 |
<?php
|
| 2 |
// $Id: ezshop.module $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Provides an easy webshop interface.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_help().
|
| 11 |
*/
|
| 12 |
function ezshop_help($section) {
|
| 13 |
$output = '';
|
| 14 |
switch ($section) {
|
| 15 |
case 'admin/settings/ezshop':
|
| 16 |
$output = '<p>' . t('Select the content types for webshopping.') . '</p>';
|
| 17 |
break;
|
| 18 |
}
|
| 19 |
return $output;
|
| 20 |
}
|
| 21 |
|
| 22 |
/**
|
| 23 |
* Implementation of hook_perm().
|
| 24 |
*/
|
| 25 |
function ezshop_perm() {
|
| 26 |
return array('access ezshop', 'administer ezshop');
|
| 27 |
}
|
| 28 |
|
| 29 |
// TODO: Development path: view ezshop orders.
|
| 30 |
|
| 31 |
/**
|
| 32 |
* Implementation of hook_menu().
|
| 33 |
*/
|
| 34 |
function ezshop_menu($may_cache) {
|
| 35 |
$items = array();
|
| 36 |
if ($may_cache) {
|
| 37 |
$items[] = array(
|
| 38 |
'path' => 'admin/settings/ezshop',
|
| 39 |
'title' => t('EZShop'),
|
| 40 |
'description' => t('Select the content types for webshopping.'),
|
| 41 |
'callback' => 'drupal_get_form',
|
| 42 |
'callback arguments' => 'ezshop_admin_settings',
|
| 43 |
'access' => user_access('administer ezshop'),
|
| 44 |
'type' => MENU_NORMAL_ITEM,
|
| 45 |
);
|
| 46 |
$items[] = array(
|
| 47 |
'path' => 'ezshop/list',
|
| 48 |
'title' => t('View cart'),
|
| 49 |
'description' => t('View the contents of your cart.'),
|
| 50 |
'callback' => 'drupal_get_form',
|
| 51 |
'callback arguments' => 'ezshop_list',
|
| 52 |
'access' => user_access('access ezshop'),
|
| 53 |
'type' => MENU_NORMAL_ITEM,
|
| 54 |
);
|
| 55 |
}
|
| 56 |
else {
|
| 57 |
if(arg(0) == 'ezshop' && is_numeric(arg(2))) {
|
| 58 |
$node = node_load(arg(2));
|
| 59 |
if($node->nid) {
|
| 60 |
switch(arg(1)) {
|
| 61 |
case 'add':
|
| 62 |
$items[] = array(
|
| 63 |
'path' => 'ezshop/add/'. $node->nid,
|
| 64 |
'title' => t('Add to cart'),
|
| 65 |
'callback' => 'ezshop_add',
|
| 66 |
'callback arguments' => arg(2),
|
| 67 |
'access' => user_access('access ezshop'),
|
| 68 |
'type' => MENU_CALLBACK,
|
| 69 |
);
|
| 70 |
case 'remove':
|
| 71 |
$items[] = array(
|
| 72 |
'path' => 'ezshop/remove/'. $node->nid,
|
| 73 |
'title' => t('Remove from cart'),
|
| 74 |
'callback' => 'ezshop_remove',
|
| 75 |
'callback arguments' => arg(2),
|
| 76 |
'access' => user_access('access ezshop'),
|
| 77 |
'type' => MENU_CALLBACK,
|
| 78 |
);
|
| 79 |
}
|
| 80 |
}
|
| 81 |
}
|
| 82 |
}
|
| 83 |
return $items;
|
| 84 |
}
|
| 85 |
|
| 86 |
/**
|
| 87 |
* Menu callback; displays the ezshop module settings page.
|
| 88 |
*/
|
| 89 |
function ezshop_admin_settings() {
|
| 90 |
$form['ezshop_content_types'] = array(
|
| 91 |
'#type' => 'checkboxes',
|
| 92 |
'#title' => t('Content types for webshopping'),
|
| 93 |
'#options' => node_get_types('names'),
|
| 94 |
'#default_value' => variable_get('ezshop_content_types', FALSE),
|
| 95 |
);
|
| 96 |
$form['ezshop_linkweight'] = array(
|
| 97 |
'#type' => 'textfield',
|
| 98 |
'#title' => t('Weight of the %add_to_cart link (on node pages)', array('%add_to_cart' => t('Add to cart'))),
|
| 99 |
'#default_value' => variable_get('ezshop_linkweight', 99),
|
| 100 |
);
|
| 101 |
$form['ezshop_ordermail'] = array(
|
| 102 |
'#type' => 'textfield',
|
| 103 |
'#title' => t('E-mail to sent the orders to'),
|
| 104 |
'#description' => t('Leave empty for %site_mail.', array('%site_mail' => variable_get('site_mail', ini_get('sendmail_from')))),
|
| 105 |
'#default_value' => variable_get('ezshop_ordermail', FALSE),
|
| 106 |
);
|
| 107 |
$form['ezshop_mailfooter'] = array(
|
| 108 |
'#type' => 'textarea',
|
| 109 |
'#title' => t('Message to be displayed as the footer of the email'),
|
| 110 |
'#default_value' => variable_get('ezshop_mailfooter', FALSE),
|
| 111 |
);
|
| 112 |
return system_settings_form($form);
|
| 113 |
}
|
| 114 |
|
| 115 |
/**
|
| 116 |
* Validate ezshop_admin_settings form submissions.
|
| 117 |
*/
|
| 118 |
function ezshop_admin_settings_validate($form_id, $form_values) {
|
| 119 |
if(!valid_email_address($form_values['ezshop_ordermail'])) {
|
| 120 |
form_set_error('ezshop_ordermail', t('Invalid e-mail address.'));
|
| 121 |
}
|
| 122 |
if((!is_numeric($form_values['ezshop_linkweight'])) || ($form_values['ezshop_linkweight'] < -99) || ($form_values['ezshop_linkweight'] > 99)) {
|
| 123 |
form_set_error('ezshop_linkweight', t('Weight of the link must be an integer between -99 and 99.'));
|
| 124 |
var_dump($form_values['ezshop_linkweight']);
|
| 125 |
}
|
| 126 |
}
|
| 127 |
|
| 128 |
/**
|
| 129 |
* Add the 'Add to cart' link to the nodes with the specified types.
|
| 130 |
*/
|
| 131 |
function ezshop_nodeapi(&$node, $op) {
|
| 132 |
if($op == 'view') {
|
| 133 |
$set = variable_get('ezshop_content_types', array());
|
| 134 |
if($set[$node->type] === $node->type) {
|
| 135 |
$node->content['ezshop'] = array(
|
| 136 |
'#weight' => variable_get('ezshop_linkweight', 99),
|
| 137 |
'#value' => '<div class="ezshop-add">'. l(t('Add to cart'), 'ezshop/add/'. $node->nid) .'</div>',
|
| 138 |
);
|
| 139 |
}
|
| 140 |
}
|
| 141 |
}
|
| 142 |
|
| 143 |
/**
|
| 144 |
* Menu callback; adds the node to the cart.
|
| 145 |
*/
|
| 146 |
function ezshop_add($nid) {
|
| 147 |
global $user;
|
| 148 |
if($user->uid == 0) {
|
| 149 |
return t('<a href="@register">Register</a> and <a href="@login">login</a> to be able to use the cart.', array('@register' => url('user/register'), '@login' => url('user/login')));
|
| 150 |
}
|
| 151 |
$ret = db_fetch_object(db_query('SELECT cnt FROM {ezshop_items} WHERE uid = %d AND nid = %d', $user->uid, $nid));
|
| 152 |
if($ret->cnt == 0) {
|
| 153 |
// This user does not have this node yet.
|
| 154 |
db_query('INSERT INTO {ezshop_items} (uid, nid, cnt) VALUES (%d, %d, 1)', $user->uid, $nid);
|
| 155 |
}
|
| 156 |
else {
|
| 157 |
db_query('UPDATE {ezshop_items} SET cnt = cnt + 1 WHERE uid = %d AND nid = %d', $user->uid, $nid);
|
| 158 |
}
|
| 159 |
$node = node_load($nid);
|
| 160 |
drupal_set_message(t('%title has been added to the cart.', array('%title' => $node->title)));
|
| 161 |
drupal_goto('node/'. $nid);
|
| 162 |
}
|
| 163 |
|
| 164 |
/**
|
| 165 |
* Menu callback; removes the node from the cart.
|
| 166 |
*/
|
| 167 |
function ezshop_remove($nid) {
|
| 168 |
global $user;
|
| 169 |
if($user->uid == 0) {
|
| 170 |
return t('<a href="@register">Register</a> and <a href="@login">login</a> to be able to use the cart.', array('@register' => url('user/register'), '@login' => url('user/login')));
|
| 171 |
}
|
| 172 |
db_query('DELETE FROM {ezshop_items} WHERE uid = %d AND nid = %d', $user->uid, $nid);
|
| 173 |
$node = node_load($nid);
|
| 174 |
drupal_set_message(t('%title has been removed from the cart.', array('%title' => $node->title)));
|
| 175 |
drupal_goto('ezshop/list');
|
| 176 |
}
|
| 177 |
|
| 178 |
/**
|
| 179 |
* Menu callback; display the cart.
|
| 180 |
*/
|
| 181 |
function ezshop_list() {
|
| 182 |
global $user;
|
| 183 |
if($user->uid == 0) {
|
| 184 |
drupal_set_message(t('<a href="@register">Register</a> and <a href="@login">login</a> to be able to use the cart.', array('@register' => url('user/register'), '@login' => url('user/login'))));
|
| 185 |
drupal_goto();
|
| 186 |
}
|
| 187 |
$header = array(
|
| 188 |
t('Count'),
|
| 189 |
t('Title'),
|
| 190 |
t('Operations'),
|
| 191 |
);
|
| 192 |
$sql = 'SELECT cnt, ei.nid nid, title FROM {ezshop_items} ei INNER JOIN {node} n ON n.nid = ei.nid WHERE ei.uid = %d';
|
| 193 |
$result = db_query($sql, $user->uid);
|
| 194 |
$form = array();
|
| 195 |
if(db_num_rows($result)) {
|
| 196 |
$form['cnt']['#tree'] = TRUE;
|
| 197 |
while($row = db_fetch_object($result)) {
|
| 198 |
$form['cnt'][$row->nid] = array('#type' => 'textfield', '#default_value' => $row->cnt, '#size' => 2);
|
| 199 |
$form['title'][$row->nid] = array('#value' => l($row->title, 'node/'. $row->nid));
|
| 200 |
$form['operations'][$row->nid] = array('#value' => l(t('Remove'), 'ezshop/remove/'. $row->nid));
|
| 201 |
}
|
| 202 |
}
|
| 203 |
$form['refresh'] = array(
|
| 204 |
'#type' => 'submit',
|
| 205 |
'#value' => t('Refresh'),
|
| 206 |
);
|
| 207 |
$form['order'] = array(
|
| 208 |
'#type' => 'submit',
|
| 209 |
'#value' => t('Order these items'),
|
| 210 |
);
|
| 211 |
return $form;
|
| 212 |
}
|
| 213 |
|
| 214 |
/**
|
| 215 |
* Theme callback; display the cart as a table.
|
| 216 |
*/
|
| 217 |
function theme_ezshop_list($form) {
|
| 218 |
$header = array(
|
| 219 |
t('Count'),
|
| 220 |
t('Title'),
|
| 221 |
t('Operations'),
|
| 222 |
);
|
| 223 |
if(isset($form['cnt']) && is_array($form['cnt']) && $form['cnt']['#tree']) {
|
| 224 |
foreach(element_children($form['cnt']) as $key) {
|
| 225 |
$rows[] = array(
|
| 226 |
drupal_render($form['cnt'][$key]),
|
| 227 |
drupal_render($form['title'][$key]),
|
| 228 |
drupal_render($form['operations'][$key]),
|
| 229 |
);
|
| 230 |
}
|
| 231 |
}
|
| 232 |
else {
|
| 233 |
return t('Your cart is empty.');
|
| 234 |
}
|
| 235 |
$output = theme('table', $header, $rows);
|
| 236 |
$output .= drupal_render($form);
|
| 237 |
return $output;
|
| 238 |
}
|
| 239 |
|
| 240 |
/**
|
| 241 |
* FAPI callback, handles the cart submissions (both refresh and order).
|
| 242 |
*/
|
| 243 |
function ezshop_list_submit($form_id, $form_values) {
|
| 244 |
global $user, $base_url;
|
| 245 |
switch($_POST['op']) {
|
| 246 |
case t('Refresh'):
|
| 247 |
foreach($form_values['cnt'] as $key => $value) {
|
| 248 |
if($value > 0) {
|
| 249 |
db_query('UPDATE {ezshop_items} SET cnt = %d WHERE uid = %d AND nid = %d', $value, $user->uid, $key);
|
| 250 |
}
|
| 251 |
else {
|
| 252 |
db_query('DELETE FROM {ezshop_items} WHERE uid = %d AND nid = %d', $user->uid, $key);
|
| 253 |
}
|
| 254 |
}
|
| 255 |
drupal_set_message(t('Cart contents has been refreshed.'));
|
| 256 |
break;
|
| 257 |
case t('Order these items'):
|
| 258 |
$message = t('Order details') ."\n". t('Count') .' -- '. t('Title') ."\n";
|
| 259 |
foreach($form_values['cnt'] as $key => $value) {
|
| 260 |
// XXX: A simple SELECT wouldn't be faster?
|
| 261 |
$node = node_load($key);
|
| 262 |
$message .= $value .' -- '. $node->title ."\n";
|
| 263 |
}
|
| 264 |
if(module_exists('profile')) {
|
| 265 |
$result = db_query('SELECT f.title, f.type, v.value FROM {profile_fields} f INNER JOIN {profile_values} v ON f.fid = v.fid WHERE uid = %d', $user->uid);
|
| 266 |
$user->profile = array();
|
| 267 |
while($field = db_fetch_object($result)) {
|
| 268 |
$user->profile[$field->title] = _profile_field_serialize($field->type) ? unserialize($field->value) : $field->value;
|
| 269 |
}
|
| 270 |
if(!empty($user->profile)) {
|
| 271 |
$message .= "\n". t('Profile details') ."\n";
|
| 272 |
foreach($user->profile as $key => $value) {
|
| 273 |
if(!empty($value)) {
|
| 274 |
$message .= "$key: $value\n";
|
| 275 |
}
|
| 276 |
}
|
| 277 |
}
|
| 278 |
}
|
| 279 |
$footer = variable_get('ezshop_mailfooter', FALSE);
|
| 280 |
if(!empty($footer)) {
|
| 281 |
$message .= "\n\n". strip_tags($footer);
|
| 282 |
}
|
| 283 |
$message = wordwrap($message, 80);
|
| 284 |
$subject = t('Orders of @user at @site', array('@user' => $user->name, '@site' => $base_url));
|
| 285 |
// The first mail should be sent to the admin.
|
| 286 |
$from = $user->mail;
|
| 287 |
$headers = array(
|
| 288 |
'Reply-to' => $from,
|
| 289 |
'Return-path' => "<$from>",
|
| 290 |
);
|
| 291 |
$recipient = variable_get('ezshop_ordermail', variable_get('site_mail', ini_get('sendmail_from')));
|
| 292 |
drupal_mail('ezshop_order', $recipient, $subject, $message, $from, $headers);
|
| 293 |
// The second mail should be sent to the customer.
|
| 294 |
$from = variable_get('ezshop_ordermail', variable_get('site_mail', ini_get('sendmail_from')));
|
| 295 |
$headers = array(
|
| 296 |
'Reply-to' => $from,
|
| 297 |
'Return-path' => "<$from>",
|
| 298 |
);
|
| 299 |
$recipient = $user->mail;
|
| 300 |
drupal_mail('ezshop_order', $recipient, $subject, $message, $from, $headers);
|
| 301 |
db_query('DELETE FROM {ezshop_items} WHERE uid = %d', $user->uid);
|
| 302 |
drupal_set_message(t('Your order has been sent both to you and the admin of the shop. Your cart is now empty.'));
|
| 303 |
drupal_goto('<front>');
|
| 304 |
break;
|
| 305 |
}
|
| 306 |
}
|
| 307 |
|
| 308 |
/**
|
| 309 |
* Views handler callback, display an "Add to cart" link.
|
| 310 |
*/
|
| 311 |
function ezshop_views_handler_field_node_cart($fieldinfo, $fielddata, $value, $data) {
|
| 312 |
return l(empty($fielddata['options']) ? t('Add to cart') : $fielddata['options'], 'ezshop/add/'. $data->nid);
|
| 313 |
}
|
| 314 |
|
| 315 |
/**
|
| 316 |
* Views handler callback, display a "Remove from cart" link.
|
| 317 |
*/
|
| 318 |
function ezshop_views_handler_field_node_remove($fieldinfo, $fielddata, $value, $data) {
|
| 319 |
return l(empty($fielddata['options']) ? t('Remove from cart') : $fielddata['options'], 'ezshop/remove/'. $data->nid);
|
| 320 |
}
|
| 321 |
|
| 322 |
/**
|
| 323 |
* Views handler callback, display the UID as a username (link with appropriate permissions).
|
| 324 |
*/
|
| 325 |
function ezshop_views_handler_field_uid_username($fieldinfo, $fielddata, $value, $data) {
|
| 326 |
$user = user_load(array('uid' => $value));
|
| 327 |
return theme_username($user);
|
| 328 |
}
|
| 329 |
|
| 330 |
/**
|
| 331 |
* Views handler callback, display the UID as is.
|
| 332 |
*/
|
| 333 |
function ezshop_views_handler_field_uid_uid($fieldinfo, $fielddata, $value, $data) {
|
| 334 |
return $value;
|
| 335 |
}
|
| 336 |
|
| 337 |
/**
|
| 338 |
* Implementation of hook_views_tables().
|
| 339 |
*/
|
| 340 |
function ezshop_views_tables() {
|
| 341 |
$ret['ezshop'] = array(
|
| 342 |
'name' => 'node',
|
| 343 |
'provider' => 'internal',
|
| 344 |
'fields' => array(
|
| 345 |
'cart' => array(
|
| 346 |
'name' => t('EZShop: Add to cart'),
|
| 347 |
'handler' => 'ezshop_views_handler_field_node_cart',
|
| 348 |
'sortable' => false,
|
| 349 |
'notafield' => 'true',
|
| 350 |
'help' => t('Display an "add to cart" link, fill the option field with the text for the link. Leave empty for %empty.', array('%empty' => t('Add to cart'))),
|
| 351 |
'option' => array(
|
| 352 |
'#type' => 'textfield',
|
| 353 |
'#size' => 10,
|
| 354 |
),
|
| 355 |
),
|
| 356 |
'remove' => array(
|
| 357 |
'name' => t('EZShop: Remove from cart'),
|
| 358 |
'handler' => 'ezshop_views_handler_field_node_remove',
|
| 359 |
'sortable' => false,
|
| 360 |
'notafield' => 'true',
|
| 361 |
'help' => t('Display a "remove from cart" link, fill the option field with the text for the link. Leave empty for %empty.', array('%empty' => t('Remove from cart'))),
|
| 362 |
'option' => array(
|
| 363 |
'#type' => 'textfield',
|
| 364 |
'#size' => 10,
|
| 365 |
),
|
| 366 |
),
|
| 367 |
),
|
| 368 |
);
|
| 369 |
$ret['ezshop_items'] = array(
|
| 370 |
'name' => 'ezshop_items',
|
| 371 |
'join' => array(
|
| 372 |
'left' => array(
|
| 373 |
'table' => 'node',
|
| 374 |
'field' => 'nid',
|
| 375 |
),
|
| 376 |
'right' => array(
|
| 377 |
'field' => 'nid'
|
| 378 |
),
|
| 379 |
),
|
| 380 |
'fields' => array(
|
| 381 |
'ezshop_uid' => array(
|
| 382 |
'name' => t('EZShop: User ID'),
|
| 383 |
'help' => t('User ID which EZShop items belong to.'),
|
| 384 |
'field' => 'uid',
|
| 385 |
'handler' => array(
|
| 386 |
'ezshop_views_handler_field_uid_username' => t('Username'),
|
| 387 |
'ezshop_views_handler_field_uid_uid' => t('User ID')
|
| 388 |
),
|
| 389 |
),
|
| 390 |
'ezshop_cnt_static' => array(
|
| 391 |
'name' => t('EZShop: Item count'),
|
| 392 |
'help' => t('Shows the item count that belongs to the user.'),
|
| 393 |
'field' => 'cnt',
|
| 394 |
),
|
| 395 |
),
|
| 396 |
'filters' => array(
|
| 397 |
'ezshop_uid' => array(
|
| 398 |
'name' => t('EZShop: Owner is Current User'),
|
| 399 |
'help' => t('User ID which EZShop items belong to.'),
|
| 400 |
'field' => 'uid',
|
| 401 |
'operator' => 'views_handler_operator_eqneq',
|
| 402 |
'list' => 'views_handler_filter_usercurrent',
|
| 403 |
'list-type' => 'select',
|
| 404 |
),
|
| 405 |
'ezshop_cnt' => array(
|
| 406 |
'name' => t('EZShop: Item Count'),
|
| 407 |
'help' => t('Item count in EZShop cart.'),
|
| 408 |
'field' => 'cnt',
|
| 409 |
'operator' => 'views_handler_operator_gtlt',
|
| 410 |
),
|
| 411 |
),
|
| 412 |
);
|
| 413 |
return $ret;
|
| 414 |
}
|
| 415 |
|
| 416 |
// vim: set ft=php syntax=php expandtab ts=2 sw=2 autoindent smartindent:
|