UsersStatistics.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ 1sSMNsMnbK9nBM1T7DpWzpZ/DXZZAkNVkJZzxT0kyRQ=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2014 Online Ventures Software LTD & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.1
  11. */
  12. /**
  13. * users statistics table service class
  14. */
  15. namespace Ppb\Service;
  16. use Ppb\Db\Table\UsersStatistics as UsersStatisticsTable,
  17. Ppb\Model\Elements,
  18. Cube\Db\Expr;
  19. class UsersStatistics extends AbstractService
  20. {
  21. /**
  22. *
  23. * class constructor
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. $this->setTable(
  29. new UsersStatisticsTable());
  30. }
  31. /**
  32. *
  33. * create or update a stat row
  34. * if a row that matches the ip, user agent and accept language exists in the table, update the row
  35. * otherwise insert a new row
  36. *
  37. * @param array $data
  38. *
  39. * @return $this
  40. */
  41. public function save($data)
  42. {
  43. $row = null;
  44. $data = $this->_prepareSaveData($data);
  45. $select = $this->_table->select()
  46. ->where('remote_addr = ?', $data['remote_addr'])
  47. ->where('http_user_agent = ?', $data['http_user_agent']);
  48. $row = $this->_table->fetchRow($select);
  49. $data['updated_at'] = new Expr('now()');
  50. if (count($row) > 0) {
  51. if (isset($data['http_referrer'])) {
  52. unset($data['http_referrer']);
  53. }
  54. $this->_table->update($data, "id='{$row['id']}'");
  55. }
  56. else {
  57. $data['created_at'] = new Expr('now()');
  58. $this->_table->insert($data);
  59. }
  60. return $this;
  61. }
  62. }