/[drupal]/contributions/modules/affiliate_products_shop/affiliate_products_shop.module
ViewVC logotype

Contents of /contributions/modules/affiliate_products_shop/affiliate_products_shop.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download) (as text)
Fri Jan 25 14:19:53 2008 UTC (22 months ago) by evakoss
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
Changes since 1.1: +3 -3 lines
File MIME type: text/x-php
*** empty log message ***
1 <?php
2 // $Id: affiliate_products_shop.module,v 1.1 2007/12/29 09:25:18 evakoss Exp $
3
4 /**
5 * @file
6 * Enables users to submit affiliate products.
7 */
8 include_once 'lib/nusoap.php';
9 /**
10 * Implementation of hook_help().
11 */
12 function affiliate_products_shop_help($section) {
13 switch ($section) {
14 case 'admin/modules#description':
15 return t('Allow user to manage feeds from CJ3 and Link Share.');
16 }
17 }
18
19
20 /**
21 * Implementation of hook_menu().
22 */
23 function affiliate_products_shop_menu($may_cache) {
24 global $user, $base_path;
25 $access = user_access("administer affiliate_products_shop");
26 $user_access = user_access('access content');
27 $items = array();
28 if ($may_cache)
29 {
30 $items[] = array(
31 'path' => 'admin/affiliate',
32 'title' => t('Affiliate Management'),
33 'description' => t('Manage Feed Systems'),
34 'position' => 'right',
35 'weight' => -5,
36 'callback' => 'system_admin_menu_block_page',
37 'access' => user_access('administer site configuration')
38 );
39 $items[] = array(
40 'path' => 'admin/affiliate/affiliate_products_shop',
41 'title' => t('Affiliate Feeds'),
42 'description' => t('Add / Edit / Delete New CJ3 and Link Share Feed(s).'),
43 'callback' => 'affiliate_feeds_admin_form',
44 'access' => $access,
45 'weight' => 0
46 );
47 $items[] = array(
48 'path' => 'admin/affiliate/affiliate_products_merchant_form',
49 'title' => t('Add Merchant'),
50 'description' => t('Add Merchant Manually.'),
51 'callback' => 'drupal_get_form',
52 'callback arguments' => array('affiliate_merchant_admin_form'),
53 'access' => $access,
54 'weight' => 1
55 );
56
57 $items[] = array(
58 'path' => 'admin/affiliate/affiliate_products_coupon_form',
59 'title' => t('Add CJ3 and Link Share Coupon(s)'),
60 'description' => t('Add Coupon(s) Manually.'),
61 'callback' => 'drupal_get_form',
62 'callback arguments' => array('affiliate_coupon_admin_form'),
63 'access' => $access,
64 'weight' => 2
65 );
66 $items[] = array(
67 'path' => 'admin/affiliate/affiliate_shop_settings',
68 'title' => t('Settings'),
69 'callback' => 'drupal_get_form',
70 'callback arguments' => array('affiliate_setting_admin_form'),
71 'access' => $access,
72 'weight' => 2
73 );
74 $items[] = array(
75 'path' => 'admin/affiliate/affiliate_products_shop/add',
76 'title' => t('Add Affiliate Feeds'),
77 'callback' => 'affiliate_feeds_admin_form',
78 'access' => $access,
79 'type' => MENU_DEFAULT_LOCAL_TASK,
80 'weight' => 1
81 );
82 $items[] = array(
83 'path' => 'admin/affiliate/affiliate_products_shop/view',
84 'title' => t('Active Affiliate Feeds'),
85 'callback' => 'affiliate_feeds_view',
86 'callback arguments' => array(1),
87 'access' => $access,
88 'type' => MENU_LOCAL_TASK,
89 'weight' => 2
90 );
91 $items[] = array(
92 'path' => 'admin/affiliate/affiliate_products_shop/view_deactive',
93 'title' => t('Deactive Affiliate Feeds'),
94 'callback' => 'affiliate_feeds_view',
95 'callback arguments' => array(2),
96 'access' => $access,
97 'type' => MENU_LOCAL_TASK,
98 'weight' => 3
99 );
100 $items[] = array(
101 'path' => 'admin/affiliate/affiliate_products_merchant_delete',
102 'callback' => 'drupal_get_form',
103 'callback arguments' => array('merchant_delete'),
104 'type' => MENU_SUGGESTED_ITEM,
105 'access' => $access,
106 );
107 $items[] = array(
108 'path' => 'admin/affiliate/affiliate_products_coupon_delete',
109 'callback' => 'drupal_get_form',
110 'type' => MENU_SUGGESTED_ITEM,
111 'callback arguments' => array('coupon_delete'),
112 'access' => $access,
113 );
114 $items[] = array(
115 'path' => 'merchants',
116 'title' => t('Affiliate Merchants'),
117 'callback' => 'affiliate_merchants',
118 'access' => $user_access,
119 'type' => MENU_NORMAL_ITEM
120 );
121 } else {
122 $merchant_list = _get_all_merchants();
123 foreach($merchant_list as $key => $val)
124 {
125 $items[] = array('path' => 'merchants/'.$key,
126 'title' => t($val),
127 'callback' => 'affiliate_merchants_vise',
128 'callback arguments' => array($key),
129 'access' => $user_access,
130 'type' => MENU_CALLBACK,);
131 }
132 }
133 return $items;
134 }
135
136 /* show merchants and feeds */
137
138 function _get_merchant_alphabatically($row,$val) {
139 global $base_url;
140 $out = '<p><a name="'.strtoupper($val).'"><h3>'.strtoupper($val).'</h3></a>';
141 foreach($row as $key => $value)
142 {
143 $substr = substr($value,0,1);
144 if(strtolower($substr) == strtolower($val) && !is_numeric($substr))
145 {
146 $out .= l($value,'merchants/'.$key).'<br>';
147 }
148 }
149 $out .= '</p>';
150 return $out;
151 }
152
153 function affiliate_merchants() {
154 drupal_set_title(t('Merchants List'));
155 $alpha_arr = array('#','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
156 $output = '<br><br>Following is a list of all merchants.';
157 $output .= '<p>';
158 foreach($alpha_arr as $value) {
159 if($value == '#') {
160 $output .= '<a href="##">#</a>&nbsp;&nbsp;';
161 } else {
162 $val = strtoupper($value);
163 $output .= '<a href="#'.$val.'">'.$val.'</a>&nbsp;&nbsp;';
164 }
165 }
166 $output .= '</p><p>&nbsp;</p>';
167 $row = _get_all_merchants(1);
168 foreach($alpha_arr as $val)
169 {
170 $output .= _get_merchant_alphabatically($row,$val);
171 }
172 return $output;
173 }
174
175 /**
176 * will show coupons, merchant vise
177 **/
178
179 function _get_merchant_logo($logo = '', $logo_alt = '') {
180 global $base_url;
181 if(file_exists($logo)) {
182 $image = theme('image', $logo, t($logo_alt), t($logo_alt));
183 return $image;
184 }
185 return false;
186 }
187
188 function _get_strip_ext($domain,$ext) {
189 if(strpos($domain,$ext))
190 {
191 $exp_dom = explode($ext,$domain);
192 $domain = $exp_dom[0];
193 }
194 return $domain;
195 }
196
197 function affiliate_merchants_vise($merchant_id) {
198 global $base_url;
199 drupal_add_css(drupal_get_path('module', 'affiliate_products_shop') .'/affiliate_products_shop.css');
200 $output = '';
201 $merchant_result = db_query("SELECT * FROM {affiliate_manager_merchants} WHERE merchant_id = '%s'",$merchant_id);
202 $row_merchant = db_fetch_object($merchant_result);
203 $output .= '<div><p>';
204 if(!empty($row_merchant->merchant_home_page_link)){
205 $astart = '<a href="'.$row_merchant->merchant_home_page_link.'" target="_blank">';
206 $aend = '</a>';
207 }
208 $output .= $astart;
209 $output .= _get_merchant_logo($row_merchant->merchant_logo,$row_merchant->merchant_logo_alt);
210 $output .= $aend;
211 if($aend) {
212 $output .= '<br><br>Click here to go to '.$astart._get_strip_ext($row_merchant->merchant_name,'.com').$aend.'<br><br>';
213 }
214 $output .= '</p></div>';
215
216 $count_query = "SELECT COUNT(coupon_id) FROM {affiliate_manager_coupon} WHERE coupon_status = 1 AND coupon_merchant_id = '".$merchant_id."'";
217 $pager_limit = 20;
218 $sql = "SELECT * FROM {affiliate_manager_coupon} WHERE coupon_status = 1 AND coupon_merchant_id = '".$merchant_id."'";
219 $coupon_result = pager_query($sql,$pager_limit, 0, $count_query);
220
221 if(db_num_rows($coupon_result) > 0)
222 {
223 $output .= '<div class="coupon_start">';
224 $bg = '#FFFFFF';
225 while($row_coupon = db_fetch_object($coupon_result))
226 {
227 $output .= '';
228 $output .= '<div class = "inner_div" style="background:'.$bg.';">';
229 $output .= '<img src="'.$base_url.'/'.drupal_get_path('module', 'affiliate_products_shop').'/star.png" border="0" align="left" class="star_img" >';
230 $display_text = (!empty($row_coupon->coupon_display_text)) ? $row_coupon->coupon_display_text : $row_coupon->coupon_link_name;
231 $output .= '<a href = "'.$row_coupon->coupon_link_url.'" target = "_blank">'.$display_text.'</a>';
232 if(!empty($row_coupon->coupon_track_url))
233 {
234 $output .= '<img src="'.$row_coupon->coupon_track_url.'" width="1" height="1" border="0"/>';
235 }
236 $output .= '</div>';
237 $bg = ($bg == '#FFFFFF') ? '#DFE7F1' : '#FFFFFF';
238 }
239 $output .= '</div>';
240 $output .= theme('pager', NULL, $pager_limit, 0);
241 } else {
242 $output = '<br><br>Currently, we do not have any coupons or deals for <b>'.ucfirst($row_merchant->merchant_name).'</b>';
243 }
244 if(empty($output))
245 {
246 $output = '<br><br>Currently, we do not have any coupons or deals for <b>'.ucfirst($row_merchant->merchant_name).'</b>';
247 }
248 if(!empty($row_merchant->merchant_description)) {
249 $output .= '<p class = "mer_desc">Description:</p><div class = "mer_div_desc">'.$row_merchant->merchant_description.'</div>';
250 }
251 return $output;
252 }
253
254 /*
255 work on admin part
256 */
257 function affiliate_feeds_admin_form() {
258 $output = drupal_get_form('affiliate_feeds_linkshare_form');
259 $output .= drupal_get_form('affiliate_feeds_cj3_form');
260 return $output;
261 }
262
263 function _get_content_merchant_types() {
264 return array(
265 'merchant_name' => "'%s'",
266 'merchant_id' => "'%s'",
267 'merchant_home_page_link' => "'%s'",
268 'merchant_description' => "'%s'",
269 'merchant_logo' => "'%s'",
270 'merchant_logo_alt' => "'%s'",
271 'merchant_status' => '%d'
272 );
273 }
274
275 function _get_content_merchant_values($data) {
276 return array(
277 'merchant_name' => $data->mname,
278 'merchant_id' => $data->mid,
279 'merchant_home_page_link' => $data->home_page_link,
280 'merchant_description' => $data->desc,
281 'merchant_logo' => $data->logo,
282 'merchant_logo_alt' => $data->alt,
283 'merchant_status' => $data->edmode
284 );
285 }
286
287 function _get_content_coupon_types() {
288 return array(
289 'coupon_link_id' => "'%s'",
290 'coupon_merchant_id' => "'%s'",
291 'coupon_category' => "'%s'",
292 'coupon_link_name' => "'%s'",
293 'coupon_display_text' => "'%s'",
294 'coupon_link_url' => "'%s'",
295 'coupon_track_url' => "'%s'",
296 'coupon_type' => "'%s'",
297 'coupon_status' => '%d'
298 );
299 }
300
301 function _get_content_coupon_values($data) {
302 return array(
303 'coupon_link_id' => $data->linkID,
304 'coupon_merchant_id' => $data->mid,
305 'coupon_category' => $data->category,
306 'coupon_link_name' => $data->linkName,
307 'coupon_display_text' => $data->displayText,
308 'coupon_link_url' => $data->linkUrl,
309 'coupon_track_url' => $data->trackUrl,
310 'coupon_type' => $data->feed_type,
311 'coupon_status' => $data->edmode
312 );
313 }
314
315 function _get_linkhtml($linkCodeHTML,$case){
316 switch($case)
317 {
318 case "link_url":
319 $ex_link = explode('">',$linkCodeHTML);
320 $linkurl = explode('<a href="',$ex_link[0]);
321 return $linkurl[1];
322 break;
323 case "link_text":
324 $ex_link = explode('">',$linkCodeHTML);
325 $ex_mtext = explode('</a>',$ex_link[1]);
326 return $ex_mtext[0];
327 break;
328 case "track_url":
329 $ex_tc_url = explode('src="',$linkCodeHTML);
330 $ex_trackurl = explode('"',$ex_tc_url[1]);
331 return $ex_trackurl[0];
332 break;
333 }
334 return;
335 }
336
337 /* LINK SHARE ADMIN FEED FORM */
338 function affiliate_feeds_linkshare_form() {
339 global $base_url;
340 $form = array();
341 $form['linkshare'] = array(
342 '#type' => 'fieldset',
343 '#title' => t('LINK SHARE FEEDS'),
344 '#weight' => 0,
345 '#collapsible' => TRUE,
346 '#collapsed' => TRUE,
347 );
348 $form['linkshare']['linkshare_username'] = array(
349 '#type' => 'textfield',
350 '#title' => t('Link Share Username'),
351 '#maxlength' => 255,
352 '#default_value' => '',
353 '#description' => t("Your Link share username."),
354 '#required' => TRUE,
355 );
356 $form['linkshare']['linkshare_password'] = array(
357 '#type' => 'password',
358 '#title' => t('Link Share Password'),
359 '#maxlength' => 255,
360 '#default_value' => '',
361 '#description' => t("Your Link share password."),
362 '#required' => TRUE,
363 );
364 $form['linkshare']['linkshare_sid'] = array(
365 '#type' => 'textfield',
366 '#title' => t('Link Share Sid'),
367 '#maxlength' => 255,
368 '#default_value' => '',
369 '#description' => t("Your SID for Link share feeds."),
370 '#required' => TRUE,
371 );
372 $form['linkshare']['linkshare_wsdl_feed'] = array(
373 '#type' => 'textfield',
374 '#title' => t('Link Share Feed URL'),
375 '#maxlength' => 255,
376 '#default_value' => 'http://apps.linksynergy.com/ws/services/TextPromotion?wsdl',
377 '#description' => t("Your link share feed url i.e.: http://apps.linksynergy.com/ws/services/TextPromotion?wsdl"),
378 '#required' => TRUE,
379 );
380 $form['linkshare']['link_share_submit'] = array('#type' => 'submit',
381 '#value' => t('Submit'),
382 );
383 return $form;
384 }
385
386 /* CJ3 ADMIN FEED FORM */
387 function affiliate_feeds_cj3_form() {
388 global $base_url;
389 $form = array();
390 $form['cj3'] = array(
391 '#type' => 'fieldset',
392 '#title' => t('COMMISSION JUNCTION FEEDS'),
393 '#weight' => 0,
394 '#collapsible' => TRUE,
395 '#collapsed' => TRUE,
396 );
397 $form['cj3']['cj3_developerkey'] = array(
398 '#type' => 'textfield',
399 '#title' => t('CJ3 Developer Key'),
400 '#maxlength' => 500,
401 '#default_value' => '',
402 '#description' => t("Developer Key for Commission Junction feeds."),
403 '#required' => TRUE,
404 );
405 $form['cj3']['cj3_websiteid'] = array(
406 '#type' => 'textfield',
407 '#title' => t('CJ3 Website ID'),
408 '#maxlength' => 255,
409 '#default_value' => '',
410 '#description' => t("Website Id for Commission Junction feeds."),
411 '#required' => TRUE,
412 );
413 $form['cj3']['cj3_wsdl_feed'] = array(
414 '#type' => 'textfield',
415 '#title' => t('CJ3 Feed URL'),
416 '#maxlength' => 255,
417 '#default_value' => 'https://linksearch.api.cj.com/wsdl/rpc_encoded/linkSearchService.wsdl',
418 '#description' => t("Your commission junction feed url i.e.: https://linksearch.api.cj.com/wsdl/rpc_encoded/linkSearchService.wsdl"),
419 '#required' => TRUE,
420 );
421 $form['cj3']['cj3_token'] = array(
422 '#type' => 'textfield',
423 '#title' => t('CJ3 Token ID'),
424 '#maxlength' => 255,
425 '#default_value' => '',
426 '#description' => t("Token Id for Commission Junction feeds."),
427 '#required' => FALSE,
428 );
429 $form['cj3']['cj3_advertisercids'] = array(
430 '#type' => 'textfield',
431 '#title' => t('CJ3 Advertisers CId'),
432 '#maxlength' => 255,
433 '#default_value' => '',
434 '#description' => "*&nbsp;CIDs: You may provide list of one or more advertiser CIDs, separated by commas, to limit the results to a specific sub-set of merchants.<br>*&nbsp;Empty String: Leaving this field blank returns results for all advertisers - both joined and not-joined.<br>*&nbsp;joined: This special value (joined) restricts the search to advertisers with which you have a relationship.<br>*&nbsp;notjoined: This special value (notjoined) restricts the search to advertisers with which you do not have a relationship.",
435 '#required' => FALSE,
436 );
437 $form['cj3']['cj3_keywords'] = array(
438 '#type' => 'textfield',
439 '#title' => t('CJ3 Keywords'),
440 '#maxlength' => 255,
441 '#default_value' => '',
442 '#description' => t("Keywords for restricts the result feeds based on keywords found in the link's name or description."),
443 '#required' => FALSE,
444 );
445 $form['cj3']['cj3_category'] = array(
446 '#type' => 'textfield',
447 '#title' => t('CJ3 Category'),
448 '#maxlength' => 255,
449 '#default_value' => '',
450 '#description' => t("<b>Important:</b> The system does not support searching top-level categories. You must choose a specific sub-category in which to search. For example, if you want links for medical equipment, you must use Equipment (the sub-category) as the value of the category parameter."),
451 '#required' => FALSE,
452 );
453 $form['cj3']['cj3_linktype'] = array(
454 '#type' => 'textfield',
455 '#title' => t('CJ3 Link Type'),
456 '#maxlength' => 255,
457 '#default_value' => 'Text Link',
458 '#description' => t("Limits the results to links of a specific link type."),
459 '#required' => FALSE,
460 );
461 $form['cj3']['cj3_linksize'] = array(
462 '#type' => 'textfield',
463 '#title' => t('CJ3 Link Size'),
464 '#maxlength' => 255,
465 '#default_value' => '',
466 '#description' => t("Limits the results to links with a specific link size."),
467 '#required' => FALSE,
468 );
469 $form['cj3']['cj3_language'] = array(
470 '#type' => 'textfield',
471 '#title' => t('CJ3 Language'),
472 '#maxlength' => 255,
473 '#default_value' => '',
474 '#description' => t("Limits the results to links offered in a specific language. Default language is ENGLISH, Leave blank for default."),
475 '#required' => FALSE,
476 );
477 $form['cj3']['cj3_serviceablearea'] = array(
478 '#type' => 'textfield',
479 '#title' => t('CJ3 Serviceable Area'),
480 '#maxlength' => 255,
481 '#default_value' => '',
482 '#description' => t("Limits the results to links offered by advertisers with a specific targeted area or targeted areas. Separate multiple values with a comma."),
483 '#required' => FALSE,
484 );
485 $form['cj3']['cj3_sortby'] = array(
486 '#type' => 'textfield',
487 '#title' => t('CJ3 Sort By'),
488 '#maxlength' => 255,
489 '#default_value' => '',
490 '#description' => t("Sort the results by any of the following: Name, AdvertiserID, AdvertiserName, Destination, Medium, Width, Height, Cid."),
491 '#required' => FALSE,
492 );
493 $form['cj3']['cj3_sortorder'] = array(
494 '#type' => 'textfield',
495 '#title' => t('CJ3 Sort Order'),
496 '#maxlength' => 255,
497 '#default_value' => '',
498 '#description' => t("Specifies the order in which the results are sorted; the following values are acceptable:- <b>asc:</b> ascending, <b>dec:</b> descending [default value]"),
499 '#required' => FALSE,
500 );
501 $form['cj3']['cj3_startat'] = array(
502 '#type' => 'textfield',
503 '#title' => t('CJ3 Start At'),
504 '#maxlength' => 255,
505 '#default_value' => '0',
506 '#description' => t("Specifies the first record to return in the request. The Default first record is 0."),
507 '#required' => FALSE,
508 );
509 $form['cj3']['cj3_maxresults'] = array(
510 '#type' => 'textfield',
511 '#title' => t('CJ3 Max Results'),
512 '#maxlength' => 255,
513 '#default_value' => '10',
514 '#description' => t("<b>Note:</b> 100 results is the system limit for results per request. If you request a value greater than 100, the system only returns 100."),
515 '#required' => FALSE,
516 );
517 $form['cj3']['cj3_submit'] = array(
518 '#type' => 'submit',
519 '#value' => t('Submit'),
520 );
521 return $form;
522 }
523
524 /* hook_submit for admin form */
525
526 /* WORK FOR ADD LINK SHARE FEED TO DATABASE */
527 function affiliate_feeds_linkshare_form_submit($form_id, $form_values) {
528 ini_set("max_execution_time",120);
529 global $user;
530 $client = new soapclient($form_values['linkshare_wsdl_feed'],true);
531 $err = $client->getError();
532 if ($err) {
533 form_set_error('affiliate_products_shop', t('<h2>Link Share Constructor error:</h2><pre>%errors</pre>', array('%errors' => theme('placeholder', $err))));
534 drupal_goto('admin/affiliate/affiliate_products_shop');
535 }
536 $client->setCredentials($form_values['linkshare_username'],$form_values['linkshare_password']);
537 $result = $client->call("GetAllLinks", array("sid" => $form_values['linkshare_sid']));
538
539 if($result && is_array($result))
540 {
541 if(array_key_exists('faultcode', $result))
542 {
543 form_set_error('affiliate_products_shop', t('<h2>Link Share error, Fault Data Found-:</h2><pre>%errors</pre>', array('%errors' => theme('placeholder', $result['faultstring']))));
544 drupal_goto('admin/affiliate/affiliate_products_shop');
545 }
546
547 $km = 1;
548 foreach($result as $link)
549 {
550 // change array to object
551 $data = (object)$link;
552
553 $data->feed_type = 'LinkShare';
554 $data->mid = trim($data->mid);
555 $data->linkName = ($data->linkName == 'Text') ? $data->displayText : $data->linkName;
556 $data->displayText = ($data->displayText) ? $data->displayText : $data->linkName;
557 $data->edmode = 1;
558
559 // find current merchant is already in our DB or not.
560 $db_merchant = db_query("SELECT merchant_id FROM {affiliate_manager_merchants} WHERE merchant_id = '%s'",$data->mid);
561 if(db_num_rows($db_merchant) < 1)
562 {
563 // INSERT FEED MERCHANT VALUES INTO MERCHANT TABLE
564 $merchant_table_types = _get_content_merchant_types();
565 $merchant_table_values = _get_content_merchant_values($data);
566 $merchant_query = 'INSERT INTO {affiliate_manager_merchants} ('. implode(', ', array_keys($merchant_table_types)) .') VALUES ('. implode(', ', $merchant_table_types) .')';
567 db_query($merchant_query, $merchant_table_values);
568
569 // CREATE ALIAS FOR MERCHANT FORCE FULLY
570 if(module_exists('path'))
571 {
572 path_set_alias('merchants/'.$data->mid,'merchants/'.$data->mname);
573 }
574 }
575
576 $db_coupon = db_query("SELECT * FROM {affiliate_manager_coupon} WHERE coupon_link_id = '%s' AND coupon_merchant_id = '%s'",$data->linkID, $data->mid);
577 if(db_num_rows($db_coupon) < 1) {
578 // Insert the coupon node into the database:
579 $coupon_table_types = _get_content_coupon_types();
580 $coupon_table_values = _get_content_coupon_values($data);
581 $coupon_query = 'INSERT INTO {affiliate_manager_coupon} ('. implode(', ', array_keys($coupon_table_types)) .') VALUES ('. implode(', ', $coupon_table_types) .')';
582 db_query($coupon_query, $coupon_table_values);
583 } else if(db_num_rows($db_coupon) > 0) {
584 $row_coupon = db_fetch_object($db_coupon);
585 if($row_coupon->coupon_status == 1)
586 {
587 $coupon_table_types = _get_content_coupon_types();
588 $coupon_table_values = _get_content_coupon_values($data);
589 $arr = array();
590 foreach ($coupon_table_types as $key => $value) {
591 $arr[] = $key .' = '. $value;
592 }
593 $coupon_table_values['coupon_status'] = 1;
594 $coupon_table_values[] = $row_coupon->coupon_link_id;
595 $coupon_query = "UPDATE {affiliate_manager_coupon} SET ". implode(', ', $arr) ." WHERE coupon_link_id = '%s'";
596 db_query($coupon_query, $coupon_table_values);
597 }
598 }
599 /*
600 if($km > 25)
601 {
602 break;
603 } else {
604 $km++;
605 }
606 */
607 }
608 drupal_set_message('Link Share Feeds Successfully Added/Update.');
609 } else {
610 drupal_set_message('Feeds Not Found From LinkShare.');
611 }
612 drupal_goto('admin/affiliate/affiliate_products_shop');
613 }
614
615 /* WORK FOR ADD COMMISSION JUNCTION FEED TO DATABASE */
616 function affiliate_feeds_cj3_form_submit($form_id, $form_values) {
617 global $user;
618 ini_set("max_execution_time",120);
619 $ini = ini_set("soap.wsdl_cache_enabled","0");
620 $client = new soapclient($form_values['cj3_wsdl_feed'], "wsdl");
621 $err = $client->getError();
622 if ($err) {
623 form_set_error('affiliate_products_shop', t('<h2>Constructor error:</h2><pre>%errors</pre>', array('%errors' => theme('placeholder', $err))));
624 drupal_goto('admin/affiliate/affiliate_products_shop');
625 }
626
627 $proxy = $client->getProxy();
628 $max_rs = ($form_values['cj3_maxresults'] > 100) ? 100 : $form_values['cj3_maxresults'];
629 $results = $proxy->search(
630 $developerKey = $form_values['cj3_developerkey'],
631 $token = $form_values['cj3_token'],
632 $websiteId = $form_values['cj3_websiteid'],
633 $advertiserIds = $form_values['cj3_advertisercids'],
634 $keywords = $form_values['cj3_keywords'],
635 $category = $form_values['cj3_category'],
636 $linkType = $form_values['cj3_linktype'],
637 $linkSize = $form_values['cj3_linksize'],
638 $language = $form_values['cj3_language'],
639 $serviceableArea = $form_values['cj3_serviceablearea'],
640 $sortBy = $form_values['cj3_sortby'],
641 $sortOrder = $form_values['cj3_sortorder'],
642 $startAt = $form_values['cj3_startat'],
643 $maxResults = $max_rs);
644 if($results && is_array($results))
645 {
646 if(array_key_exists('faultcode', $results))
647 {
648 form_set_error('affiliate_products_shop', t('<h2>Commission Junction error, Fault Data Found-:</h2><pre>%errors</pre>', array('%errors' => theme('placeholder', $results['faultstring']))));
649 drupal_goto('admin/affiliate/affiliate_products_shop');
650 }
651
652
653 // working on add values to DB
654 foreach($results['links'] as $key => $value)
655 {
656 $data = array();
657 $data = (object)$data;
658 // The changed timestamp is always updated for bookkeeping purposes (revisions, searching, ...)
659 $data->edmode = 1;
660 $data->mid = $value['cid'];
661 $data->linkID = $value['linkId'];
662 $data->feed_type = 'CJ3';
663 // find current merchant is already in our DB or not.
664 $db_merchant = db_query("SELECT merchant_id FROM {affiliate_manager_merchants} WHERE merchant_id = '%s'",$data->mid);
665 if(db_num_rows($db_merchant) < 1)
666 {
667 // some related fields to this content
668 $data->mname = $value['name'];
669 $data->home_page_value = '';
670 $data->desc = '';
671 $data->logo_alt = '';
672
673 // INSERT FEED MERCHANT VALUES INTO MERCHANT TABLE
674 $merchant_table_types = _get_content_merchant_types();
675 $merchant_table_values = _get_content_merchant_values($data);
676 $merchant_query = 'INSERT INTO {affiliate_manager_merchants} ('. implode(', ', array_keys($merchant_table_types)) .') VALUES ('. implode(', ', $merchant_table_types) .')';
677 db_query($merchant_query, $merchant_table_values);
678
679 // CREATE ALIAS FOR MERCHANT FORCE FULLY
680 if(module_exists('path'))
681 {
682 path_set_alias('merchants/'.$data->mid,'merchants/'.$data->mname);
683 }
684 }
685
686 $db_coupon = db_query("SELECT * FROM {affiliate_manager_coupon} WHERE coupon_link_id = '%s' AND coupon_merchant_id = '%s'",$data->linkID, $data->mid);
687 if(db_num_rows($db_coupon) < 1) {
688 // some related fields to this content
689 $data->edmode = 1;
690 $data->linkName = $value['linkName'];
691 $data->category = '';
692 $data->displayText = _get_linkhtml($value['linkCodeHTML'],'link_text');
693 $data->linkUrl = _get_linkhtml($value['linkCodeHTML'],'link_url');
694 $data->trackUrl = _get_linkhtml($value['linkCodeHTML'],'track_url');
695
696 // Insert the coupon node into the database:
697 $coupon_table_types = _get_content_coupon_types();
698 $coupon_table_values = _get_content_coupon_values($data);
699 $coupon_query = 'INSERT INTO {affiliate_manager_coupon} ('. implode(', ', array_keys($coupon_table_types)) .') VALUES ('. implode(', ', $coupon_table_types) .')';
700 db_query($coupon_query, $coupon_table_values);
701 } else if(db_num_rows($db_coupon) > 0) {
702 $row_coupon = db_fetch_object($db_coupon);
703 if($row_coupon->coupon_status == 1)
704 {
705 // some related fields to this content
706 $data->linkName = $value['linkName'];
707 $data->displayText = _get_linkhtml($value['linkCodeHTML'],'link_text');
708 $data->linkUrl = _get_linkhtml($value['linkCodeHTML'],'link_url');
709 // $data->start_date = $value['promotionStartDate'];
710 // $data->end_date = $value['promotionEndDate'];
711
712 $data->trackUrl = _get_linkhtml($value['linkCodeHTML'],'track_url');
713
714 $coupon_table_types = _get_content_coupon_types();
715 $coupon_table_values = _get_content_coupon_values($data);
716 $arr = array();
717 foreach ($coupon_table_types as $key => $value) {
718 $arr[] = $key .' = '. $value;
719 }
720 $coupon_table_values['coupon_status'] = 1;
721 $coupon_table_values[] = $row_coupon->coupon_link_id;
722 $coupon_query = "UPDATE {affiliate_manager_coupon} SET ". implode(', ', $arr) ." WHERE coupon_link_id = '%s'";
723 db_query($coupon_query, $coupon_table_values);
724 }
725 }
726 }
727 drupal_set_message('Commission Junction Feeds Successfully Added/Update.');
728 } else {
729 drupal_set_message('Feeds Not Found From Commission Junction.');
730 }
731 drupal_goto('admin/affiliate/affiliate_products_shop');
732 }
733
734 /* Feed view by admin for edit and delete */
735 function affiliate_feeds_view($view) {
736 global $base_url;
737 $tl = ($view == 1) ? 'Active ' : 'Deactive';
738 $post_bk = ($view == 1) ? 'view' : 'view_deactive';
739 $post_back = '?page='.$_REQUEST['page'];
740 $post_back = $post_bk.$post_back;
741 drupal_set_title(t('All '.$tl.' Feed(s)'));
742
743 $header[] = array('data' => t('Sr.No.'));
744 $header[] = array('data' => t('Display Text'));
745 $header[] = array('data' => t('Link Id'));
746 $header[] = array('data' => t('Coupon Type'));
747 $header[] = array('data' => t('Operations'), 'colspan' => 2);
748
749 $count_query = "SELECT COUNT(amc.coupon_link_id) FROM {affiliate_manager_merchants} m LEFT JOIN {affiliate_manager_coupon} amc ON m.merchant_id = amc.coupon_merchant_id WHERE amc.coupon_status = ".$view." ORDER BY merchant_name";
750 $numset = db_result(db_query($count_query,$view));
751
752 $pager_limit = 30;
753
754 $rs_result = pager_query("SELECT m.mer_id, m.merchant_name, m.merchant_id, amc.* FROM {affiliate_manager_merchants} m LEFT JOIN {affiliate_manager_coupon} amc ON m.merchant_id = amc.coupon_merchant_id WHERE amc.coupon_status = ".$view." ORDER BY merchant_name", $pager_limit, 0, $count_query);
755
756 if(db_num_rows($rs_result) > 0)
757 {
758 $last_merchant_id = '';
759 $rows = array();
760 $fee_sr_no = 1;
761 while($row_merchant = db_fetch_object($rs_result))
762 {
763 if($row_merchant->merchant_id != $last_merchant_id){
764 $fee_sr_no = 1;
765 $rows[] = array(array('data' => l($row_merchant->merchant_name,'merchants/'.$row_merchant->merchant_id), 'colspan' => count($header) - 1), l(t('edit'), 'admin/affiliate/affiliate_products_merchant_form/'. $row_merchant->mer_id.'/edit',array(),'destination=admin/affiliate/affiliate_products_shop/'.$post_back), l(t('delete'), 'admin/affiliate/affiliate_products_merchant_delete/'. $row_merchant->mer_id,array(),'destination=admin/affiliate/affiliate_products_shop/'.$post_bk));
766 }
767
768 $rows[] = array($fee_sr_no.'.',$row_merchant->coupon_link_name,$row_merchant->coupon_link_id,$row_merchant->coupon_type,l(t('edit'), 'admin/affiliate/affiliate_products_coupon_form/'.$row_merchant->coupon_id.'/edit',array(),'destination=admin/affiliate/affiliate_products_shop/'.$post_back), l(t('delete'), 'admin/affiliate/affiliate_products_coupon_delete/'.$row_merchant->coupon_id,array(), 'destination=admin/affiliate/affiliate_products_shop/'.$post_bk));
769 $fee_sr_no++;
770 $last_merchant_id = $row_merchant->merchant_id;
771 }
772 }
773 if ($pager = theme('pager', NULL, $pager_limit, 0))
774 {
775 $rows[] = array(array("data" => $pager, "colspan" => count($header)));
776 }
777 if ($numset) {
778 $output = theme('table', $header, $rows);
779 } else {
780 $output = t('<h2>Result Not Found</h2>');
781 }
782 return $output;
783 }
784
785 function _load_merchant($mid) {
786 $merchant = array();
787 $rs_merchant = db_query("SELECT * FROM {affiliate_manager_merchants} WHERE mer_id = %d",$mid);
788 if(db_num_rows($rs_merchant) > 0) {
789 while($row_merchant = db_fetch_array($rs_merchant))
790 {
791 $merchant[] = $row_merchant;
792 }
793 }
794 return $merchant;
795 }
796
797 function _load_coupon($cid) {
798 $coupon = array();
799 $rs_coupon = db_query("SELECT * FROM {affiliate_manager_coupon} WHERE coupon_id = %d",$cid);
800 if(db_num_rows($rs_coupon) > 0) {
801 while($row_coupon = db_fetch_array($rs_coupon))
802 {
803 $coupon[] = $row_coupon;
804 }
805 }
806 return $coupon;
807 }
808
809 function merchant_delete(){
810
811 $edit = arg(3);
812 $editdata = _load_merchant($edit);
813 $editdata = (object)$editdata[0];
814 $form['mer_id'] = array(
815 "#type" => 'hidden',
816 "#value" => $editdata->mer_id,
817 );
818 $form['mname'] = array(
819 "#type" => 'hidden',
820 "#value" => $editdata->merchant_name,
821 );
822 $form['merchant_logo'] = array(
823 "#type" => 'hidden',
824 "#value" => $editdata->merchant_logo,
825 );
826 $form['merchant_id'] = array(
827 "#type" => 'hidden',
828 "#value" => $editdata->merchant_id,
829 );
830 $form['destination'] = array('#type' => 'value', '#value' => $_REQUEST['destination']);
831 return confirm_form($form,
832 t('Are you sure you want to delete %title merchant?', array('%title' => $editdata->merchant_name)),
833 $_REQUEST['destination'],'All the coupons related to this merchant will also delete.',
834 t('Delete'), t('Cancel'));
835 }
836
837 function merchant_delete_submit($form_id, $form_values) {
838 db_query("DELETE FROM {affiliate_manager_coupon} WHERE coupon_merchant_id = '%s'",$form_values['merchant_id']);
839 $coupon_delete = db_affected_rows();
840 file_delete($form_values['merchant_logo']);
841 db_query("DELETE FROM {affiliate_manager_merchants} WHERE mer_id = %d",$form_values['mer_id']);
842 drupal_set_message('Merchant <b><i>'.$form_values['mname'].'</i></b> successfully deleted.');
843 drupal_set_message($coupon_delete.' Coupon(s) successfully deleted.');
844 drupal_goto($form['destination']);
845 }
846
847 function coupon_delete(){
848
849 $edit = arg(3);
850 $editdata = _load_coupon($edit);
851 $editdata = (object)$editdata[0];
852 $form['coupon_id'] = array(
853 "#type" => 'hidden',
854 "#value" => $editdata->coupon_id,
855 );
856 $form['coupon_link_name'] = array(
857 "#type" => 'hidden',
858 "#value" => $editdata->coupon_link_name,
859 );
860 $form['destination'] = array('#type' => 'value', '#value' => $_REQUEST['destination']);
861 return confirm_form($form,
862 t('Are you sure you want to delete %title coupon?', array('%title' => $editdata->coupon_link_name)),
863 $_REQUEST['destination'],'',
864 t('Delete'), t('Cancel'));
865 }
866
867 function coupon_delete_submit($form_id, $form_values) {
868 db_query("DELETE FROM {affiliate_manager_coupon} WHERE coupon_id = %d",$form_values['coupon_id']);
869 drupal_set_message("Coupon ".$form_values['coupon_link_name']." successfully deleted.");
870 drupal_goto($form['destination']);
871 }
872
873 /* Form for merchant to add functionality */
874 function affiliate_merchant_admin_form() {
875
876 $form = array();
877 $edit = NULL;
878 $logo_lable = 'Upload Merchant Logo';
879 if(arg(3) && arg(4) == 'edit' && $_REQUEST['destination']) {
880 $merid = arg(3);
881 $edit = _load_merchant($merid);
882 $edit = (object)$edit[0];
883 }
884 if(isset($edit)) {
885 drupal_set_title('Edit Merchant');
886 $logo_lable = 'Change Merchant Logo';
887 $form['mer_id'] = array('#type' => 'value', '#value' => $edit->mer_id);
888 $form['destination'] = array('#type' => 'value', '#value' => $_REQUEST['destination']);
889 if(file_exists($edit->merchant_logo)) {
890 $form['logo_image'] = array('#type' => 'markup', '#value' => '<img src="'.base_path().$edit->merchant_logo.'" border="0" weight="100" height="100">', '#weight' => 5);
891 }
892 }
893 $form['mname'] = array(
894 '#type' => 'textfield',
895 '#title' => t('Merchant Name'),
896 '#size' => 40,
897 '#maxlength' => 64,
898 '#default_value' => $edit->merchant_name,
899 '#required' => true,
900 '#weight' => 1,
901 );
902 if(isset($edit)) {
903 $form['mid'] = array(
904 '#type' => 'textfield',
905 '#title' => t('Merchant ID'),
906 '#size' => 40,
907 '#maxlength' => 64,
908 '#default_value' => $edit->merchant_id,
909 '#attributes' => array('readonly' => 'Yes'),
910 '#required' => true,
911 '#weight' => 2,
912 );
913 } else {
914 $form['mid'] = array(
915 '#type' => 'textfield',
916 '#title' => t('Merchant ID'),
917 '#size' => 40,
918 '#maxlength' => 64,
919 '#default_value' => $edit->merchant_id,
920 '#required' => true,
921 '#weight' => 2,
922 );
923 }
924 $form['home_page_link'] = array(
925 '#type' => 'textfield',
926 '#title' => t('Merchant home page link'),
927 '#description' => t("Merchant home page URL. (i.e: http://www.merchanthomepage.com)"),
928 '#size' => 40,
929 '#maxlength' => 255,
930 '#default_value' => $edit->merchant_home_page_link,
931 '#required' => false,
932 '#weight' => 3,
933 );
934 $form['desc'] = array(
935 '#type' => 'textarea',
936 '#title' => t('Merchant Description'),
937 '#description' => t("Describe something related to this merchant."),
938 '#cols' => 30,
939 '#rows' => 6,
940 '#default_value' => $edit->merchant_description,
941 '#required' => true,
942 '#weight' => 4,
943 );
944 $form['logo'] = array(
945 '#type' => 'file',
946 '#title' => t($logo_lable),
947 '#description' => t("You can upload merchant logo to show on merchant page."),
948 '#weight' => 6,
949 );
950 $form['alt'] = array(
951 '#type' => 'textfield',
952 '#title' => t('Title for logo'),
953 '#description' => t("logo title/alt."),
954 '#size' => 20,
955 '#maxlength' => 30,
956 '#default_value' => $edit->merchant_logo_alt,
957 '#required' => FALSE,
958 '#weight' => 7,
959 );
960 $form['edmode'] = array(
961 '#type' => 'radios',
962 '#title' => t('Merchant Status'),
963 '#default_value' => $edit->merchant_status,
964 '#options' => array('1' => 'Show', '2' => 'Hide'),
965 '#required' => true,
966 '#weight' => 8,
967 );
968 $form['#attributes']['enctype'] = 'multipart/form-data';
969 $form['submit'] = array('#type' => 'submit','#value' => t('Submit'), '#weight' => 10);
970 return $form;
971 }
972
973 /* validation of the merchant form */
974 function affiliate_merchant_admin_form_validate($form_id, $form_values) {
975 // checking if this merchant id already exist into the database
976 $merchantid = trim($form_values['mid']);
977 if(!isset($form_values['mer_id'])) {
978 $rs_already = db_query("SELECT * FROM {affiliate_manager_merchants} WHERE LOWER(merchant_id) = LOWER('%s')",$merchantid);
979 } else {
980 $rs_already = db_query("SELECT * FROM {affiliate_manager_merchants} WHERE LOWER(merchant_id) = LOWER('%s') AND mer_id != %d",$merchantid,$form_values['mer_id']);
981 }
982 if(db_num_rows($rs_already) > 0) {
983 form_set_error('mid', t('Merchant with the same ID already exists.'));
984 }
985 if($_FILES['files']['name']['logo']){
986 // checking on file extention
987 $f_name = $_FILES['files']['name']['logo'];
988 $f_type = $_FILES['files']['type']['logo'];
989 $f_size = $_FILES['files']['size']['logo'];
990 $f_filename = basename($f_name);
991 $f_dotpos = strpos($f_filename,'.');
992 $f_extension = substr($f_filename,$f_dotpos+1);
993
994 $f_allowed = array("jpg","jpeg","gif","bmp","png");
995
996 $form_values['merchant_logo'] = $f_name;
997
998 if(!in_array($f_extension,$f_allowed))
999 {
1000 form_set_error('logo', t('File not a valid format file, only possible to upload files with the following extensions: %files-allowed.', array('%files-allowed' => implode(', ',$f_allowed))));
1001 }
1002
1003 if (!file_save_upload('logo', variable_get('merchant_logo_path', 'merchant_logo') .'/logo-'. $form_values['mid'] .'.'. $f_extension, 1)) {
1004 form_set_error('logo', t("Failed to upload the logo image; the %directory directory doesn't exist or is not writable.", array('%directory' => variable_get('merchant_logo_path', 'merchant_logo'))));
1005 }
1006 }
1007 }
1008 /* finally submit form after validation */
1009 function affiliate_merchant_admin_form_submit($form_id, $form_values) {
1010 $data = array();
1011 if(!isset($form_values['mer_id'])) {
1012 // some related fields to this content
1013 $data['mid'] = $form_values['mid'];
1014 $data['mname'] = $form_values['mname'];
1015 $data['home_page_link'] = $form_values['home_page_link'];
1016 $data['desc'] = $form_values['desc'];
1017 $data['edmode'] = $form_values['edmode'];
1018 if($_FILES['files']['name']['logo']){
1019 $f_name = $_FILES['files']['name']['logo'];
1020 $f_filename = basename($f_name);
1021 $f_dotpos = strpos($f_filename,'.');
1022 $f_extension = substr($f_filename,$f_dotpos+1);
1023
1024 // checking on file extention
1025 $data['logo'] = 'files/'.variable_get('merchant_logo_path', 'merchant_logo') .'/logo-'. $form_values['mid'] .'.'. $f_extension;
1026 $data['alt'] = $form_values['alt'];