User.php 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ 1Qeh3xzMW2NC6NPKe63pm407X7W6ifuoOD53vDSNyZk=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2017 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.10 [rev.7.10.01]
  11. */
  12. /**
  13. * users table row object model
  14. */
  15. namespace Ppb\Db\Table\Row;
  16. use Cube\Controller\Request,
  17. Cube\Db\Expr,
  18. Cube\Db\Select,
  19. Cube\Controller\Front,
  20. Cube\View\Helper\Url as UrlViewHelper,
  21. Ppb\Model\Shipping as ShippingModel,
  22. Ppb\Service;
  23. class User extends AbstractRow
  24. {
  25. /**
  26. * name of user token cookie
  27. */
  28. const USER_TOKEN = 'UserToken';
  29. /**
  30. * remember me cookie
  31. */
  32. const REMEMBER_ME = 'RememberMe';
  33. /**
  34. *
  35. * reputation service
  36. *
  37. * @var \Ppb\Service\Reputation
  38. */
  39. protected $_reputation;
  40. /**
  41. *
  42. * shipping model
  43. *
  44. * @var \Ppb\Model\Shipping
  45. */
  46. protected $_shipping;
  47. /**
  48. *
  49. * generate unique user token
  50. *
  51. * @return string
  52. */
  53. public static function generateToken()
  54. {
  55. return uniqid(time(), true);
  56. }
  57. /**
  58. *
  59. * get reputation service
  60. *
  61. * @return \Ppb\Service\Reputation
  62. */
  63. public function getReputation()
  64. {
  65. if (!$this->_reputation instanceof Service\Reputation) {
  66. $this->setReputation(
  67. new Service\Reputation());
  68. }
  69. return $this->_reputation;
  70. }
  71. /**
  72. *
  73. * set reputation service
  74. *
  75. * @param \Ppb\Service\Reputation $reputation
  76. *
  77. * @return \Ppb\Db\Table\Row\User
  78. */
  79. public function setReputation(Service\Reputation $reputation)
  80. {
  81. $this->_reputation = $reputation;
  82. return $this;
  83. }
  84. /**
  85. *
  86. * get shipping model
  87. *
  88. * @return \Ppb\Model\Shipping
  89. */
  90. public function getShipping()
  91. {
  92. if (!$this->_shipping instanceof ShippingModel) {
  93. $this->setShipping(
  94. new ShippingModel($this));
  95. }
  96. return $this->_shipping;
  97. }
  98. /**
  99. *
  100. * set shipping model
  101. *
  102. * @param \Ppb\Model\Shipping $shipping
  103. *
  104. * @return \Ppb\Db\Table\Row\User
  105. */
  106. public function setShipping(ShippingModel $shipping)
  107. {
  108. $this->_shipping = $shipping;
  109. return $this;
  110. }
  111. /**
  112. *
  113. * checks if the user accepts public questions
  114. *
  115. * @return bool
  116. */
  117. public function acceptPublicQuestions()
  118. {
  119. $settings = $this->getSettings();
  120. if ($settings['enable_public_questions'] && $this->getGlobalSettings('enable_public_questions')) {
  121. return true;
  122. }
  123. return false;
  124. }
  125. /**
  126. *
  127. * check if user account is active
  128. *
  129. * @return bool
  130. */
  131. public function isActive()
  132. {
  133. if (
  134. $this->getData('active') &&
  135. $this->getData('approved') &&
  136. $this->getData('mail_activated') &&
  137. $this->getData('payment_status') == 'confirmed'
  138. ) {
  139. return true;
  140. }
  141. return false;
  142. }
  143. /**
  144. *
  145. * check if the user is a seller
  146. *
  147. * @return bool
  148. */
  149. public function isSeller()
  150. {
  151. $settings = $this->getSettings();
  152. $privateSite = (isset($settings['private_site'])) ? (bool)$settings['private_site'] : false;
  153. if (!$privateSite || $this->getData('is_seller')) {
  154. return true;
  155. }
  156. return false;
  157. }
  158. /**
  159. *
  160. * check if the user can list items
  161. * (is a seller and if seller verification is required, is verified)
  162. *
  163. * @7.9 seller can only list items if the account is active
  164. *
  165. * @return bool
  166. */
  167. public function canList()
  168. {
  169. $settings = $this->getSettings();
  170. $isSeller = $this->isSeller();
  171. if ($isSeller &&
  172. $this->isActive() &&
  173. $this->isForceStore() &&
  174. ($this->isVerified() || !$settings['seller_verification_mandatory'])
  175. ) {
  176. return true;
  177. }
  178. return false;
  179. }
  180. /**
  181. *
  182. * get user role
  183. * we have Guest, Incomplete, Suspended, Buyer, Seller, BuyerSeller
  184. *
  185. * @return string the role of the user
  186. */
  187. public function getRole()
  188. {
  189. if (!count($this)) {
  190. return 'Guest';
  191. }
  192. if ($this->getData('approved') && $this->getData('mail_activated') && $this->getData('payment_status') == 'confirmed') {
  193. if ($this->getData('active')) {
  194. if ($this->isSeller()) {
  195. return 'BuyerSeller';
  196. }
  197. else {
  198. return 'Buyer';
  199. }
  200. }
  201. else {
  202. return 'Suspended';
  203. }
  204. }
  205. else {
  206. return 'Incomplete';
  207. }
  208. }
  209. /**
  210. *
  211. * get store status (true if enabled and items can be listed in it, false otherwise)
  212. *
  213. * @param bool $simple if this flag is set, it will not check if items can be listed in the store
  214. *
  215. * @return bool true if user can list in store, false otherwise
  216. */
  217. public function storeStatus($simple = false)
  218. {
  219. $settings = $this->getSettings();
  220. if ($settings['enable_stores'] && $this->getData('store_active')) {
  221. if ($simple === true) {
  222. return true;
  223. }
  224. if ($this->getData('store_subscription_id')) {
  225. $subscription = $this->findParentRow('\Ppb\Db\Table\StoresSubscriptions');
  226. if ($this->countStoreListings() < $subscription['listings']) {
  227. return true;
  228. }
  229. return false;
  230. }
  231. else {
  232. // default store - unlimited items
  233. return true;
  234. }
  235. }
  236. return false;
  237. }
  238. /**
  239. *
  240. * get store logo
  241. *
  242. * @return string|null
  243. */
  244. public function storeLogo()
  245. {
  246. $storeSettings = $this->getStoreSettings();
  247. $logo = null;
  248. if (!empty($storeSettings['store_logo_path'])) {
  249. $logo = (is_array($storeSettings['store_logo_path'])) ?
  250. current(array_filter($storeSettings['store_logo_path'])) : $storeSettings['store_logo_path'];
  251. }
  252. return $logo;
  253. }
  254. /**
  255. *
  256. * returns the user's total number of store listings (open/closed, active/suspended)
  257. *
  258. * @return int
  259. */
  260. public function countStoreListings()
  261. {
  262. return $this->countListings('store');
  263. }
  264. /**
  265. *
  266. * count a user's total number of live listings
  267. *
  268. * @param string $show
  269. *
  270. * @return int
  271. */
  272. public function countListings($show = null)
  273. {
  274. $show = (in_array($show, array('store', 'other-items'))) ? $show : 'by-user';
  275. $request = new Request();
  276. $request->clearParams()
  277. ->setParam('show', $show)
  278. ->setParam('user_id', $this->getData('id'));
  279. $listingsService = new Service\Listings();
  280. $select = $listingsService->select(Service\Listings::SELECT_LISTINGS, $request);
  281. $select->reset(Select::COLUMNS)
  282. ->reset(Select::ORDER);
  283. $select->columns(array('nb_rows' => new Expr('count(*)')));
  284. $stmt = $select->query();
  285. return (integer)$stmt->fetchColumn('nb_rows');
  286. }
  287. /**
  288. *
  289. * get user payment mode
  290. *
  291. * @return string will return the account type ('live', 'account')
  292. */
  293. public function userPaymentMode()
  294. {
  295. $settings = $this->getSettings();
  296. if ($settings['user_account_type'] == 'global') {
  297. return $settings['payment_mode'];
  298. }
  299. return $this->getData('account_mode');
  300. }
  301. /**
  302. *
  303. * update user balance (if in account mode)
  304. * on the front end only listings from active users are shown
  305. *
  306. * @param float $amount positive = debit | negative = credit
  307. *
  308. * @return \Ppb\Db\Table\Row\User
  309. */
  310. public function updateBalance($amount)
  311. {
  312. $paymentMode = $this->userPaymentMode();
  313. if ($paymentMode == 'account') {
  314. $settings = $this->getSettings();
  315. $balance = $this->getData('balance') + $amount;
  316. $this->save(array(
  317. 'balance' => $balance,
  318. ));
  319. if ($amount > 0 && $balance > $this->getData('max_debit')) {
  320. $sendEmail = false;
  321. if ($settings['suspend_over_limit_accounts']) {
  322. $this->updateActive(0);
  323. $sendEmail = true;
  324. }
  325. else if (!$this->getData('debit_exceeded_date')) { // only set this flag if the column is null
  326. $sendEmail = true;
  327. $this->save(array(
  328. 'debit_exceeded_date' => new Expr('now()')
  329. ));
  330. }
  331. if ($sendEmail) {
  332. $mail = new \Members\Model\Mail\User();
  333. $mail->accountBalanceExceeded($this)->send();
  334. }
  335. }
  336. else if ($amount < 0 && $balance < $this->getData('max_debit')) {
  337. $this->save(array(
  338. 'debit_exceeded_date' => new Expr('null') // reset cron suspension flag
  339. ));
  340. $this->updateActive(1);
  341. }
  342. }
  343. return $this;
  344. }
  345. /**
  346. *
  347. * when suspending the user, also suspend his active listings
  348. * inactive listings will not have their active flag altered
  349. *
  350. * @param int $active
  351. *
  352. * @return $this
  353. */
  354. public function updateActive($active = 1)
  355. {
  356. $listingsFlag = ($active) ? -1 : 1;
  357. $listingsService = new Service\Listings();
  358. $listingsService->getTable()->update(
  359. array('active' => (-1) * $listingsFlag),
  360. "user_id = '" . $this->getData('id') . "' AND active = '{$listingsFlag}'");
  361. $this->save(array(
  362. 'active' => $active
  363. ));
  364. return $this;
  365. }
  366. /**
  367. *
  368. * update the store settings for the user
  369. *
  370. * @param array $data
  371. *
  372. * @return \Ppb\Db\Table\Row\User
  373. */
  374. public function updateStoreSettings(array $data)
  375. {
  376. $array = array();
  377. // if store account id is changed, store_active = 0
  378. if (!empty($data['store_subscription_id'])) {
  379. if ($data['store_subscription_id'] != $this->getData('store_subscription_id')) {
  380. $array['store_active'] = 0;
  381. }
  382. $array['store_subscription_id'] = $data['store_subscription_id'];
  383. }
  384. if (isset($data['store_name'])) {
  385. $array['store_name'] = $data['store_name'];
  386. $array['store_slug'] = $this->_sluggizeStoreName($array['store_name']);
  387. }
  388. if (isset($data['store_category_id'])) {
  389. $array['store_category_id'] = $data['store_category_id'];
  390. }
  391. $data = array_merge($this->getStoreSettings(), $data);
  392. $array['store_settings'] = serialize($data);
  393. $this->save($array);
  394. return $this;
  395. }
  396. /**
  397. *
  398. * update the postage settings for the user
  399. *
  400. * @param array $data
  401. *
  402. * @return \Ppb\Db\Table\Row\User
  403. */
  404. public function updatePostageSettings(array $data)
  405. {
  406. $this->save(array(
  407. 'postage_settings' => serialize($data)
  408. ));
  409. return $this;
  410. }
  411. /**
  412. *
  413. * update the selling prefilled fields for the user
  414. *
  415. * @param array $data
  416. *
  417. * @return \Ppb\Db\Table\Row\User
  418. */
  419. public function updatePrefilledFields(array $data)
  420. {
  421. $this->save(array(
  422. 'prefilled_fields' => serialize($data)
  423. ));
  424. return $this;
  425. }
  426. /**
  427. *
  428. * update the global settings field for the user
  429. *
  430. * @param array $data
  431. *
  432. * @return \Ppb\Db\Table\Row\User
  433. */
  434. public function updateGlobalSettings(array $data)
  435. {
  436. $data = array_merge($this->getGlobalSettings(), $data);
  437. $this->save(array(
  438. 'global_settings' => serialize($data)
  439. ));
  440. return $this;
  441. }
  442. /**
  443. *
  444. * update preferred seller status
  445. *
  446. * @param int $flag
  447. *
  448. * @return $this
  449. */
  450. public function updatePreferredSeller($flag)
  451. {
  452. $flag = ($flag == 1) ? 1 : 0;
  453. if ($flag) {
  454. $settings = $this->getSettings();
  455. $expirationDate = ($settings['preferred_sellers_expiration'] > 0) ?
  456. new Expr('(now() + interval ' . intval($settings['preferred_sellers_expiration']) . ' day)') : new Expr('null');
  457. }
  458. else {
  459. $expirationDate = new Expr('now()');
  460. }
  461. $this->save(array(
  462. 'preferred_seller' => $flag,
  463. 'preferred_seller_expiration' => $expirationDate,
  464. ));
  465. return $this;
  466. }
  467. /**
  468. *
  469. * updates the user verification related fields (flag and last/next payment dates)
  470. *
  471. * @param int $flag
  472. * @param bool $updateLastPayment if set to true and the flag is enabled, will update the last payment field
  473. * @param null $recurringDays custom recurring days field. if null, will use the value from the settings table
  474. * @param null $refundUser
  475. *
  476. * @return $this
  477. */
  478. public function updateUserVerification($flag, $updateLastPayment = true, $recurringDays = null, $refundUser = null)
  479. {
  480. $flag = ($flag == 1) ? 1 : 0;
  481. $params = array(
  482. 'user_verified' => $flag,
  483. );
  484. if ($flag) {
  485. $settings = $this->getSettings();
  486. $recurringDays = ($recurringDays !== null) ? $recurringDays : $settings['user_verification_recurring'];
  487. $nextPayment = ($recurringDays > 0) ?
  488. new Expr('(greatest(now(), "' . $this->getData('user_verified_next_payment') . '") + interval ' . intval($recurringDays) . ' day)') : new Expr('null');
  489. $refundUser = ($refundUser !== null) ? $refundUser : $settings['user_verification_refund'];
  490. if ($updateLastPayment) {
  491. $params['user_verified_last_payment'] = new Expr('now()');
  492. }
  493. if ($refundUser && $this->userPaymentMode() == 'account') {
  494. $params['balance'] = $this->getData('balance') - $settings['user_verification_fee'];
  495. }
  496. }
  497. else {
  498. $nextPayment = new Expr('now()');
  499. }
  500. $params['user_verified_next_payment'] = $nextPayment;
  501. $this->save($params);
  502. return $this;
  503. }
  504. /**
  505. *
  506. * updates the user's store account and subscription
  507. *
  508. * @param int $flag
  509. * @param bool|null|int $storeSubscriptionId if false, don't update the subscription id, if null set default account, otherwise set subscription id
  510. * @param bool $updateLastPayment
  511. *
  512. * @return $this
  513. */
  514. public function updateStoreSubscription($flag, $storeSubscriptionId = false, $updateLastPayment = true)
  515. {
  516. $flag = ($flag == 1) ? 1 : 0;
  517. $params = array();
  518. $params['store_active'] = $flag;
  519. if ($storeSubscriptionId !== false) {
  520. $params['store_subscription_id'] = ($storeSubscriptionId === null) ?
  521. new Expr('null') : (int)$storeSubscriptionId;
  522. }
  523. $this->save($params);
  524. $params = array();
  525. if ($flag) {
  526. if ($this->getData('store_subscription_id') > 0) {
  527. $storeSubscription = $this->findParentRow('\Ppb\Db\Table\StoresSubscriptions');
  528. $params['store_next_payment'] = ($storeSubscription['recurring_days'] > 0) ?
  529. new Expr('(greatest(now(), "' . $this->getData('store_next_payment') . '") + interval ' . intval($storeSubscription['recurring_days']) . ' day)') : new Expr('null');
  530. }
  531. else {
  532. $params['store_next_payment'] = new Expr('null');
  533. }
  534. if ($updateLastPayment) {
  535. $params['store_last_payment'] = new Expr('now()');
  536. }
  537. if (count($params) > 0) {
  538. $this->save($params);
  539. }
  540. }
  541. return $this;
  542. }
  543. /**
  544. *
  545. * save the user's settings (admin area) and return any output messages
  546. *
  547. * @param array $params
  548. *
  549. * @return array
  550. */
  551. public function updateSettings(array $params)
  552. {
  553. $translate = $this->getTranslate();
  554. $messages = array();
  555. $data = array(
  556. 'account_mode' => $params['account_mode'],
  557. );
  558. if ($params['user_verified'] != $this->getData('user_verified')) {
  559. $this->updateUserVerification($params['user_verified'], false, null, false);
  560. $status = ($params['user_verified'] == 1) ? 'verified' : 'unverified';
  561. $messages[] = sprintf($translate->_("The account has been %s."), $status);
  562. }
  563. if ($params['account_mode'] == 'account') {
  564. $data = array_merge($data, array(
  565. 'balance' => $params['balance'],
  566. 'max_debit' => $params['max_debit'],
  567. ));
  568. if ($params['balance'] != $this->getData('balance')) {
  569. $view = Front::getInstance()->getBootstrap()->getResource('view');
  570. $messages[] = sprintf($translate->_("The user's balance has been changed to: %s %s"),
  571. $view->amount(abs($params['balance'])), (($params['balance'] > 0) ? 'debit' : 'credit'));
  572. $settings = $this->getSettings();
  573. $amount = $params['balance'] - $this->getData('balance');
  574. $name = sprintf($translate->_('Admin Balance Adjustment - User ID: #%s'), $this->getData('id'));
  575. if ($params['balance_adjustment_reason']) {
  576. $name .= $translate->_(' - Comment: ') . $params['balance_adjustment_reason'];
  577. }
  578. // save balance adjustment process in the accounting table
  579. $accountingService = new Service\Accounting();
  580. $accountingService->save(array(
  581. 'name' => $name,
  582. 'amount' => $amount,
  583. 'user_id' => $this->getData('id'),
  584. 'currency' => $settings['currency'],
  585. ));
  586. }
  587. }
  588. if (array_key_exists('is_seller', $params)) {
  589. if ($params['is_seller'] != $this->getData('is_seller')) {
  590. $data['is_seller'] = $params['is_seller'];
  591. $status = ($params['is_seller'] == 1) ? $translate->_('enabled') : $translate->_('disabled');
  592. $messages[] = sprintf($translate->_("The user's listing capabilities have been %s."), $status);
  593. }
  594. }
  595. if (array_key_exists('preferred_seller', $params)) {
  596. if ($params['preferred_seller'] != $this->getData('preferred_seller')) {
  597. $this->updatePreferredSeller($params['preferred_seller']);
  598. $status = ($params['preferred_seller'] == 1) ? $translate->_('enabled') : $translate->_('disabled');
  599. $messages[] = sprintf($translate->_("The preferred seller status has been %s."), $status);
  600. }
  601. }
  602. if (array_key_exists('store_active', $params)) {
  603. if ($params['store_active'] != $this->getData('store_active')) {
  604. $data['store_active'] = $params['store_active'];
  605. $status = ($params['store_active'] == 1) ? $translate->_('enabled') : $translate->_('disabled');
  606. $messages[] = sprintf($translate->_("The store has been %s."), $status);
  607. }
  608. }
  609. if (array_key_exists('assign_default_store_account', $params)) {
  610. if ($params['assign_default_store_account']) {
  611. $data = array_merge($data,
  612. array('store_active' => 1,
  613. 'store_subscription_id' => new Expr('null'),
  614. 'store_next_payment' => new Expr('null'),
  615. ));
  616. $messages[] = $translate->_("The default store account has been set.");
  617. }
  618. }
  619. if (count($data) > 0) {
  620. $this->save($data);
  621. }
  622. return $messages;
  623. }
  624. /**
  625. *
  626. * get the reputation score of the user
  627. * proxy for the \Ppb\Service\Reputation::getScore() method
  628. *
  629. * @return integer
  630. */
  631. public function getReputationScore()
  632. {
  633. $reputationData = \Ppb\Utility::unserialize($this->getData('reputation_data'));
  634. if (isset($reputationData['score'])) {
  635. return $reputationData['score'];
  636. }
  637. return $this->getReputation()->getScore($this->getData('id'));
  638. }
  639. /**
  640. *
  641. * get the positive reputation percentage of the user
  642. * proxy for the \Ppb\Service\Reputation::getPercentage() method
  643. *
  644. * @return string
  645. */
  646. public function getReputationPercentage()
  647. {
  648. $reputationData = \Ppb\Utility::unserialize($this->getData('reputation_data'));
  649. if (isset($reputationData['percentage'])) {
  650. return $reputationData['percentage'];
  651. }
  652. return $this->getReputation()->getPercentage($this->getData('id'));
  653. }
  654. /**
  655. *
  656. * get user postage settings
  657. *
  658. * @param string|null $key
  659. *
  660. * @return array
  661. */
  662. public function getPostageSettings($key = null)
  663. {
  664. $postageSettings = \Ppb\Utility::unserialize($this->getData('postage_settings'), array());
  665. if ($key !== null) {
  666. return isset($postageSettings[$key]) ? $postageSettings[$key] : null;
  667. }
  668. return $postageSettings;
  669. }
  670. /**
  671. *
  672. * get user store settings
  673. *
  674. * @param string|null $key
  675. *
  676. * @return array|string|null
  677. */
  678. public function getStoreSettings($key = null)
  679. {
  680. $storeSettings = \Ppb\Utility::unserialize($this->getData('store_settings'), array());
  681. $storeSettings['store_subscription_id'] = $this->getData('store_subscription_id');
  682. if ($key !== null) {
  683. return isset($storeSettings[$key]) ? $storeSettings[$key] : null;
  684. }
  685. return $storeSettings;
  686. }
  687. /**
  688. *
  689. * get listing setup prefilled fields
  690. * include the user's default address in the prefilled fields array
  691. * if the array is empty, return null
  692. * 7.8: remove any null variables from the array
  693. *
  694. * @return array|null
  695. */
  696. public function getPrefilledFields()
  697. {
  698. $prefilledFields = \Ppb\Utility::unserialize($this->getData('prefilled_fields'), null);
  699. $userAddress = array(
  700. 'country' => $this->getData('country'),
  701. 'state' => $this->getData('state'),
  702. 'address' => $this->getData('zip_code'),
  703. );
  704. $prefilledFields = array_filter(array_merge($userAddress, (array)$prefilledFields));
  705. return (count($prefilledFields) > 0) ? $prefilledFields : null;
  706. }
  707. /**
  708. *
  709. * get user global settings
  710. *
  711. * @param string|null $key
  712. *
  713. * @return array|string|null
  714. */
  715. public function getGlobalSettings($key = null)
  716. {
  717. $globalSettings = \Ppb\Utility::unserialize($this->getData('global_settings'), array());
  718. if ($key !== null) {
  719. return isset($globalSettings[$key]) ? $globalSettings[$key] : null;
  720. }
  721. return $globalSettings;
  722. }
  723. /**
  724. *
  725. * calculate the reputation score of a user based on different input variables
  726. * proxy for the \Ppb\Service\Reputation::calculateScore() method
  727. *
  728. * @param float $score score threshold
  729. * @param string $operand calculation operand
  730. * @param string $reputationType reputation type to be calculated (sale, purchase)
  731. * @param string $interval calculation interval
  732. *
  733. * @return int resulted score
  734. */
  735. public function calculateReputationScore($score = null, $operand = '=', $reputationType = null, $interval = null)
  736. {
  737. return intval($this->getReputation()
  738. ->calculateScore($this->getData('id'), $score, $operand, $reputationType, $interval));
  739. }
  740. /**
  741. *
  742. * check if the user can pay the signup fee
  743. *
  744. * @return bool
  745. */
  746. public function canPaySignupFee()
  747. {
  748. if ($this->getData('payment_status') != 'confirmed') {
  749. return true;
  750. }
  751. return false;
  752. }
  753. /**
  754. *
  755. * check if the user can apply tax to his listings
  756. * return the tax type id if tax can be applied
  757. * seller will choose which tax to apply from the global settings page
  758. *
  759. * @return int|false
  760. */
  761. public function canApplyTax()
  762. {
  763. $settings = $this->getSettings();
  764. if ($settings['enable_tax_listings']) {
  765. return ($this->getGlobalSettings('enable_tax')) ? $this->getGlobalSettings('tax_type') : false;
  766. }
  767. return false;
  768. }
  769. /**
  770. *
  771. * check if the user is a verified user
  772. * - used on the purchase controller for buyer verification
  773. * - used on the listing controller (add/edit actions) for the seller verification
  774. *
  775. * @param bool $default
  776. *
  777. * @throws \RuntimeException
  778. * @return bool
  779. */
  780. public function isVerified($default = true)
  781. {
  782. $settings = $this->getSettings();
  783. if ($settings['user_verification']) {
  784. if ($this->getData('user_verified')) {
  785. return true;
  786. }
  787. return false;
  788. }
  789. return (bool)$default;
  790. }
  791. /**
  792. *
  793. * check if the user has enabled force payment for his products
  794. *
  795. * @return bool
  796. */
  797. public function isForcePayment()
  798. {
  799. $settings = $this->getSettings();
  800. if ($settings['enable_products'] && $settings['enable_force_payment']) {
  801. if ($this->getGlobalSettings('enable_force_payment')) {
  802. return true;
  803. }
  804. }
  805. return false;
  806. }
  807. /**
  808. *
  809. * if the force stores setting is enabled and if the user doesnt have an active store
  810. * and has a number of live items that is higher than the force stores setting, it will return false
  811. *
  812. * @return bool
  813. */
  814. public function isForceStore()
  815. {
  816. $settings = $this->getSettings();
  817. if ($settings['enable_stores'] && $settings['force_stores']) {
  818. if (!$this->storeStatus()) {
  819. if ($this->countListings() >= $settings['force_stores']) {
  820. return false;
  821. }
  822. }
  823. }
  824. return true;
  825. }
  826. /**
  827. *
  828. * check if the user is an administrator
  829. *
  830. * @return bool
  831. */
  832. public function isAdmin()
  833. {
  834. $roles = array_keys(Service\Users::getAdminRoles());
  835. if (in_array($this->getData('role'), $roles)) {
  836. return true;
  837. }
  838. return false;
  839. }
  840. /**
  841. *
  842. * returns an array used by the url view helper to generate the store url
  843. * if the store is disabled, return the main page url
  844. *
  845. * @return array
  846. */
  847. public function storeLink()
  848. {
  849. if ($this->storeStatus(true)) {
  850. $slug = $this->getData('store_slug');
  851. if ($slug) {
  852. return array(
  853. 'module' => 'listings',
  854. 'controller' => 'browse',
  855. 'action' => 'index',
  856. 'show' => 'store',
  857. 'store_slug' => $slug,
  858. );
  859. }
  860. else {
  861. return array(
  862. 'module' => 'listings',
  863. 'controller' => 'browse',
  864. 'action' => 'index',
  865. 'show' => 'store',
  866. 'name' => $this->getData('store_name'),
  867. 'user_id' => $this->getData('id'),
  868. );
  869. }
  870. }
  871. else {
  872. return array(
  873. 'module' => 'app',
  874. 'controller' => 'index',
  875. 'action' => 'index',
  876. );
  877. }
  878. }
  879. /**
  880. *
  881. * user's store name
  882. *
  883. * @return string
  884. */
  885. public function storeName()
  886. {
  887. return ($this->getData('store_name')) ? $this->getData('store_name') : $this->getData('username');
  888. }
  889. /**
  890. *
  891. * returns an array used by the url view helper to generate the user's other items url
  892. *
  893. * @return array
  894. */
  895. public function otherItemsLink()
  896. {
  897. return array(
  898. 'module' => 'listings',
  899. 'controller' => 'browse',
  900. 'action' => 'index',
  901. 'show' => 'other-items',
  902. 'username' => $this->getData('username'),
  903. 'user_id' => $this->getData('id'),
  904. );
  905. }
  906. /**
  907. * returns an array used by the url view helper to generate the user's feedback details url
  908. *
  909. * @return array
  910. */
  911. public function reputationLink()
  912. {
  913. $username = $this->getData('username');
  914. return array(
  915. 'module' => 'members',
  916. 'controller' => 'reputation',
  917. 'action' => 'details',
  918. 'username' => (stristr($username, ' ')) ? $this->getData('id') : $username,
  919. );
  920. }
  921. /**
  922. *
  923. * set the active address for the user
  924. *
  925. * @param int|null|\Ppb\Db\Table\Row\UserAddressBook $address
  926. *
  927. * @return $this
  928. */
  929. public function setAddress($address = null)
  930. {
  931. if (!$address instanceof UserAddressBook) {
  932. $address = $this->getAddress(intval($address));
  933. }
  934. if (count($address) > 0) {
  935. foreach ($address as $key => $value) {
  936. if ($key == 'id') {
  937. $key = 'address_id';
  938. }
  939. $this->addData($key, $value);
  940. }
  941. }
  942. return $this;
  943. }
  944. /**
  945. *
  946. * get a user's address from the address book table
  947. *
  948. * @param int|null $id the id of the address or null if we are looking for the primary address
  949. *
  950. * @return \Ppb\Db\Table\Row\UserAddressBook|null will return an address book row object or null if the user has no address saved
  951. */
  952. public function getAddress($id = null)
  953. {
  954. $select = $this->getTable()->select();
  955. if (!$id) {
  956. $select->where('is_primary = ?', 1);
  957. }
  958. else {
  959. $select->where('id = ?', $id);
  960. }
  961. $rowset = $this->findDependentRowset('\Ppb\Db\Table\UsersAddressBook', null, $select);
  962. $result = $rowset->getRow(0);
  963. // if ($result === null) {
  964. // $addressBookService = new Service\UsersAddressBook();
  965. //
  966. // $result = new UserAddressBook(array(
  967. // 'table' => $addressBookService->getTable(),
  968. // 'data' => array_map(function () {
  969. // }, array_flip($addressBookService->getAddressFields()))
  970. // ));
  971. // }
  972. return $result;
  973. }
  974. /**
  975. *
  976. * check whether to display make offer ranges
  977. *
  978. * @return bool
  979. */
  980. public function displayMakeOfferRanges()
  981. {
  982. $settings = $this->getSettings();
  983. if ($settings['show_make_offer_ranges'] && $this->getGlobalSettings('show_make_offer_ranges')) {
  984. return true;
  985. }
  986. return false;
  987. }
  988. /**
  989. *
  990. * check if a user has added this store owner as favorite
  991. *
  992. * @param int $userId
  993. *
  994. * @return bool
  995. */
  996. public function isFavoriteStore($userId)
  997. {
  998. return (count($this->_getFavoriteStores($userId))) ? true : false;
  999. }
  1000. /**
  1001. *
  1002. * add/remove this user store to favorites for a certain user
  1003. *
  1004. * @param int $userId
  1005. *
  1006. * @return $this
  1007. */
  1008. public function processFavoriteStore($userId)
  1009. {
  1010. if ($this->isFavoriteStore($userId)) {
  1011. $this->_getFavoriteStores($userId)->delete();
  1012. }
  1013. else {
  1014. $favoriteStoresService = new Service\FavoriteStores();
  1015. $favoriteStoresService->save(array(
  1016. 'user_id' => $userId,
  1017. 'store_id' => $this->getData('id'),
  1018. ));
  1019. }
  1020. return $this;
  1021. }
  1022. /**
  1023. *
  1024. * checks if the user is in vacation
  1025. *
  1026. * @return bool
  1027. */
  1028. public function isVacation()
  1029. {
  1030. if ($this->getGlobalSettings('vacation_mode')) {
  1031. $returnDate = $this->getGlobalSettings('vacation_mode_return_date');
  1032. if (empty($returnDate) || (strtotime($returnDate) > time())) {
  1033. return true;
  1034. }
  1035. }
  1036. return false;
  1037. }
  1038. /**
  1039. *
  1040. * check if the user has selected to receive seller specific email notifications
  1041. *
  1042. * @return bool
  1043. */
  1044. public function emailSellerNotifications()
  1045. {
  1046. return (
  1047. $this->getGlobalSettings('disable_emails') ||
  1048. $this->getGlobalSettings('disable_seller_notifications')
  1049. ) ? false : true;
  1050. }
  1051. /**
  1052. *
  1053. * check if the user has selected to receive offers module specific email notifications
  1054. *
  1055. * @return bool
  1056. */
  1057. public function emailOffersNotifications()
  1058. {
  1059. return (
  1060. $this->getGlobalSettings('disable_emails') ||
  1061. $this->getGlobalSettings('disable_offers_notifications')
  1062. ) ? false : true;
  1063. }
  1064. /**
  1065. *
  1066. * check if the user has selected to receive messaging module specific email notifications
  1067. *
  1068. * @return bool
  1069. */
  1070. public function emailMessagingNotifications()
  1071. {
  1072. return (
  1073. $this->getGlobalSettings('disable_emails') ||
  1074. $this->getGlobalSettings('disable_messaging_notifications')
  1075. ) ? false : true;
  1076. }
  1077. /**
  1078. *
  1079. * get favorite stores rowset for a certain user
  1080. *
  1081. * @param int $userId
  1082. *
  1083. * @return \Cube\Db\Table\Rowset
  1084. */
  1085. protected function _getFavoriteStores($userId = null)
  1086. {
  1087. $select = null;
  1088. if ($userId !== null) {
  1089. $select = $this->getTable()->select()
  1090. ->where('user_id = ?', $userId);
  1091. }
  1092. return $this->findDependentRowset('\Ppb\Db\Table\FavoriteStores', 'Store', $select);
  1093. }
  1094. /**
  1095. *
  1096. * return sluggized store name value
  1097. * uses the cleanString method from the Url view helper
  1098. *
  1099. * @param string $storeName
  1100. *
  1101. * @return string
  1102. */
  1103. protected function _sluggizeStoreName($storeName)
  1104. {
  1105. $usersService = new Service\Users();
  1106. $duplicate = true;
  1107. do {
  1108. $storeSlug = UrlViewHelper::cleanString($storeName);
  1109. $rowset = $usersService->fetchAll(
  1110. $usersService->getTable()->select()
  1111. ->where('store_slug = ?', $storeSlug)
  1112. ->where('id != ?', $this->getData('id'))
  1113. );
  1114. if (count($rowset) > 0) {
  1115. $storeName .= '1';
  1116. }
  1117. else {
  1118. $duplicate = false;
  1119. }
  1120. } while ($duplicate === true);
  1121. return $storeSlug;
  1122. }
  1123. }