Advertisement
Guest User

Popshops Sample API script

a guest
Oct 4th, 2011
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 31.13 KB | None | 0 0
  1. <?php
  2. defined('POPSHOPS_API_VERSION') or die('');
  3.  
  4. class PopShops {
  5.  
  6. function PopShops($options=array()) {
  7. // Setup required api keys
  8. $this->apiKey = $options['api_key'];
  9. $this->catalogKey = $options['catalog_key'];
  10. $this->apiVersion = POPSHOPS_API_VERSION;
  11.  
  12. // Setup list of categories to build predefined searches to be used as categories
  13. $this->categories = (isset( $options['categories'] ) ) ? $options['categories'] : array();
  14.  
  15. // Setup default parameters for how many results to return
  16. $this->subid = (isset( $options['subid'] ) ) ? $options['subid'] : 'popshops';
  17. $this->localRedirects = (isset( $options['localRedirects'] ) ) ? $options['localRedirects'] : false;
  18. $this->productLimit = (isset( $options['productLimit'] ) ) ? $options['productLimit'] : 9;
  19. $this->productGridColumns = (isset( $options['productGridColumns'] ) ) ? $options['productGridColumns'] : 3;
  20. $this->productOffset = 0;
  21. $this->dealLimit = (isset( $options['dealLimit'] ) ) ? $options['dealLimit'] : 25;
  22. $this->dealOffset = 0;
  23. $this->nameSpace = (isset( $options['nameSpace'] ) ) ? $options['nameSpace'] : 'psps_';
  24.  
  25. if ( !isset( $_REQUEST[ $this->nameSpace.'keywords' ] ) && isset( $options['defaultSearch'] ) ) {
  26. $_REQUEST[ $this->nameSpace.'keywords' ] = $options['defaultSearch'];
  27. }
  28.  
  29. // Setup list of parameters to pass on to PopShops. These will be parsed out of the url.
  30. $this->params = array('keywords','merchant_id','merchant_type_id','category_id','brand_id','price_min','price_max','product_sort','product_limit','product_offset','product_group_id','deal_type_id','deal_offset');
  31. }
  32.  
  33. function findMerchants() {
  34. $request = "http://www.popshops.com/v".$this->apiVersion."/".$this->apiKey."/merchants.xml?";
  35. $request .= "catalog_key=".$this->catalogKey;
  36. $this->merchantResults = $this->requestResults($request);
  37. }
  38.  
  39. function findDeals() {
  40. $request = "http://www.popshops.com/v".$this->apiVersion."/".$this->apiKey."/deals.xml?";
  41. $request .= "catalog_key=".$this->catalogKey;
  42. $request = $this->addParameter($request, 'deal_limit', $this->dealLimit);
  43. $request = $this->addParameters($request,$this->params);
  44. $this->dealResults = $this->requestResults($request);
  45. }
  46.  
  47. function findDealTypes() {
  48. $request = "http://www.popshops.com/v".$this->apiVersion."/".$this->apiKey."/deal_types.xml";
  49. $this->dealTypeResults = $this->requestResults($request);
  50. }
  51.  
  52. function findMerchantTypes() {
  53. $request = "http://www.popshops.com/v".$this->apiVersion."/".$this->apiKey."/merchant_types.xml";
  54. $this->merchantTypeResults = $this->requestResults($request);
  55. }
  56.  
  57. function findNetworks() {
  58. $request = "http://www.popshops.com/v".$this->apiVersion."/".$this->apiKey."/networks.xml";
  59. $this->networkResults = $this->requestResults($request);
  60. }
  61.  
  62. function findProducts(){
  63. $request = "http://www.popshops.com/v".$this->apiVersion."/".$this->apiKey."/products.xml?";
  64. $request .= "catalog_key=".$this->catalogKey;
  65. $request = $this->addParameter($request, 'product_limit', $this->productLimit);
  66. $request = $this->addParameters($request,$this->params);
  67. $this->productResults = $this->requestResults($request);
  68. }
  69.  
  70. function productStoreName($product){
  71. $out = '';
  72. $storeName = $this->merchantNameFor($product);
  73.  
  74. if (!empty($storeName)) {
  75. $out .= '<p class="psps-text psps-store-name"><span>Store: </span>';
  76. $out .= '<a href="'.$this->localRedirectURL($product['url']) .'" rel="nofollow">'.$storeName.'</a>';
  77. $out .= '</p>';
  78. }
  79. return $out;
  80. }
  81.  
  82. function productPrice($product){
  83. $out = '';
  84. $out .= '<p class="psps-text psps-price">';
  85.  
  86. if (intval($product['merchant_price']) > 0 && (floatval($product['merchant_price']) != floatval($product['retail_price']))) {
  87. $out .= '<span class="psps-retail-price"><del>$'.number_format(floatval($product['retail_price']),2).'</del></span>';
  88. $out .= '<span class="psps-store-price">$'.number_format(floatval($product['merchant_price']),2).'</span>';
  89. } else {
  90. $out .= '<span class="psps-store-price">$'.number_format(floatval($product['merchant_price']),2).'</span>';
  91. }
  92.  
  93. $out .= '</p>';
  94. return $out;
  95. }
  96.  
  97. function renderProduct($product) {
  98. $out = '';
  99. $out .= '<div class="psps-img"><a href="'.$this->localRedirectURL($product['url']).'" rel="nofollow"><img src="'.$product['large_image_url'].'" /></a></div>';
  100. $out .= $this->productPrice($product);
  101. $out .= '<p class="psps-text psps-name"><a href="'.$this->localRedirectURL($product['url']).'" rel="nofollow">'.$product['name'].'</a></p>';
  102. $out .= '<p class="psps-text psps-description">'.substr($product['description'],0,150) .'...</p>';
  103. $out .= $this->productStoreName($product);
  104. return $out;
  105. }
  106.  
  107. function renderProductGrid() {
  108. $out = '<table style="float:left;">';
  109.  
  110. $count = 0;
  111. $rowCount = 1;
  112. $cols = $this->productGridColumns;
  113. $size = sizeof($this->productResults->products->product);
  114.  
  115. foreach($this->productResults->products->product as $product) {
  116. if ($count%$cols == 0) { $rowCount++; }
  117.  
  118. if (($count == 0) || ($count%$cols == 0)) { $out .= '<tr>'; }
  119.  
  120. $out .= '<td class="psps-cell">';
  121. $out .= $this->renderProduct($product);
  122. $out .= '</td>';
  123.  
  124. // This will add any additional cells in the last row that might be missing.
  125. if ( $size == $count+1 && $size%$cols != 0 ) {
  126. $i = 0;
  127. while ( $i < ( $cols - $size%$cols) ) {
  128. $out .= '<td> </td>';
  129. $i++;
  130. }
  131. }
  132.  
  133. $count++;
  134. if (($count%$cols == 0) || ($count == $size)) {
  135. $out .= '</tr>';
  136. }
  137. }
  138.  
  139. $out .= '</table>';
  140. return $out;
  141. }
  142.  
  143. function renderCustomCategories(){
  144. $out = '';
  145. if (sizeOf($this->categories) > 0) {
  146. $out .= '<h3>Popular categories</h3>';
  147. $out .= '<ul>';
  148. foreach ($this->categories as $category) {
  149. $out .= '<li>'.$this->customCategoryLink($category).'</li>';
  150. }
  151. $out .= '</ul>';
  152. }
  153. return $out;
  154. }
  155.  
  156. function merchantNameFor($object) {
  157. if (isset($this->merchantResults)) {
  158. $merchants = $this->merchantResults;
  159. } else {
  160. $merchants = isset($this->productResults) ? $this->productResults->merchants : $this->dealResults->merchants;
  161. }
  162.  
  163. foreach($merchants->merchant as $merchant) {
  164. if (intval($merchant['id']) == intval($object['merchant_id'])) {
  165. return $merchant['name'];
  166. }
  167. }
  168. return '';
  169. }
  170.  
  171. function merchantLogoFor($object) {
  172. if (isset($this->merchantResults)) {
  173. $merchants = $this->merchantResults;
  174. } else {
  175. $merchants = isset($this->productResults) ? $this->productResults->merchants : $this->dealResults->merchants;
  176. }
  177.  
  178. foreach($merchants->merchant as $merchant) {
  179. if (intval($merchant['id']) == intval($object['merchant_id'])) {
  180. return $merchant['logo_url'];
  181. }
  182. }
  183. return '';
  184. }
  185.  
  186. function localRedirectURL($destination) {
  187. if ( $this->localRedirects == true ) {
  188. $url = $this->baseURL($this->params);
  189. $url = str_replace('?&','?',$url);
  190. $delimiter = (strpos($url,'?') > -1) ? '&' : '?';
  191. return $url.$delimiter.$this->nameSpace.'destination='.str_replace('http://','',$destination);
  192. } else {
  193. return $destination;
  194. }
  195. }
  196.  
  197. function renderDealRows(){
  198. $out = '';
  199. if (sizeOf($this->dealResults->deals->deal) > 0) {
  200. $out .= '<ul>';
  201. foreach($this->dealResults->deals->deal as $deal) {
  202. $out .= '<li><div class="psps-deal">';
  203. $out .= '<div class="psps-deal-logo"><a href="'.$this->localRedirectURL($deal['url']).'" rel="nofollow"><img src="'.$this->merchantLogoFor($deal).'" alt="'.$this->merchantNameFor($deal).'"/></a></div>';
  204. $out .= '<div class="psps-deal-name">'.$deal['name'].'</div>';
  205. $out .= '<div class="psps-deal-meta">';
  206. $out .= '<span class="psps-deal-expiration">Expires: '.$deal['end_on'].'</span>';
  207. if (isset($deal['code'])) {
  208. $out .= '<span class="psps-deal-code">Code: '.$deal['code'].'</span>';
  209. }
  210.  
  211. $out .= '</div>';
  212. $out .= '</div></li>';
  213. }
  214. $out .= '</ul>';
  215. }
  216. return $out;
  217. }
  218.  
  219. function renderDeals(){
  220. $out = '';
  221. if (sizeOf($this->productResults->deals->deal) > 0) {
  222. $out .= '<h3>Deals</h3>';
  223. $out .= '<div class="psps-filter-options psps-tall">';
  224. $out .= '<ul>';
  225. foreach($this->productResults->deals->deal as $deal) {
  226. $out .= '<li><div class="psps-deal">';
  227. $out .= '<div class="psps-merchant"><a href="'.$this->localRedirectURL($deal['url']).'" rel="nofollow">'.$this->merchantNameFor($deal).'</a></div>';
  228. $out .= '<div class="psps-name">'.$deal['name'].'</div>';
  229. $out .= '</div></li>';
  230. }
  231. $out .= '</ul>';
  232. $out .= '</div>';
  233. }
  234. return $out;
  235. }
  236.  
  237. function renderDealMerchantTypeFilter(){
  238. return $this->renderMerchantTypeFilter($this->dealResults);
  239. }
  240.  
  241. function renderDealMerchantTypeSelect(){
  242. return $this->renderMerchantTypeSelect($this->dealResults);
  243. }
  244.  
  245. function renderDealMerchantFilter(){
  246. return $this->renderMerchantFilter($this->dealResults);
  247. }
  248.  
  249. function renderDealMerchantSelect(){
  250. return $this->renderMerchantSelect($this->dealResults);
  251. }
  252.  
  253. function renderDealTypeFilter(){
  254. $out = '';
  255. if (sizeOf($this->dealResults->deal_types) > 0) {
  256. $out .= '<h3>Types of deals</h3>';
  257. $out .= '<div class="psps-filter-options">';
  258. $out .= '<ul>';
  259. foreach($this->dealResults->deal_types->deal_type as $dealType) {
  260. $out .= '<li>';
  261. $out .= '<span class="psps-label">'.$this->dealTypeLink($dealType).'</span>';
  262. $out .= ' <span class="psps-count">('.number_format(floatval($dealType['deal_count'])).')</span>';
  263. $out .= '</li>';
  264. }
  265. $out .= '</ul>';
  266. $out .= '</div>';
  267. }
  268. return $out;
  269. }
  270.  
  271. function renderDealTypeSelect(){
  272. $out = '';
  273. if (sizeOf($this->dealResults->deal_types) > 0) {
  274. $out .= '<h3>Types of deals</h3>';
  275. $out .= '<select class="psps-select" onchange="window.location=this.value;">';
  276. $out .= '<option value="'.$this->baseURL(array('deal_type_id')).'">All</option>';
  277. foreach($this->dealResults->deal_types->deal_type as $dealType) {
  278. $param = isset($_REQUEST[$this->nameSpace.'deal_type_id']) ? $_REQUEST[$this->nameSpace.'deal_type_id'] : 0;
  279. $selected = (intval($dealType['id']) == intval($param)) ? ' selected="selected"' : '';
  280. $out .= '<option value="'.$this->dealTypeUrl($dealType).'"'.$selected.'>';
  281. $out .= $dealType['name'].' ('.number_format(floatval($dealType['deal_count'])).')';
  282. $out .= '</option>';
  283. }
  284. $out .= '</select>';
  285. }
  286. return $out;
  287. }
  288.  
  289. function dealTypeLink($dealType) {
  290. return '<a href="'.$this->dealTypeUrl($dealType).'">'.$dealType['name'].'</a>';
  291. }
  292.  
  293. function dealTypeUrl($dealType) {
  294. $url = $this->baseURL(array('deal_type_id'));
  295. $url = $this->addParameter($url,'deal_type_id',$dealType['id']);
  296.  
  297. if (isset($_REQUEST[$this->nameSpace.'merchant_type_id'])) {
  298. $url = $this->addParameter($url,'merchant_type_id',$_REQUEST[$this->nameSpace.'merchant_type_id']);
  299. }
  300.  
  301. $url = str_replace('?&','?',$url);
  302. return $url;
  303. }
  304.  
  305.  
  306. function renderCategoryFilter(){
  307. if (isset($_REQUEST[$this->nameSpace.'category_id'])) {
  308. $this->renderMerchantCategoryFilter();
  309. } else {
  310. $this->renderMerchantTypeFilter($this->productResults);
  311. }
  312. }
  313.  
  314. function renderMerchantCategoryFilter(){
  315. $out = '';
  316. if (sizeOf($this->productResults->categories->category) > 0) {
  317. $out .= '<h3>Categories</h3>';
  318. $out .= '<div class="psps-filter-options">';
  319. $out .= '<ul>';
  320. foreach($this->productResults->categories->category as $category) {
  321. $out .= '<li>';
  322. $out .= '<span class="psps-label">'.$this->categoryLink($category).'</span>';
  323. $out .= ' <span class="psps-count">('.number_format(floatval($category['product_count'])).')</span>';
  324. $out .= '</li>';
  325. }
  326. $out .= '</ul>';
  327. $out .= '</div>';
  328. }
  329. return $out;
  330. }
  331.  
  332. function renderMerchantTypeFilter($results){
  333. $out = '';
  334. if (sizeOf($results->merchant_types->merchant_type) > 0) {
  335. $out .= '<h3>Categories</h3>';
  336. $out .= '<div class="psps-filter-options">';
  337. $out .= '<ul>';
  338. foreach($results->merchant_types->merchant_type as $merchant_type) {
  339. $out .= '<li>';
  340. $out .= '<span class="psps-label">'.$this->merchantTypeLink($merchant_type).'</span>';
  341. $count = isset($merchant_type['deal_count']) ? $merchant_type['deal_count'] : $merchant_type['product_count'];
  342. $out .= ' <span class="psps-count">('.number_format(intval($count)).')</span>';
  343. $out .= '</li>';
  344. }
  345. $out .= '</ul>';
  346. $out .= '</div>';
  347. }
  348. return $out;
  349. }
  350.  
  351. function renderMerchantTypeSelect($results){
  352. $out = '';
  353. if (sizeOf($results->merchant_types->merchant_type) > 0) {
  354. $out .= '<h3>Categories</h3>';
  355. $out .= '<select class="psps-select" onchange="window.location=this.value;">';
  356. $out .= '<option value="'.$this->baseURL(array('merchant_type_id')).'">All</option>';
  357. foreach($results->merchant_types->merchant_type as $merchant_type) {
  358. $param = isset($_REQUEST[$this->nameSpace.'merchant_type_id']) ? $_REQUEST[$this->nameSpace.'merchant_type_id'] : 0;
  359. $selected = (intval($merchant_type['id']) == intval($param)) ? ' selected="selected"' : '';
  360.  
  361. $out .= '<option value="'.$this->merchantTypeUrl($merchant_type).'"'.$selected.'>';
  362. $out .= $merchant_type['name'];
  363. $count = isset($merchant_type['deal_count']) ? $merchant_type['deal_count'] : $merchant_type['product_count'];
  364. $out .= ' ('.number_format(intval($count)).')';
  365. $out .= '</option>';
  366. }
  367. $out .= '</select>';
  368. }
  369. return $out;
  370. }
  371.  
  372. function renderBrandFilter(){
  373. $out = '';
  374. if (sizeOf($this->productResults->brands->brand) > 0) {
  375. $out .= '<h3>Brands</h3>';
  376. $out .= '<div class="psps-filter-options">';
  377. $out .= '<ul>';
  378. foreach($this->productResults->brands->brand as $brand) {
  379. $out .= '<li>';
  380. $out .= '<span class="psps-label">'.$this->brandLink($brand).'</span>';
  381. $out .= ' <span class="psps-count">('.number_format(intval($brand['product_count'])).')</span>';
  382. $out .= '</li>';
  383. }
  384. $out .= '</ul>';
  385. $out .= '</div>';
  386. }
  387. return $out;
  388. }
  389.  
  390. function renderMerchantFilter($results){
  391. $out = '';
  392. if (sizeOf($results->merchants->merchant) > 0) {
  393. $out .= '<h3>Stores</h3>';
  394. $out .= '<div class="psps-filter-options">';
  395. $out .= '<ul>';
  396. foreach($results->merchants->merchant as $merchant) {
  397. $out .= '<li>';
  398. $out .= '<span class="psps-label">'.$this->merchantLink($merchant).'</span>';
  399. $count = isset($merchant['deal_count']) ? $merchant['deal_count'] : $merchant['product_count'];
  400. $out .= ' <span class="psps-count">('.number_format(intval($count)).')</span>';
  401. $out .= '</li>';
  402. }
  403. $out .= '</ul>';
  404. $out .= '</div>';
  405. }
  406. return $out;
  407. }
  408.  
  409. function renderMerchantSelect($results){
  410. $out = '';
  411. if (sizeOf($results->merchants->merchant) > 0) {
  412. $out .= '<h3>Stores</h3>';
  413. $out .= '<select class="psps-select" onchange="window.location=this.value">';
  414. $out .= '<option value="'.$this->baseURL(array('merchant_id')).'">All</option>';
  415. foreach($results->merchants->merchant as $merchant) {
  416. $param = isset($_REQUEST[$this->nameSpace.'merchant_id']) ? $_REQUEST[$this->nameSpace.'merchant_id'] : 0;
  417. $selected = (intval($merchant['id']) == intval($param)) ? ' selected="selected"' : '';
  418.  
  419. $out .= '<option value="'.$this->merchantUrl($merchant).'"'.$selected.'>';
  420. $out .= $merchant['name'];
  421. $count = isset($merchant['deal_count']) ? $merchant['deal_count'] : $merchant['product_count'];
  422. $out .= ' ('.number_format(intval($count)).')';
  423. $out .= '</option>';
  424. }
  425. $out .= '</select>';
  426. }
  427. return $out;
  428. }
  429.  
  430. function renderPriceRangeFilter() {
  431. $out = '';
  432. if (sizeOf($this->productResults->price_ranges->price_range) > 0) {
  433. $out .= '<h3>Prices</h3>';
  434. $out .= '<div class="psps-filter-options">';
  435. $out .= '<ul>';
  436. foreach($this->productResults->price_ranges->price_range as $price_range) {
  437. $out .= '<li>';
  438. $out .= '<span class="psps-label">'.$this->priceLink(array('price_min' => $price_range['min'], 'price_max' => $price_range['max'])).'</span>';
  439. $out .= ' <span class="psps-count">('.number_format(intval($price_range['product_count'])).')</span>';
  440. $out .= '</li>';
  441. }
  442. $out .= '</ul>';
  443. $out .= '</div>';
  444. }
  445. return $out;
  446. }
  447.  
  448. function renderSuggestedMerchants() {
  449. if (!isset($this->cachedSuggestedMerchants)) {
  450. $out = '';
  451. if (!isset($this->productResults->suggested_merchants)) return $out;
  452. $out .= '<div id="psps-suggested-merchants">';
  453. $out .= '<h2>Go directly to this store:</h2>';
  454. foreach($this->productResults->suggested_merchants->merchant as $suggested_merchant) {
  455. $out .= '<div class="psps-suggested-merchant">';
  456. if (strlen($suggested_merchant['logo_url']) > 0) {
  457. $out .= '<a href="'.$suggested_merchant['url'].'" rel="nofollow"><img src="'.$suggested_merchant['logo_url'].'" /></a>';
  458. }
  459. $out .= '<a href="'.$suggested_merchant['url'].'" rel="nofollow">'.$suggested_merchant['name'].'</a>';
  460. $out .= '</div>';
  461. }
  462. $out .= '</div>';
  463. $this->cachedSuggestedMerchants = $out;
  464. }
  465. return $this->cachedSuggestedMerchants;
  466. }
  467.  
  468. function renderDealPaginationSummary(){
  469. return $this->renderPaginationSummary($this->dealResults['deal_offset'], $this->dealResults->deals['total_count'], $this->dealLimit);
  470. }
  471.  
  472. function renderProductPaginationSummary(){
  473. return $this->renderPaginationSummary($this->productResults['product_offset'], $this->productResults->products['total_count'], $this->productLimit);
  474. }
  475.  
  476. function renderPaginationSummary($offset,$count,$limit){
  477. if (!isset($this->cachedPaginationSummary)) {
  478. $start = intval($offset)+1;
  479. $finish = (intval($count) < $limit) ? $count : (($start+$limit)-1);
  480. $this->cachedPaginationSummary = 'Showing '.$start.'-'.$finish.' of '.number_format(intval($count));
  481. }
  482. return $this->cachedPaginationSummary;
  483. }
  484.  
  485. function renderDealPaginationLinks() {
  486. return $this->renderPaginationLinks('deal', $this->dealResults, $this->dealResults->deals['total_count']);
  487. }
  488.  
  489. function renderProductPaginationLinks(){
  490. return $this->renderPaginationLinks('product', $this->productResults, $this->productResults->products['total_count']);
  491. }
  492.  
  493.  
  494. function renderPaginationLinks($type, $results, $count){
  495. if (!isset($this->cachedPagination_links)) {
  496. $out = '';
  497. $itemCount = intval($count);
  498. $limit = $this->productLimit;
  499.  
  500. if ($itemCount > $limit) {
  501. $offset = intval($results[$type.'_offset']);
  502. $pagesAtOnce = 5;
  503. $pageNumber = ($offset == 0) ? 1 : intval(($offset/$limit)+1);
  504.  
  505. $totalPages = intval($itemCount / $limit);
  506. if (($itemCount%$limit) > 0) $totalPages++;
  507. $pagesAtATime = ($totalPages <= $pagesAtOnce) ? $totalPages : $pagesAtOnce;
  508.  
  509. $out .= '<div class="psps-results-pages">';
  510.  
  511. if ($pageNumber != 1) {
  512. $out .= '<span class="psps-search-next">'.$this->pageLink(array($type.'_offset' => ($limit*($pageNumber-2)), 'page' => $pageNumber-1, 'text' => 'Previous')).'</span>';
  513. }
  514.  
  515. if ($pageNumber <= 2 || $totalPages <= 5) {
  516. $start_page = 1;
  517. $end_page = $pagesAtATime;
  518. } else if (($totalPages - $pageNumber) == 0) {
  519. $start_page = $pageNumber - 4;
  520. $end_page = $pageNumber;
  521. } else if (($totalPages - $pageNumber) == 1) {
  522. $start_page = $pageNumber - 3;
  523. $end_page = $pageNumber + 1;
  524. } else {
  525. $start_page = $pageNumber - 2;
  526. $end_page = $pageNumber + 2;
  527. }
  528.  
  529. $i = $start_page;
  530.  
  531. while ($i < $end_page+1) {
  532. if ($i == $pageNumber) {
  533. $out .= '<span class="psps-search-current">'.$pageNumber.'</span>';
  534. } else {
  535. $out .= $this->pageLink(array($type.'_offset' => ($limit*($i-1)),'page' => $i, 'text' => $i));
  536. }
  537. $i++;
  538. }
  539.  
  540. if ($pageNumber < $totalPages) {
  541. $out .= '<span class="psps-search-next">'.$this->pageLink(array('product_offset' => ($limit*($pageNumber)), 'page' => $pageNumber+1, 'text' => 'Next')).'</span>';
  542. }
  543.  
  544. $out .= '</div>';
  545. }
  546. $this->cachedPaginationLinks = $out;
  547. }
  548. return $this->cachedPaginationLinks;
  549. }
  550.  
  551. function customCategoryLink($category) {
  552. $url = $this->baseURL($this->params);
  553.  
  554. if (isset($category['search_options']) && sizeOf($category['search_options']) > 0) {
  555. foreach($category['search_options'] as $key => $value) {
  556. $url = $this->addParameter($url,$key,$value);
  557. }
  558. } else {
  559. $url = $this->addParameter($url,'keywords',$category['name']);
  560. }
  561. $url = str_replace('?&','?',$url);
  562. return '<a href="'.$url.'">'.$category['name'].'</a>';
  563. }
  564.  
  565. function brandLink($brand) {
  566. return '<a href="'.$this->brandUrl($brand).'" title="See '.$brand['name'].' products">'.$brand['name'].'</a>';
  567. }
  568.  
  569. function brandUrl($brand) {
  570. $url = $this->baseURL(array('merchant_type_id','brand_id','price_max','price_min','product_offset','category_id'));
  571. $url = $this->addParameter($url,'brand_id',$brand['id']);
  572. $url = str_replace('?&','?',$url);
  573. return $url;
  574. }
  575.  
  576. function categoryLink($category) {
  577. return '<a href="'.$this->categoryUrl($category).'" title="See '.$category['name'].' products">'.$category['name'].'</a>';
  578. }
  579.  
  580. function categoryUrl($category) {
  581. $url = $this->baseURL(array('merchant_id','merchant_type_id','brand_id','price_max','price_min','product_offset','category_id'));
  582. $url = $this->addParameter($url,'category_id',$category['id']);
  583. $url = $this->addParameter($url,'merchant_id',$_REQUEST['merchant_id']);
  584. $url = str_replace('?&','?',$url);
  585. return $url;
  586. }
  587.  
  588. function merchantLink($merchant) {
  589. return '<a href="'.$this->merchantUrl($merchant).'" title="See '.$merchant['name'].' products">'.$merchant['name'].'</a>';
  590. }
  591.  
  592. function merchantUrl($merchant){
  593. $url = $this->baseURL(array('merchant_id','merchant_type_id','brand_id','price_max','price_min','product_offset','category_id'));
  594. $url = $this->addParameter($url,'merchant_id',$merchant['id']);
  595. $url = str_replace('?&','?',$url);
  596. return $url;
  597. }
  598.  
  599. function merchantTypeLink($merchant) {
  600. return '<a href="'.$this->merchantTypeUrl($merchant).'" title="See '.$merchant['name'].' products">'.$merchant['name'].'</a>';
  601. }
  602.  
  603. function merchantTypeUrl($merchant){
  604. $url = $this->baseURL(array('merchant_id','merchant_type_id','brand_id','price_max','price_min','product_offset','category_id'));
  605. $url = $this->addParameter($url,'merchant_type_id',$merchant['id']);
  606. if (isset($_REQUEST[$this->nameSpace.'merchant_id'])) {
  607. $url = $this->addParameter($url,'merchant_id',$_REQUEST[$this->nameSpace.'merchant_id']);
  608. }
  609. $url = str_replace('?&','?',$url);
  610. return $url;
  611. }
  612.  
  613. // $options = array('price_min' => '', 'price_max' => '', 'text' => '')
  614. function priceLink($options=array()) {
  615. $label = (intval($options['price_min']) == 0) ? "Under $".$options['price_max'] : '$'.$options['price_min'].'-$'.$options['price_max'];
  616. return '<a href="'.$this->priceUrl($options).'" title="Filter by prices $'.$options['price_min'].'-$'.$options['price_max'].'">'.$label.'</a>';
  617. }
  618.  
  619. function priceUrl($options=array()){
  620. $url = $this->baseURL(array('merchant_type_id','brand_id','price_min','price_max'));
  621. $url = $this->addParameter($url,'price_min',$options['price_min']);
  622. $url = $this->addParameter($url,'price_max',$options['price_max']);
  623. $url = str_replace('?&','?',$url);
  624. return $url;
  625. }
  626.  
  627. // $options = array('product_offset' => '', 'page' => '', 'text' => '')
  628. function pageLink($options=array()) {
  629. return '<a href="'.$this->pageUrl($options).'" title="Go to page '.$options['page'].'">'.$options['text'].'</a>';
  630. }
  631.  
  632. function pageUrl($options=array()){
  633. if (isset($options['product_offset'])) {
  634. $url = $this->baseURL(array('product_offset'));
  635. $url = $this->addParameter($url,'product_offset',$options['product_offset']);
  636. } else {
  637. $url = $this->baseURL(array('deal_offset'));
  638. $url = $this->addParameter($url,'deal_offset',$options['deal_offset']);
  639. }
  640.  
  641. $url = str_replace('?&','?',$url);
  642. return $url;
  643. }
  644.  
  645. function addParameters($url,$paramNames_to_add) {
  646. // $url = $this->addParameter($url, 'include_deals', '1');
  647. $url = $this->addParameter($url, 'url_subid', $this->subid);
  648.  
  649. foreach($paramNames_to_add as $paramName) {
  650. if (isset($_REQUEST[$this->nameSpace.$paramName]) && (strlen($_REQUEST[$this->nameSpace.$paramName]) > 0)) {
  651. $url = $this->addParameter($url,$paramName,$_REQUEST[$this->nameSpace.$paramName]);
  652. }
  653. }
  654.  
  655. $url = str_replace($this->nameSpace, '', $url);
  656. $url = str_replace('?&','?',$url);
  657. return $url;
  658. }
  659.  
  660. function addParameter($url,$paramName,$paramValue) {
  661. if (strlen($paramValue) > 0) {
  662. $delimiter = (strpos($url,'?') > -1) ? '&' : '?';
  663. $url = $url.$delimiter.$this->nameSpace.$paramName.'='.urlencode($paramValue);
  664. }
  665. return $url;
  666. }
  667.  
  668. function baseURL($paramNamesToStrip) {
  669. $url = $this->requestURI();
  670. foreach ($_REQUEST as $key => $value) {
  671. foreach ($paramNamesToStrip as $paramName) {
  672. $url = $this->stripParameter($url,$paramName,$key,$value);
  673. }
  674. }
  675. return $url;
  676. }
  677.  
  678. function stripParameter($url,$paramName,$key,$value){
  679. if (strpos($key,$paramName) > -1 ) {
  680. $url = str_replace('&'.$key.'='.urlencode($value), "", $url);
  681. $url = str_replace($key.'='.urlencode($value), "", $url);
  682. }
  683. return $url;
  684. }
  685.  
  686. function requestURI() {
  687. if(!isset($_SERVER['REQUEST_URI'])) {
  688. $url = $_SERVER['PHP_SELF'];
  689. if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
  690. $url .= $HTTP_SERVER_VARS['QUERY_STRING'];
  691. }
  692. return $url;
  693. } else {
  694. return $_SERVER['REQUEST_URI'];
  695. }
  696. }
  697.  
  698. function requestResults($request){
  699. if (function_exists( 'curl_init')) {
  700. $session = curl_init($request);
  701. curl_setopt($session,CURLOPT_HEADER,false);
  702. curl_setopt($session,CURLOPT_RETURNTRANSFER,true);
  703. $response = curl_exec($session);
  704. curl_close($session);
  705. } else {
  706. $response = file_get_contents($request);
  707. }
  708. return simplexml_load_string($response);
  709. }
  710.  
  711. function findCachedMerchantTypes() {
  712. $this->cachedMerchantTypes = simplexml_load_string($this->cachedMerchantTypesXml());
  713. }
  714.  
  715. function cachedMerchantTypesXml(){
  716. return '<merchant_types total_count="44">
  717. <merchant_type name="Adult" merchant_count="18" id="34"/>
  718. <merchant_type name="Apparel & Accessories" merchant_count="129" id="26"/>
  719. <merchant_type name="Apparel - Lingerie" merchant_count="16" id="59"/>
  720. <merchant_type name="Apparel - Plus Size" merchant_count="30" id="14"/>
  721. <merchant_type name="Automotive & Motorcycle" merchant_count="24" id="5"/>
  722. <merchant_type name="Babies & Kids" merchant_count="62" id="9"/>
  723. <merchant_type name="Bags & Luggage" merchant_count="19" id="30"/>
  724. <merchant_type name="Beauty & Fragrance" merchant_count="70" id="40"/>
  725. <merchant_type name="Books & Entertainment" merchant_count="51" id="1"/>
  726. <merchant_type name="Career & Business Supplies" merchant_count="4" id="23"/>
  727. <merchant_type name="Christmas" merchant_count="6" id="39"/>
  728. <merchant_type name="Computers & Accessories" merchant_count="54" id="11"/>
  729. <merchant_type name="Costume & Party Supplies" merchant_count="35" id="20"/>
  730. <merchant_type name="Crafting & Scrapbooking" merchant_count="18" id="32"/>
  731. <merchant_type name="Department Stores" merchant_count="52" id="47"/>
  732. <merchant_type name="Electronics & Accessories" merchant_count="72" id="10"/>
  733. <merchant_type name="Flowers & Related" merchant_count="16" id="3"/>
  734. <merchant_type name="Food & Drink" merchant_count="62" id="37"/>
  735. <merchant_type name="Gifts & Collectibles" merchant_count="64" id="53"/>
  736. <merchant_type name="Green & Organic" merchant_count="14" id="25"/>
  737. <merchant_type name="Health & Wellness" merchant_count="39" id="41"/>
  738. <merchant_type name="Home & Garden" merchant_count="218" id="33"/>
  739. <merchant_type name="Jewelry" merchant_count="84" id="28"/>
  740. <merchant_type name="Magazines" merchant_count="14" id="36"/>
  741. <merchant_type name="Medical & Nursing" merchant_count="16" id="51"/>
  742. <merchant_type name="Mobile Phones & Accessories" merchant_count="18" id="4"/>
  743. <merchant_type name="Musical Supplies" merchant_count="15" id="18"/>
  744. <merchant_type name="Novelties & Collectibles" merchant_count="39" id="66"/>
  745. <merchant_type name="Office Supplies" merchant_count="31" id="45"/>
  746. <merchant_type name="Outdoor Gear" merchant_count="33" id="17"/>
  747. <merchant_type name="Pets & Animal Gear" merchant_count="47" id="19"/>
  748. <merchant_type name="Photo & Personalized" merchant_count="17" id="63"/>
  749. <merchant_type name="Religious" merchant_count="8" id="57"/>
  750. <merchant_type name="Shoes & Accessories" merchant_count="58" id="29"/>
  751. <merchant_type name="Sports & Recreation" merchant_count="102" id="31"/>
  752. <merchant_type name="Supplements" merchant_count="23" id="58"/>
  753. <merchant_type name="Tickets & Events" merchant_count="6" id="55"/>
  754. <merchant_type name="Toner & Ink" merchant_count="8" id="6"/>
  755. <merchant_type name="Tools & hardware" merchant_count="20" id="43"/>
  756. <merchant_type name="Toys, Games & Hobbies" merchant_count="53" id="27"/>
  757. <merchant_type name="Travel & Hotels" merchant_count="19" id="16"/>
  758. <merchant_type name="TV & Studio Stores" merchant_count="18" id="49"/>
  759. <merchant_type name="Vision Care" merchant_count="24" id="2"/>
  760. <merchant_type name="Weddings & Celebrations" merchant_count="27" id="35"/>
  761. </merchant_types>
  762. ';
  763. }
  764. }
  765. ?>
  766.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement