| 1 |
<?php
|
| 2 |
/**
|
| 3 |
* @file
|
| 4 |
* Various page callbacks.
|
| 5 |
*/
|
| 6 |
|
| 7 |
/**
|
| 8 |
* Product commissions settings form.
|
| 9 |
*/
|
| 10 |
function uc_affiliate_product_settings_form($form_state, $node) {
|
| 11 |
$form = array();
|
| 12 |
|
| 13 |
$form['descr'] = array(
|
| 14 |
'#value' => '<p>'.t('At this page you can setup global and per-user commissions for this product.
|
| 15 |
If nothing specified for user, will be used product\'s commission, if it not set either, will be
|
| 16 |
used <a href="!link">default affiliate commission</a>. You can specify either one-level values or
|
| 17 |
multi-level values separated by semicolons (<em>50%;20;10%;1</em>).',
|
| 18 |
array('!link' => url('admin/store/affiliate/settings'))).'</p>',
|
| 19 |
);
|
| 20 |
$form['nid'] = array(
|
| 21 |
'#type' => 'hidden',
|
| 22 |
'#value' => $node->nid,
|
| 23 |
);
|
| 24 |
$form['commissions'] = array(
|
| 25 |
'#type' => 'markup',
|
| 26 |
'#theme' => 'uc_affiliate_product_settings_table',
|
| 27 |
);
|
| 28 |
|
| 29 |
$commissions = uc_affiliate_get_product_commissions_data($node->nid, NULL, array('as_string' => TRUE));
|
| 30 |
|
| 31 |
$form['commissions']['global'] = array(
|
| 32 |
'#tree' => TRUE,
|
| 33 |
);
|
| 34 |
$form['commissions']['global']['name'] = array(
|
| 35 |
'#type' => 'item',
|
| 36 |
'#title' => t('Global product\'s settings'),
|
| 37 |
);
|
| 38 |
$form['commissions']['global']['allowed'] = array(
|
| 39 |
'#type' => 'select',
|
| 40 |
'#default_value' => isset($commissions[0]['allowed']) ? $commissions[0]['allowed'] : '',
|
| 41 |
'#options' => array(
|
| 42 |
PRODUCT_DEFAULT_FOR_AFFILIATES => t('<default>'),
|
| 43 |
PRODUCT_DISABLED_FOR_AFFILIATES => t('No'),
|
| 44 |
PRODUCT_ENABLED_FOR_AFFILIATES => 'Yes'
|
| 45 |
),
|
| 46 |
);
|
| 47 |
|
| 48 |
$form['commissions']['global']['amount'] = array(
|
| 49 |
'#type' => 'textfield',
|
| 50 |
'#default_value' => $commissions[0]['commission'],
|
| 51 |
'#size' => '15',
|
| 52 |
'#element_validate' => array('uc_affiliate_validate_commission'),
|
| 53 |
);
|
| 54 |
if (isset($commissions[0])) {
|
| 55 |
unset($commissions[0]);
|
| 56 |
}
|
| 57 |
|
| 58 |
if (!empty($commissions)) {
|
| 59 |
$form['commissions']['users'] = array(
|
| 60 |
'#tree' => TRUE,
|
| 61 |
);
|
| 62 |
foreach ($commissions as $item){
|
| 63 |
$form['commissions']['users'][$item['affid']]['delete'] = array(
|
| 64 |
'#type' => 'checkbox',
|
| 65 |
);
|
| 66 |
$form['commissions']['users'][$item['affid']]['name'] = array(
|
| 67 |
'#type' => 'hidden',
|
| 68 |
'#value' => $item['username'],
|
| 69 |
);
|
| 70 |
$form['commissions']['users'][$item['affid']]['name_title'] = array(
|
| 71 |
'#type' => 'item',
|
| 72 |
'#title' => l($item['username'], 'user/'. $item['affid'])
|
| 73 |
);
|
| 74 |
$form['commissions']['users'][$item['affid']]['allowed'] = array(
|
| 75 |
'#type' => 'select',
|
| 76 |
'#default_value' => $item['allowed'],
|
| 77 |
'#options' => array(
|
| 78 |
PRODUCT_DEFAULT_FOR_AFFILIATES => t('<default>'),
|
| 79 |
PRODUCT_DISABLED_FOR_AFFILIATES => t('No'),
|
| 80 |
PRODUCT_ENABLED_FOR_AFFILIATES => 'Yes'
|
| 81 |
),
|
| 82 |
);
|
| 83 |
$form['commissions']['users'][$item['affid']]['amount'] = array(
|
| 84 |
'#type' => 'textfield',
|
| 85 |
'#default_value' => $item['commission'],
|
| 86 |
'#size' => '15',
|
| 87 |
'#element_validate' => array('uc_affiliate_validate_commission'),
|
| 88 |
);
|
| 89 |
}
|
| 90 |
}
|
| 91 |
$form['commissions']['new'] = array(
|
| 92 |
'#tree' => TRUE,
|
| 93 |
);
|
| 94 |
$form['commissions']['new']['name'] = array(
|
| 95 |
'#type' => 'textfield',
|
| 96 |
'#autocomplete_path' => 'user/affiliate/autocomplete',
|
| 97 |
'#size' => '20',
|
| 98 |
);
|
| 99 |
$form['commissions']['new']['allowed'] = array(
|
| 100 |
'#type' => 'select',
|
| 101 |
'#options' => array(
|
| 102 |
PRODUCT_DEFAULT_FOR_AFFILIATES => t('<default>'),
|
| 103 |
PRODUCT_DISABLED_FOR_AFFILIATES => t('No'),
|
| 104 |
PRODUCT_ENABLED_FOR_AFFILIATES => 'Yes'
|
| 105 |
),
|
| 106 |
);
|
| 107 |
$form['commissions']['new']['amount'] = array(
|
| 108 |
'#type' => 'textfield',
|
| 109 |
'#size' => 15,
|
| 110 |
'#element_validate' => array('uc_affiliate_validate_commission'),
|
| 111 |
);
|
| 112 |
|
| 113 |
$form['submit'] = array(
|
| 114 |
'#type' => 'submit',
|
| 115 |
'#value' => t('Save changes'),
|
| 116 |
);
|
| 117 |
|
| 118 |
if (!empty($commissions)) {
|
| 119 |
$form['delete'] = array(
|
| 120 |
'#type' => 'submit',
|
| 121 |
'#value' => t('Remove selected'),
|
| 122 |
);
|
| 123 |
}
|
| 124 |
|
| 125 |
return $form;
|
| 126 |
}
|
| 127 |
|
| 128 |
/**
|
| 129 |
* Validate product commissons.
|
| 130 |
*/
|
| 131 |
function uc_affiliate_product_settings_form_validate($form, &$form_state) {
|
| 132 |
$node = node_load($form_state['values']['nid']);
|
| 133 |
if ($node->type != 'product') {
|
| 134 |
form_set_error('nid', t('Node is not a product.'));
|
| 135 |
}
|
| 136 |
if (isset($form_state['values']['new']) && ($form_state['values']['new']['name'])) {
|
| 137 |
$result = db_query('SELECT DISTINCT(u.uid) FROM {role} r INNER JOIN {permission} p ON p.rid = r.rid INNER JOIN {users_roles} ur ON ur.rid = r.rid INNER JOIN {users} u ON u.uid = ur.uid WHERE p.perm LIKE "%act as affiliate%" AND u.name = "%s"', $form_state['values']['new']['name']);
|
| 138 |
if (!db_result($result)) {
|
| 139 |
form_set_error('new[name]', t('User <em>@user</em> is not affiliate.', array('@user' => $form_state['values']['new']['name'])));
|
| 140 |
}
|
| 141 |
else {
|
| 142 |
foreach ($form_state['values']['users'] as $user) {
|
| 143 |
if ($user['name'] == $form_state['values']['new']['name']) {
|
| 144 |
form_set_error('new[name]', t('Commissions for <em>@user</em> is already present for this product.', array('@user' => $form_state['values']['new']['name'])));
|
| 145 |
}
|
| 146 |
}
|
| 147 |
}
|
| 148 |
}
|
| 149 |
}
|
| 150 |
|
| 151 |
/**
|
| 152 |
* Save product commissons.
|
| 153 |
*/
|
| 154 |
function uc_affiliate_product_settings_form_submit($form, &$form_state) {
|
| 155 |
$nid = $form_state['values']['nid'];
|
| 156 |
|
| 157 |
if (isset($form_state['values']['global'])){
|
| 158 |
$data[$nid][0] = array(
|
| 159 |
'commission' => uc_affiliate_parse_commission($form_state['values']['global']['amount']),
|
| 160 |
'allowed' => $form_state['values']['global']['allowed'],
|
| 161 |
);
|
| 162 |
}
|
| 163 |
|
| 164 |
if (isset($form_state['values']['new']) && $form_state['values']['new']['name']){
|
| 165 |
$affid = db_result(db_query('SELECT uid FROM {users} WHERE name="%s"', $form_state['values']['new']['name']));
|
| 166 |
$data[$nid][$affid] = array(
|
| 167 |
'commission' => uc_affiliate_parse_commission($form_state['values']['new']['amount']),
|
| 168 |
'allowed' => $form_state['values']['new']['allowed'],
|
| 169 |
);
|
| 170 |
}
|
| 171 |
|
| 172 |
if (!empty($form_state['values']['users'])) {
|
| 173 |
foreach ($form_state['values']['users'] as $user['affid'] => $user) {
|
| 174 |
if (!$user['delete']) {
|
| 175 |
$data[$nid][$user['affid']] = array(
|
| 176 |
'commission' => uc_affiliate_parse_commission($user['amount']),
|
| 177 |
'allowed' => $user['allowed'],
|
| 178 |
);
|
| 179 |
}
|
| 180 |
}
|
| 181 |
}
|
| 182 |
|
| 183 |
// Save data to DB.
|
| 184 |
uc_affiliate_set_product_commissions_data($data);
|
| 185 |
}
|
| 186 |
|
| 187 |
/**
|
| 188 |
* Theme product settings table.
|
| 189 |
*/
|
| 190 |
function theme_uc_affiliate_product_settings_table($element){
|
| 191 |
//Adding header of table and "Global commission" row
|
| 192 |
$header = array(
|
| 193 |
'',
|
| 194 |
'',
|
| 195 |
t('Allowed for affiliate(s)'),
|
| 196 |
t('Commission')
|
| 197 |
);
|
| 198 |
$rows = array();
|
| 199 |
$rows[] = array(
|
| 200 |
'data' => array(array(
|
| 201 |
'data' => '<h4>'.t('Global product settings').'</h4>',
|
| 202 |
'colspan' => 4,
|
| 203 |
)),
|
| 204 |
'id' => 'users_commission_title',
|
| 205 |
'class' => 'transparent',
|
| 206 |
);
|
| 207 |
|
| 208 |
$rows[] = array(
|
| 209 |
'',
|
| 210 |
'',
|
| 211 |
drupal_render($element['global']['allowed']),
|
| 212 |
drupal_render($element['global']['amount']),
|
| 213 |
);
|
| 214 |
|
| 215 |
// Adding the list of per-user commissions
|
| 216 |
$rows[] = array(
|
| 217 |
'data' => array(array(
|
| 218 |
'data' => '<h4>'.t('Per-affiliate commissions').'</h4>',
|
| 219 |
'colspan' => 4,
|
| 220 |
)),
|
| 221 |
'id' => 'users_commission_title',
|
| 222 |
'class' => 'transparent',
|
| 223 |
);
|
| 224 |
|
| 225 |
foreach (element_children($element['users']) as $key){
|
| 226 |
$rows[] = array(
|
| 227 |
drupal_render($element['users'][$key]['delete']),
|
| 228 |
drupal_render($element['users'][$key]['name']) . drupal_render($element['users'][$key]['name_title']),
|
| 229 |
drupal_render($element['users'][$key]['allowed']),
|
| 230 |
drupal_render($element['users'][$key]['amount']),
|
| 231 |
);
|
| 232 |
}
|
| 233 |
|
| 234 |
// New
|
| 235 |
$rows[] = array(
|
| 236 |
'data' => array(
|
| 237 |
'',
|
| 238 |
drupal_render($element['new']['name']),
|
| 239 |
drupal_render($element['new']['allowed']),
|
| 240 |
drupal_render($element['new']['amount']),
|
| 241 |
),
|
| 242 |
'id' => 'new_commission',
|
| 243 |
);
|
| 244 |
|
| 245 |
$output .= theme('table', $header, $rows);
|
| 246 |
|
| 247 |
$path = drupal_get_path('module', 'uc_affiliate');
|
| 248 |
drupal_add_css($path .'/uc_affiliate.css');
|
| 249 |
|
| 250 |
return $output;
|
| 251 |
}
|
| 252 |
|
| 253 |
|
| 254 |
/**
|
| 255 |
* Display dashboard for affiliate.
|
| 256 |
*/
|
| 257 |
function uc_affiliate_dashboard($affiliate) {
|
| 258 |
global $user;
|
| 259 |
|
| 260 |
$is_active = uc_affiliate_is_affiliate_active($affiliate->uid);
|
| 261 |
|
| 262 |
$dashboard = array();
|
| 263 |
foreach (module_implements('affiliate_dashboard') as $module) {
|
| 264 |
$function = $module .'_affiliate_dashboard';
|
| 265 |
$function($affiliate, $dashboard, $is_active);
|
| 266 |
}
|
| 267 |
|
| 268 |
$output = implode('', $dashboard);
|
| 269 |
|
| 270 |
drupal_set_title(t('My Business'));
|
| 271 |
return $output;
|
| 272 |
}
|
| 273 |
|
| 274 |
/**
|
| 275 |
* Implementation of hook_affiliate_dashboard().
|
| 276 |
*
|
| 277 |
* @param object $affiliate
|
| 278 |
* Affiliate object.
|
| 279 |
* @param array $dashboard
|
| 280 |
* Dashboard content array. Acts just like $node->content.
|
| 281 |
* @param bool $is_active
|
| 282 |
* Determines affiliate's activity during last three months.
|
| 283 |
*/
|
| 284 |
function uc_affiliate_affiliate_dashboard($affiliate, &$dashboard, $is_active) {
|
| 285 |
if ($is_active) {
|
| 286 |
$_GET['period'] = 'last7';
|
| 287 |
module_load_include('admin.inc', 'uc_affiliate');
|
| 288 |
$dashboard['stats'] .= '<h3>'. t('Last 7 days summary') .'</h3>';
|
| 289 |
$stats = uc_affiliate_admin_daily_reports($affiliate, TRUE);
|
| 290 |
if (!empty($stats)) {
|
| 291 |
$dashboard['stats'] .= $stats;
|
| 292 |
}
|
| 293 |
else {
|
| 294 |
$dashboard['stats'] .= '<p>'. t('No activity occurred during last week.') .'</p>';
|
| 295 |
}
|
| 296 |
}
|
| 297 |
else {
|
| 298 |
$dashboard['stats'] .= '<p>'. t('Wandering how to start? Grab your affiliate links from <a href="!url">Link Generator</a>, place them at your site and start making the cash.', array('!url' => url('user/'. $affiliate->uid .'/affiliate/links'))) .'</p>';
|
| 299 |
}
|
| 300 |
}
|
| 301 |
|
| 302 |
/**
|
| 303 |
* Display affiliate commission records.
|
| 304 |
*/
|
| 305 |
function uc_affiliate_reports($affiliate) {
|
| 306 |
global $user;
|
| 307 |
|
| 308 |
module_load_include('admin.inc', 'uc_affiliate');
|
| 309 |
$output = uc_affiliate_admin_daily_reports($affiliate);
|
| 310 |
|
| 311 |
if ($user->uid == $affiliate->uid) {
|
| 312 |
drupal_set_title(t('Stats'));
|
| 313 |
}
|
| 314 |
else {
|
| 315 |
drupal_set_title(t('@user\'s stats', array('@user' => $affiliate->name)));
|
| 316 |
}
|
| 317 |
|
| 318 |
return $output;
|
| 319 |
}
|
| 320 |
|
| 321 |
/**
|
| 322 |
* Affiliate link generation form.
|
| 323 |
*/
|
| 324 |
function uc_affiliate_link_generator_form($form_state, $affiliate = NULL) {
|
| 325 |
global $user;
|
| 326 |
|
| 327 |
$form['filter'] = array(
|
| 328 |
'#type' => 'fieldset',
|
| 329 |
'#title' => t('Choose product'),
|
| 330 |
'#attributes' => array('class' => 'container-inline'),
|
| 331 |
);
|
| 332 |
|
| 333 |
$products = uc_affiliate_get_affiliate_available_products($affiliate->uid);
|
| 334 |
|
| 335 |
if (!empty($products)) {
|
| 336 |
$default_product = 0;
|
| 337 |
|
| 338 |
// Product from submit data
|
| 339 |
if (isset($form_state['values']['product'])) {
|
| 340 |
$default_product = $form_state['values']['product'];
|
| 341 |
}
|
| 342 |
// Product from GET
|
| 343 |
elseif (isset($_GET['product'])) {
|
| 344 |
$default_product = $_GET['product'];
|
| 345 |
}
|
| 346 |
// There are no need to annoy user, if there is only one product available,
|
| 347 |
// we can use this single product as default selected.
|
| 348 |
elseif (count($products) == 1) {
|
| 349 |
$default_product = key($products);
|
| 350 |
}
|
| 351 |
|
| 352 |
$form['filter']['product'] = array(
|
| 353 |
'#type' => 'select',
|
| 354 |
'#default_value' => $default_product,
|
| 355 |
'#options' => array(
|
| 356 |
0 => t('Please, choose the product'),
|
| 357 |
),
|
| 358 |
'#attributes' => array('onchange'=>'if (this.value != 0) { this.form.submit(); }'),
|
| 359 |
);
|
| 360 |
foreach ($products as $key => $product){
|
| 361 |
$form['filter']['product']['#options'][$key] = $product['title'];
|
| 362 |
}
|
| 363 |
$form['filter']['submit'] = array(
|
| 364 |
'#type' => 'submit',
|
| 365 |
'#value' => t('Submit'),
|
| 366 |
'#suffix' => '<script type="text/javascript">$("#uc-affiliate-link-generator-form #edit-submit").css("display", "none");</script>',
|
| 367 |
);
|
| 368 |
}
|
| 369 |
else {
|
| 370 |
$form['filter']['product'] = array(
|
| 371 |
'#type' => 'item',
|
| 372 |
'#value' => t('Sorry, there are no available products at this time.'),
|
| 373 |
);
|
| 374 |
}
|
| 375 |
|
| 376 |
if ($default_product) {
|
| 377 |
$commission = uc_affiliate_get_product_commissions_data($default_product, $affiliate, array('level' => 0));
|
| 378 |
|
| 379 |
$form['link'] = array(
|
| 380 |
'#type' => 'textfield',
|
| 381 |
'#title' => t('Your affiliate link for this product'),
|
| 382 |
'#description' => t('You will get !commission for every sale of this product.', array('!commission' => uc_affiliate_commission2str($commission, TRUE))),
|
| 383 |
'#default_value' => url('node/'. $default_product, array('query' => 'a='. $affiliate->uid, 'absolute' => TRUE)),
|
| 384 |
'#attributes' => array(
|
| 385 |
'class' => 'autoselect',
|
| 386 |
),
|
| 387 |
);
|
| 388 |
}
|
| 389 |
|
| 390 |
if ($user->uid != $affiliate->uid) {
|
| 391 |
drupal_set_title(t('@user\'s links', array('@user' => $affiliate->name)));
|
| 392 |
}
|
| 393 |
else {
|
| 394 |
drupal_set_title(t('Links'));
|
| 395 |
}
|
| 396 |
|
| 397 |
$path = drupal_get_path('module', 'uc_affiliate');
|
| 398 |
drupal_add_js($path.'/uc_affiliate.js', 'module');
|
| 399 |
|
| 400 |
return $form;
|
| 401 |
}
|
| 402 |
|
| 403 |
/**
|
| 404 |
* Link generator's submit handler.
|
| 405 |
*/
|
| 406 |
function uc_affiliate_link_generator_form_submit($form, &$form_state) {
|
| 407 |
if ($form_state['values']['product']) {
|
| 408 |
$form_state['rebuild'] = TRUE;
|
| 409 |
}
|
| 410 |
}
|
| 411 |
|
| 412 |
/**
|
| 413 |
* Retrieve a pipe delimited string of autocomplete suggestions for existing affiliates
|
| 414 |
*/
|
| 415 |
function uc_affiliate_autocomplete($string = '') {
|
| 416 |
$matches = array();
|
| 417 |
if ($string) {
|
| 418 |
$result = db_query_range("SELECT DISTINCT(u.name) FROM {role} r INNER JOIN {permission} p ON p.rid = r.rid INNER JOIN {users_roles} ur ON ur.rid = r.rid INNER JOIN {users} u ON u.uid = ur.uid WHERE p.perm LIKE '%act as affiliate%' AND LOWER(u.name) LIKE LOWER('%s%%')", $string, 0, 10);
|
| 419 |
while ($user = db_fetch_object($result)) {
|
| 420 |
$matches[$user->name] = check_plain($user->name);
|
| 421 |
}
|
| 422 |
}
|
| 423 |
drupal_json($matches);
|
| 424 |
}
|