1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
<?php
/**
* TfPreferenceHandler class file.
*
* @copyright Simon Wilkinson 2013+ (https://tuskfish.biz)
* @license https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html GNU General Public License (GPL) V2
* @author Simon Wilkinson <[email protected]>
* @version Release: 1.0
* @since 1.1
* @package core
*/
// Enable strict type declaration.
declare(strict_types=1);
if (!defined("TFISH_ROOT_PATH")) die("TFISH_ERROR_ROOT_PATH_NOT_DEFINED");
/**
* Read and write site preferences to the database.
*
* @copyright Simon Wilkinson 2013+ (https://tuskfish.biz)
* @license https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html GNU General Public License (GPL) V2
* @author Simon Wilkinson <[email protected]>
* @version Release: 1.0
* @since 1.1
* @package core
* @var TfDatabase $db Instance of the Tuskfish database class.
*/
class TfPreferenceHandler
{
protected $db;
/**
* Constructor.
*
* @param TfDatabase $db Instance of the database class.
*/
public function __construct(TfDatabase $db)
{
if (is_a($db, 'TfDatabase')) {
$this->db = $db;
} else {
trigger_error(TFISH_ERROR_NOT_OBJECT, E_USER_ERROR);
}
}
/**
* Read out the site preferences into an array.
*
* @return array Array of site preferences.
*/
public function readPreferencesFromDatabase()
{
$preferences = array();
$result = $this->db->select('preference');
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$preferences[$row['title']] = $row['value'];
}
return $preferences;
}
/**
* Updates the site preferences in the database.
*
* @param TfPreference $preference Instance of the Tuskfish site preference class.
* @return bool True on success false on failure.
*/
public function writePreferences(TfPreference $preference)
{
// Convert preference object to array of key => values.
$keyValues = $preference->getPreferencesAsArray();
// Unset the validator object as it is not stored in the database.
unset($keyValues['validator']);
foreach ($keyValues as $key => $value) {
$sql = "UPDATE `preference` SET `value` = :value WHERE `title` = :title";
$statement = $this->db->preparedStatement($sql);
$statement->bindValue(':title', $key, $this->db->setType($key));
$statement->bindValue(':value', $value, $this->db->setType($value));
unset($sql, $key, $value);
$result = $this->db->executeTransaction($statement);
if (!$result) {
trigger_error(TFISH_ERROR_INSERTION_FAILED, E_USER_ERROR);
return false;
}
}
return true;
}
}