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

/tests/canoo/tests/testcases/level0/2.7.30-beta/plug-ins/www/admin/plugins/oxMarket/pcApiClient/oxPublisherConsoleMarketPluginClient.php

https://bitbucket.org/valmy/openx
PHP | 423 lines | 266 code | 33 blank | 124 comment | 17 complexity | 4e214523b6cb1119432381f9c089ca88 MD5 | raw file
  1. <?php
  2. /*
  3. +---------------------------------------------------------------------------+
  4. | OpenX v2.8 |
  5. | ========== |
  6. | |
  7. | Copyright (c) 2003-2009 OpenX Limited |
  8. | For contact details, see: http://www.openx.org/ |
  9. | |
  10. | This program is free software; you can redistribute it and/or modify |
  11. | it under the terms of the GNU General Public License as published by |
  12. | the Free Software Foundation; either version 2 of the License, or |
  13. | (at your option) any later version. |
  14. | |
  15. | This program is distributed in the hope that it will be useful, |
  16. | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  17. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  18. | GNU General Public License for more details. |
  19. | |
  20. | You should have received a copy of the GNU General Public License |
  21. | along with this program; if not, write to the Free Software |
  22. | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
  23. +---------------------------------------------------------------------------+
  24. $Id: oxPublisherConsoleMarketPluginClient.php 29196 2008-11-20 14:16:53Z apetlyovanyy $
  25. */
  26. require_once MAX_PATH . '/lib/OA/Dal.php';
  27. require_once MAX_PATH . '/lib/OX/M2M/XmlRpcExecutor.php';
  28. require_once MAX_PATH . '/lib/Zend/Http/Exception.php';
  29. require_once MAX_PATH . '/lib/max/Dal/DataObjects/Accounts.php';
  30. require_once MAX_PATH . '/lib/OX/M2M/ZendXmlRpcExecutor.php';
  31. require_once MAX_PATH . '/lib/OX/M2M/M2MProtectedRpc.php';
  32. require_once MAX_PATH . '/lib/OA/Central/M2MProtectedRpc.php';
  33. require_once dirname(__FILE__) . '/oxPublisherConsoleClient.php';
  34. require_once dirname(__FILE__) . '/oxPublisherConsoleClientException.php';
  35. /**
  36. * OpenX Market plugin - Publisher Console API client for plugin
  37. *
  38. * @package OpenXPlugin
  39. * @subpackage openXMarket
  40. * @author Andriy Petlyovanyy <apetlyovanyy@lohika.com>
  41. */
  42. class Plugins_admin_oxMarket_PublisherConsoleMarketPluginClient
  43. {
  44. /**
  45. * ext_market_assoc_data statuses - same values as Publisher Console account statuses
  46. */
  47. const LINK_IS_VALID_STATUS = 0;
  48. const ACCOUNT_DISABLED_STATUS = 1;
  49. /**
  50. * Error codes that change link status
  51. */
  52. const XML_ERR_ACCOUNT_BLOCKED = 909;
  53. /**
  54. * @var Plugins_admin_oxMarket_PublisherConsoleClient
  55. */
  56. private $pc_api_client;
  57. /**
  58. * @param array $array
  59. * return empty array if argument is null
  60. */
  61. private function putEmptyArrayIfNull($array)
  62. {
  63. if (isset($array)) {
  64. return $array;
  65. }
  66. else {
  67. return array();
  68. }
  69. }
  70. private function ensureStatusAndUpdatePcAccountId()
  71. {
  72. $publisher_account_id = null;
  73. $account_status = null;
  74. $this->getAssociatedPcAccountIdAndStatus($publisher_account_id,
  75. $account_status);
  76. if (!isset($account_status)) {
  77. throw new Plugins_admin_oxMarket_PublisherConsoleClientException(
  78. 'There is no association between PC and OXP accounts');
  79. }
  80. else {
  81. if (self::LINK_IS_VALID_STATUS != $account_status) {
  82. //TODO: Add more specific errors when such list would be created
  83. throw
  84. new Plugins_admin_oxMarket_PublisherConsoleClientException(
  85. 'Association of PC account with OXP account is invalid',
  86. $account_status);
  87. }
  88. else {
  89. $this->pc_api_client->setPublisherAccountId(
  90. $publisher_account_id);
  91. }
  92. }
  93. }
  94. /**
  95. * @param integer $publisher_account_id
  96. * @param integer $association_status
  97. */
  98. private function getAssociatedPcAccountIdAndStatus(&$publisher_account_id,
  99. &$association_status)
  100. {
  101. $oAccountAssocData = OA_Dal::factoryDO('ext_market_assoc_data');
  102. $adminAccountId = DataObjects_Accounts::getAdminAccountId();
  103. if (isset($adminAccountId)) {
  104. $oAccountAssocData->get('account_id', $adminAccountId);
  105. $publisher_account_id = $oAccountAssocData->publisher_account_id;
  106. $association_status = $oAccountAssocData->status;
  107. }
  108. }
  109. public function __construct()
  110. {
  111. $aConf = $GLOBALS['_MAX']['CONF'];
  112. $apiUrl = $aConf['oxMarket']['marketPcApiUrl'];
  113. $oServiceExecutor = new OX_M2M_ZendXmlRpcExecutor($apiUrl);
  114. $oM2MXmlRpc = new OA_Central_M2MProtectedRpc($oServiceExecutor);
  115. $this->pc_api_client =
  116. new Plugins_admin_oxMarket_PublisherConsoleClient($oM2MXmlRpc);
  117. }
  118. /**
  119. * Check if there is already association between
  120. * OXP and PC accounts
  121. *
  122. * @return boolean
  123. */
  124. public function hasAssociationWithPc()
  125. {
  126. $publisher_account_id = null;
  127. $account_status = null;
  128. $this->getAssociatedPcAccountIdAndStatus($publisher_account_id,
  129. $account_status);
  130. return isset($publisher_account_id);
  131. }
  132. /**
  133. * Return publisher account id for OXP admin account
  134. *
  135. * @return integer or null
  136. */
  137. public function getPcAccountId()
  138. {
  139. $publisher_account_id = null;
  140. $account_status = null;
  141. $this->getAssociatedPcAccountIdAndStatus($publisher_account_id,
  142. $account_status);
  143. return $publisher_account_id;
  144. }
  145. /**
  146. * Check status of association between OXP and PC accounts
  147. *
  148. * @return integer or null status code or null in case of no association
  149. */
  150. public function getAssociationWithPcStatus()
  151. {
  152. $publisher_account_id = null;
  153. $account_status = null;
  154. $this->getAssociatedPcAccountIdAndStatus($publisher_account_id,
  155. $account_status);
  156. return $account_status;
  157. }
  158. /**
  159. * @param string $username
  160. * @param string $password
  161. * @return boolean
  162. */
  163. public function linkOxp($username, $password)
  164. {
  165. $publisher_account_id = $this->pc_api_client->linkOxp(
  166. $username, $password);
  167. $doExtMarket = OA_DAL::factoryDO('ext_market_assoc_data');
  168. $aExtMarketRecords = $doExtMarket->getAll();
  169. if (count($aExtMarketRecords) > 0) {
  170. throw new Plugins_admin_oxMarket_PublisherConsoleClientException(
  171. 'There is already publisher_account_id on the OXP');
  172. }
  173. else {
  174. $account_id = DataObjects_Accounts::getAdminAccountId();
  175. if (!isset($account_id)) {
  176. throw
  177. new Plugins_admin_oxMarket_PublisherConsoleClientException(
  178. 'There is no admin account id in database');
  179. }
  180. $doExtMarket->account_id = $account_id;
  181. $doExtMarket->publisher_account_id = $publisher_account_id;
  182. $doExtMarket->status = self::LINK_IS_VALID_STATUS;
  183. $doExtMarket->insert();
  184. }
  185. $this->pc_api_client->setPublisherAccountId($publisher_account_id);
  186. return true;
  187. }
  188. /**
  189. * @param integer $lastUpdate
  190. * @return string statistics file content
  191. */
  192. public function oxmStatistics($lastUpdate)
  193. {
  194. try {
  195. $this->ensureStatusAndUpdatePcAccountId();
  196. return $this->pc_api_client->oxmStatistics($lastUpdate);
  197. } catch (Exception $e) {
  198. $this->setStatusByException($e);
  199. }
  200. }
  201. /**
  202. * @param integer $lastUpdate
  203. * @return string statistics file content
  204. */
  205. public function oxmStatisticsLimited($lastUpdate)
  206. {
  207. try {
  208. $this->ensureStatusAndUpdatePcAccountId();
  209. return $this->pc_api_client->oxmStatisticsLimited($lastUpdate);
  210. } catch (Exception $e) {
  211. $this->setStatusByException($e);
  212. }
  213. }
  214. /**
  215. * @param string $websiteUrl
  216. * @return integer website id
  217. */
  218. public function newWebsite($websiteUrl)
  219. {
  220. try {
  221. $this->ensureStatusAndUpdatePcAccountId();
  222. return $this->pc_api_client->newWebsite($websiteUrl);
  223. } catch (Exception $e) {
  224. $this->setStatusByException($e);
  225. }
  226. }
  227. /**
  228. * @param integer $websiteId
  229. * @param string $websiteUrl
  230. * @param array $att_ex
  231. * @param array $cat_ex
  232. * @param array $typ_ex
  233. */
  234. public function updateWebsite($websiteId, $websiteUrl, $att_ex,
  235. $cat_ex, $typ_ex)
  236. {
  237. try {
  238. $this->ensureStatusAndUpdatePcAccountId();
  239. return $this->pc_api_client->updateWebsite($websiteId, $websiteUrl,
  240. $this->putEmptyArrayIfNull($att_ex),
  241. $this->putEmptyArrayIfNull($cat_ex),
  242. $this->putEmptyArrayIfNull($typ_ex));
  243. } catch (Exception $e) {
  244. $this->setStatusByException($e);
  245. }
  246. }
  247. /**
  248. * Synchronize status with market and return new status
  249. *
  250. * @return int
  251. */
  252. public function updateAccountStatus()
  253. {
  254. $publisher_account_id = null;
  255. $currentStatus = null;
  256. $doExtMarket = OA_Dal::factoryDO('ext_market_assoc_data');
  257. $adminAccountId = DataObjects_Accounts::getAdminAccountId();
  258. if (isset($adminAccountId)) {
  259. $doExtMarket->get('account_id', $adminAccountId);
  260. $publisher_account_id = $doExtMarket->publisher_account_id;
  261. $currentStatus = $doExtMarket->status;
  262. }
  263. $this->pc_api_client->setPublisherAccountId($publisher_account_id);
  264. $newStatus = $this->pc_api_client->getAccountStatus();
  265. if ($newStatus != $currentStatus) {
  266. $doExtMarket->status = $newStatus;
  267. $doExtMarket->update();
  268. }
  269. return $newStatus;
  270. }
  271. /**
  272. * Set status to account disabled if exception code is one of account disabling codes
  273. *
  274. * @param Exception $exception
  275. * @throws Exception rethrows given exception
  276. */
  277. protected function setStatusByException(Exception $exception)
  278. {
  279. if ($exception->getCode() == self::XML_ERR_ACCOUNT_BLOCKED) {
  280. $adminAccountId = DataObjects_Accounts::getAdminAccountId();
  281. if (isset($adminAccountId)) {
  282. $doExtMarket = OA_DAL::factoryDO('ext_market_assoc_data');
  283. $doExtMarket->get($adminAccountId);
  284. $doExtMarket->status = self::ACCOUNT_DISABLED_STATUS;;
  285. $doExtMarket->update();
  286. }
  287. }
  288. throw $exception;
  289. }
  290. /**
  291. * Returns default restrictions.
  292. *
  293. * XXX hardcoded for now
  294. * @return array default settings
  295. */
  296. public function getDefaultRestrictions()
  297. {
  298. return array(
  299. 'attribute' => array(),
  300. 'category' => array(1, 10, 26),
  301. 'type' => array()
  302. );
  303. }
  304. /**
  305. * Returns array of Ad Categories used in marketplace
  306. *
  307. * XXX hardcoded for now
  308. * @return array array of categories names where array keys are ids
  309. */
  310. public function getAdCategories() {
  311. return array(
  312. '1' => 'Adult Entertainment',
  313. '2' => 'Arts and Entertainment',
  314. '3' => 'Automotive',
  315. '4' => 'Business',
  316. '5' => 'Careers and Jobs',
  317. '6' => 'Clothing and Apparel',
  318. '7' => 'Consumer Electronics',
  319. '8' => 'Dating and Relationships',
  320. '9' => 'Family and Parenting',
  321. '10' => 'Firearms and Weapons',
  322. '11' => 'Food and Drink',
  323. '12' => 'Gambling',
  324. '13' => 'Government',
  325. '14' => 'Health',
  326. '15' => 'Hobbies and Interests',
  327. '16' => 'Holidays',
  328. '17' => 'Home and Garden',
  329. '18' => 'Humanities and Social Sciences',
  330. '19' => 'Internet',
  331. '20' => 'Pets',
  332. '30' => 'Personal Finance',
  333. '21' => 'Property and Real Estate',
  334. '22' => 'Religion and Spirituality',
  335. '23' => 'Research and Education',
  336. '24' => 'Science and Engineering',
  337. '25' => 'Sports and Recreation',
  338. '26' => 'Tobacco and Smoking',
  339. '27' => 'Toys and Games',
  340. '28' => 'Travel and Tourism',
  341. '29' => 'Weather'
  342. );
  343. }
  344. /**
  345. * Returns array of Creative Types used in marketplace
  346. *
  347. * XXX hardcoded for now
  348. * @return array array of types names where array keys are ids
  349. */
  350. function getCreativeTypes()
  351. {
  352. return array(
  353. '1' => 'Image',
  354. '2' => 'Flash',
  355. '3' => 'Text',
  356. '4' => 'Video',
  357. '5' => 'DHTML',
  358. '6' => 'Expandable',
  359. '7' => 'Audio',
  360. '8' => 'Pop-Up',
  361. '9' => 'Pop-Under'
  362. );
  363. }
  364. /**
  365. * Returns array of Creative Attributes used in marketplace
  366. *
  367. * XXX hardcoded for now
  368. * @return array array of attributes names where array keys are ids
  369. */
  370. function getCreativeAttributes()
  371. {
  372. return array(
  373. '1' => 'Alcohol',
  374. '2' => 'Audio/Video',
  375. '3' => 'Dating/Romance',
  376. '4' => 'Download',
  377. '5' => 'English',
  378. '6' => 'Error Box',
  379. '7' => 'Excessive Animation',
  380. '8' => 'Gambling',
  381. '9' => 'Holiday',
  382. '10' => 'Incentivized',
  383. '11' => 'Male Health',
  384. '12' => 'Membership Club',
  385. '13' => 'Political',
  386. '14' => 'Suggestive',
  387. '15' => 'Tobacco'
  388. );
  389. }
  390. }