Settings.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ DbD+d11pce1LnvcthMF6w3zJUcR0Zr/YRgzoMUWOl/M=
  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.0
  11. */
  12. /**
  13. * settings table service class
  14. */
  15. namespace Ppb\Service;
  16. use Ppb\Db\Table\Settings as SettingsTable,
  17. Cube\Controller\Front;
  18. class Settings extends AbstractService
  19. {
  20. /**
  21. *
  22. * class constructor
  23. */
  24. public function __construct()
  25. {
  26. parent::__construct();
  27. $this->setTable(
  28. new SettingsTable());
  29. }
  30. /**
  31. *
  32. * save data in the settings table
  33. *
  34. * @param array $data
  35. * @return \Ppb\Service\Settings
  36. */
  37. public function save(array $data)
  38. {
  39. foreach ($data as $key => $value) {
  40. $row = $this->_table->fetchRow("name='{$key}'");
  41. if (is_array($value)) {
  42. $value = serialize($value);
  43. }
  44. if (count($row) > 0) {
  45. $this->_table->update(array('value' => $value), "name='{$key}'");
  46. }
  47. else {
  48. $this->_table->insert(array('name' => $key, 'value' => $value));
  49. }
  50. }
  51. return $this;
  52. }
  53. /**
  54. *
  55. * get one or all settings table keys
  56. *
  57. * @param string $key
  58. * @param bool $force whether to force the sql query or try to fetch the settings array from the front controller
  59. * @return array
  60. */
  61. public function get($key = null, $force = false)
  62. {
  63. $rows = array();
  64. $data = array();
  65. if ($key !== null) {
  66. $rows = array($this->_table->fetchRow("name='{$key}'"));
  67. }
  68. else {
  69. if ($force === false) {
  70. $rows = Front::getInstance()->getBootstrap()->getResource('settings');
  71. }
  72. if (empty($rows)) {
  73. $rows = $this->_table->fetchAll(
  74. $this->_table->select(array('name', 'value')));
  75. }
  76. else {
  77. return $rows;
  78. }
  79. }
  80. foreach ($rows as $row) {
  81. $data[(string)$row['name']] = $row['value'];
  82. }
  83. return $data;
  84. }
  85. }