PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wp-lister-for-ebay/classes/model/EbayCategoriesModel.php

https://bitbucket.org/sanders_nick/my-maxi-skirt
PHP | 431 lines | 250 code | 97 blank | 84 comment | 21 complexity | 25f591bb06c4c2efe1c2a77fad8bbe6c MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-1.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * EbayCategoriesModel class
  4. *
  5. * responsible for managing ebay categories and store categories and talking to ebay
  6. *
  7. */
  8. // list of used EbatNs classes:
  9. // require_once 'EbatNs_ServiceProxy.php';
  10. // require_once 'GetCategoriesRequestType.php';
  11. // require_once 'GetStoreRequestType.php';
  12. // require_once 'CategoryType.php';
  13. // require_once 'EbatNs_DatabaseProvider.php';
  14. // require_once 'EbatNs_Logger.php';
  15. // require_once 'GetCategoryFeaturesRequestType.php';
  16. class EbayCategoriesModel extends WPL_Model {
  17. const table = 'ebay_categories';
  18. var $_session;
  19. var $_cs;
  20. var $_categoryVersion;
  21. function EbayCategoriesModel()
  22. {
  23. global $wpl_logger;
  24. $this->logger = &$wpl_logger;
  25. global $wpdb;
  26. $this->tablename = $wpdb->prefix . self::table;
  27. }
  28. function initCategoriesUpdate($session, $siteid)
  29. {
  30. $this->initServiceProxy($session);
  31. $this->logger->info('initCategoriesUpdate()');
  32. // set handler to receive CategoryType items from result
  33. $this->_cs->setHandler('CategoryType', array(& $this, 'storeCategory'));
  34. // we will not know the version till the first call went through !
  35. $this->_categoryVersion = -1;
  36. // truncate the db
  37. global $wpdb;
  38. $wpdb->query('truncate '.$this->tablename);
  39. // download the data of level 1 only !
  40. $req = new GetCategoriesRequestType();
  41. $req->CategorySiteID = $siteid;
  42. $req->LevelLimit = 1;
  43. $req->DetailLevel = 'ReturnAll';
  44. $res = $this->_cs->GetCategories($req);
  45. $this->_categoryVersion = $res->CategoryVersion;
  46. // let's update the version information on the top-level entries
  47. $data['version'] = $this->_categoryVersion;
  48. $wpdb->update( $this->tablename, $data, array( 'parent_cat_id' => '0') );
  49. // include other update tasks
  50. $tasks = array();
  51. $tasks[] = array(
  52. 'task' => 'loadShippingServices',
  53. 'displayName' => 'update shipping services',
  54. 'params' => array()
  55. );
  56. $tasks[] = array(
  57. 'task' => 'loadPaymentOptions',
  58. 'displayName' => 'update payment options'
  59. );
  60. $tasks[] = array(
  61. 'task' => 'loadStoreCategories',
  62. 'displayName' => 'update custom store categories'
  63. );
  64. // fetch the data back from the db and add a task for each top-level id
  65. $rows = $wpdb->get_results( "select cat_id, cat_name from $this->tablename where parent_cat_id=0", ARRAY_A );
  66. foreach ($rows as $row)
  67. {
  68. $this->logger->info('adding task for category #'.$row['cat_id'] . ' - '.$row['cat_name']);
  69. $task = array(
  70. 'task' => 'loadEbayCategoriesBranch',
  71. 'displayName' => $row['cat_name'],
  72. 'cat_id' => $row['cat_id']
  73. );
  74. $tasks[] = $task;
  75. }
  76. return $tasks;
  77. }
  78. function loadEbayCategoriesBranch( $cat_id, $session, $siteid)
  79. {
  80. $this->initServiceProxy($session);
  81. $this->logger->info('loadEbayCategoriesBranch() #'.$cat_id );
  82. // set handler to receive CategoryType items from result
  83. $this->_cs->setHandler('CategoryType', array(& $this, 'storeCategory'));
  84. // call GetCategories()
  85. $req = new GetCategoriesRequestType();
  86. $req->CategorySiteID = $siteid;
  87. $req->LevelLimit = 255;
  88. $req->DetailLevel = 'ReturnAll';
  89. $req->ViewAllNodes = true;
  90. $req->CategoryParent = $cat_id;
  91. $this->_cs->GetCategories($req);
  92. }
  93. function downloadCategories($session, $siteid)
  94. {
  95. $this->initServiceProxy($session);
  96. $this->logger->info('downloadCategories() - DEPRECATED');
  97. // // set handler to receive CategoryType items from result
  98. // $this->_cs->setHandler('CategoryType', array(& $this, 'storeCategory'));
  99. // // we will not know the version till the first call went through !
  100. // $this->_categoryVersion = -1;
  101. // // truncate the db
  102. // global $wpdb;
  103. // $wpdb->query('truncate '.$this->tablename);
  104. // // download the data of level 1 only !
  105. // $req = new GetCategoriesRequestType();
  106. // $req->CategorySiteID = $siteid;
  107. // $req->LevelLimit = 1;
  108. // $req->DetailLevel = 'ReturnAll';
  109. // $res = $this->_cs->GetCategories($req);
  110. // $this->_categoryVersion = $res->CategoryVersion;
  111. // // let's update the version information on the top-level entries
  112. // $data['version'] = $this->_categoryVersion;
  113. // $wpdb->update( $this->tablename, $data, array( 'parent_cat_id' => '0') );
  114. // // fetch the data back from the db and run a query for
  115. // // each top-level id
  116. // $rows = $wpdb->get_results( "select cat_id, cat_name from $this->tablename where parent_cat_id=0", ARRAY_A );
  117. // foreach ($rows as $row)
  118. // {
  119. // #echo "Loading tree for " . $row['cat_id'] . "<br>\n";
  120. // $this->logger->info('Loading tree for category #'.$row['cat_id'] . ' - '.$row['cat_name']);
  121. // $req = new GetCategoriesRequestType();
  122. // $req->CategorySiteID = $siteid;
  123. // $req->LevelLimit = 255;
  124. // $req->DetailLevel = 'ReturnAll';
  125. // $req->ViewAllNodes = true;
  126. // $req->CategoryParent = $row['cat_id'];
  127. // $this->_cs->GetCategories($req);
  128. // }
  129. }
  130. function storeCategory($type, & $Category)
  131. {
  132. global $wpdb;
  133. //#type $Category CategoryType
  134. $data['cat_id'] = $Category->CategoryID;
  135. if ( $Category->CategoryParentID[0] == $Category->CategoryID ) {
  136. // avoid duplicate main categories due to the structure of the response
  137. if ( $this->getItem( $Category->CategoryID ) ) return true;
  138. $data['parent_cat_id'] = '0';
  139. } else {
  140. $data['parent_cat_id'] = $Category->CategoryParentID[0];
  141. }
  142. $data['cat_name'] = $Category->CategoryName;
  143. $data['level'] = $Category->CategoryLevel;
  144. $data['leaf'] = $Category->LeafCategory;
  145. $data['version'] = $this->_categoryVersion;
  146. // remove unrecognizable chars from category name
  147. // $data['cat_name'] = trim(str_replace('?','', $data['cat_name'] ));
  148. $wpdb->insert( $this->tablename, $data );
  149. return true;
  150. }
  151. function downloadStoreCategories($session)
  152. {
  153. global $wpdb;
  154. $this->initServiceProxy($session);
  155. $this->logger->info('downloadStoreCategories()');
  156. // download store categories
  157. $req = new GetStoreRequestType();
  158. $req->CategoryStructureOnly = true;
  159. $res = $this->_cs->GetStore($req);
  160. // empty table
  161. $wpdb->query( "DELETE FROM {$wpdb->prefix}ebay_store_categories" );
  162. // insert each category
  163. foreach( $res->Store->CustomCategories as $Category ) {
  164. $this->handleStoreCategory( $Category, 1, 0 );
  165. }
  166. }
  167. function handleStoreCategory( & $Category, $level, $parent_cat_id )
  168. {
  169. global $wpdb;
  170. if ( $level > 5 ) return false;
  171. $data = array();
  172. $data['cat_id'] = $Category->CategoryID;
  173. $data['cat_name'] = $Category->Name;
  174. $data['order'] = $Category->Order;
  175. $data['leaf'] = is_array( $Category->ChildCategory ) ? '0' : '1';
  176. $data['level'] = $level;
  177. $data['parent_cat_id'] = $parent_cat_id;
  178. // move "Other" category to the end of the list
  179. if ( $data['order'] == 0 ) $data['order'] = 999;
  180. // insert row - and manually set field type to string.
  181. // without parameter '%s' $wpdb would convert cat_id to int instead of bigint - on some servers!
  182. $wpdb->insert( $wpdb->prefix.'ebay_store_categories', $data, '%s' );
  183. // handle children recursively
  184. if ( is_array( $Category->ChildCategory ) ) {
  185. foreach ( $Category->ChildCategory as $ChildCategory ) {
  186. $this->handleStoreCategory( $ChildCategory, $level + 1, $Category->CategoryID );
  187. }
  188. }
  189. }
  190. function getCategoryConditions($session, $category_id )
  191. {
  192. $this->initServiceProxy($session);
  193. // download store categories
  194. $req = new GetCategoryFeaturesRequestType();
  195. $req->setCategoryID( $category_id );
  196. $req->setDetailLevel( 'ReturnAll' );
  197. $res = $this->_cs->GetCategoryFeatures($req);
  198. $this->logger->info('getCategoryConditions() for category ID '.$category_id);
  199. // $this->logger->info('getCategoryConditions: '.print_r($res,1));
  200. // $conditions as array
  201. // if (!isset($res->Category[0]->ConditionValues->Condition)) return null;
  202. if ( count($res->Category[0]->ConditionValues->Condition) > 0 )
  203. foreach ($res->Category[0]->ConditionValues->Condition as $Condition) {
  204. $conditions[$Condition->ID] = $Condition->DisplayName;
  205. }
  206. // $this->logger->info('getCategoryConditions: '.print_r($conditions,1));
  207. if (!is_array($conditions)) $conditions = 'none';
  208. return array( $category_id => $conditions );
  209. }
  210. function getCategorySpecifics($session, $category_id )
  211. {
  212. $this->initServiceProxy($session);
  213. // download store categories
  214. $req = new GetCategorySpecificsRequestType();
  215. $req->setCategoryID( $category_id );
  216. $req->setDetailLevel( 'ReturnAll' );
  217. $res = $this->_cs->GetCategorySpecifics($req);
  218. $this->logger->info('getCategorySpecifics() for category ID '.$category_id);
  219. // $this->logger->info('getCategorySpecifics: '.print_r($res,1));
  220. // $specifics as array
  221. // if (!isset($res->Category[0]->ConditionValues->Condition)) return null;
  222. if ( count($res->Recommendations[0]->NameRecommendation) > 0 )
  223. foreach ($res->Recommendations[0]->NameRecommendation as $Recommendation) {
  224. $new_specs = new stdClass();
  225. $new_specs->Name = $Recommendation->Name;
  226. $new_specs->ValueType = $Recommendation->ValidationRules->ValueType;
  227. $new_specs->MinValues = $Recommendation->ValidationRules->MinValues;
  228. $new_specs->MaxValues = $Recommendation->ValidationRules->MaxValues;
  229. $new_specs->SelectionMode = $Recommendation->ValidationRules->SelectionMode;
  230. if ( is_array( $Recommendation->ValueRecommendation ) ) {
  231. foreach ($Recommendation->ValueRecommendation as $recommendedValue) {
  232. $new_specs->recommendedValues[] = $recommendedValue->Value;
  233. }
  234. }
  235. #$specifics[$Recommendation->Name] = $new_specs;
  236. $specifics[] = $new_specs;
  237. }
  238. // $this->logger->info('getCategorySpecifics: '.print_r($specifics,1));
  239. if (!is_array($specifics)) $specifics = 'none';
  240. return array( $category_id => $specifics );
  241. }
  242. /* the following methods could go into another class, since they use wpdb instead of EbatNs_DatabaseProvider */
  243. function getAll() {
  244. global $wpdb;
  245. $this->tablename = $wpdb->prefix . self::table;
  246. $profiles = $wpdb->get_results("
  247. SELECT *
  248. FROM $this->tablename
  249. ORDER BY cat_name
  250. ", ARRAY_A);
  251. return $profiles;
  252. }
  253. function getItem( $id ) {
  254. global $wpdb;
  255. $this->tablename = $wpdb->prefix . self::table;
  256. $item = $wpdb->get_row("
  257. SELECT *
  258. FROM $this->tablename
  259. WHERE cat_id = '$id'
  260. ", ARRAY_A);
  261. return $item;
  262. }
  263. function getCategoryName( $id ) {
  264. global $wpdb;
  265. $this->tablename = $wpdb->prefix . self::table;
  266. $value = $wpdb->get_var("
  267. SELECT cat_name
  268. FROM $this->tablename
  269. WHERE cat_id = '$id'
  270. ");
  271. # echo mysql_error();
  272. # echo $wpdb->last_query;
  273. return $value;
  274. }
  275. function getChildrenOf( $id ) {
  276. global $wpdb;
  277. $this->tablename = $wpdb->prefix . self::table;
  278. $items = $wpdb->get_results("
  279. SELECT DISTINCT *
  280. FROM $this->tablename
  281. WHERE parent_cat_id = '$id'
  282. ", ARRAY_A);
  283. return $items;
  284. }
  285. function getStoreCategoryName( $id ) {
  286. global $wpdb;
  287. // $this->tablename = $wpdb->prefix . self::table;
  288. $this->tablename = $wpdb->prefix . 'ebay_store_categories';
  289. $value = $wpdb->get_var("
  290. SELECT cat_name
  291. FROM $this->tablename
  292. WHERE cat_id = '$id'
  293. ");
  294. return $value;
  295. }
  296. function getChildrenOfStoreCategory( $id ) {
  297. global $wpdb;
  298. // $this->tablename = $wpdb->prefix . self::table;
  299. $this->tablename = $wpdb->prefix . 'ebay_store_categories';
  300. $items = $wpdb->get_results("
  301. SELECT DISTINCT *
  302. FROM $this->tablename
  303. WHERE parent_cat_id = '$id'
  304. ORDER BY `order` ASC
  305. ", ARRAY_A);
  306. return $items;
  307. }
  308. /* recursively get full ebay category name */
  309. function getFullEbayCategoryName( $cat_id ) {
  310. global $wpdb;
  311. $result = $wpdb->get_row('SELECT * FROM '.$wpdb->prefix.'ebay_categories WHERE cat_id = '.intval($cat_id) );
  312. if ( $result ) {
  313. if ( $result->parent_cat_id != 0 ) {
  314. $parentname = self::getFullEbayCategoryName( $result->parent_cat_id ) . ' &raquo; ';
  315. } else {
  316. $parentname = '';
  317. }
  318. return $parentname . $result->cat_name;
  319. }
  320. }
  321. /* recursively get full store category name */
  322. function getFullStoreCategoryName( $cat_id ) {
  323. global $wpdb;
  324. $result = $wpdb->get_row('SELECT * FROM '.$wpdb->prefix.'ebay_store_categories WHERE cat_id = '.intval($cat_id) );
  325. if ( $result ) {
  326. if ( $result->parent_cat_id != 0 ) {
  327. $parentname = self::getFullStoreCategoryName( $result->parent_cat_id ) . ' &raquo; ';
  328. } else {
  329. $parentname = '';
  330. }
  331. return $parentname . $result->cat_name;
  332. }
  333. }
  334. }