* @link https://TheTempusProject.com * @license https://opensource.org/licenses/MIT [MIT LICENSE] */ namespace TheTempusProject\Classes; use TheTempusProject\Houdini\Classes\Issues; use TheTempusProject\Houdini\Classes\Forms; use TheTempusProject\Canary\Canary as Debug; use TheTempusProject\Bedrock\Functions\Check; use TheTempusProject\Bedrock\Functions\Upload; use TheTempusProject\Bedrock\Functions\Input; use TheTempusProject\TheTempusProject as App; class Preferences { public static $preferences = false; private static $location = false; private static $initialized = false; /** * Default constructor which will attempt to load the preferences from the location specified. * * @param {string} [$location] * @return {null|object} */ public function __construct( $location = '' ) { if ( self::$initialized !== false ) { Debug::log( 'Preferences already initialized.' ); return $this; } if ( empty( $location ) ) { $location = PREFERENCES_JSON; } self::$initialized = $this->load( $location ); if ( self::$initialized !== false ) { Debug::log( 'Preferences initialization succeeded.' ); return $this; } Debug::warn( 'Preferences initialization failed.' ); } /** * Attempts to retrieve then set the preferences from a file. * @note This function will reset the preferences every time it is used. * * @param {string} [$location] * @return {bool} */ public function load( $location ) { self::$preferences = $this->getPrefsFile( $location ); self::$location = $location; if ( self::$preferences === false || empty( self::$preferences ) ) { Debug::warn( 'Preferences load failed.' ); return false; } Debug::log( 'Preferences load succeeded.' ); return true; } /** * Opens and decodes the preferences json from the location provided. * * @param {string} [$location] * @return {bool|array} */ public function getPrefsFile( $location ) { if ( file_exists( $location ) ) { Debug::debug( "Preferences json found: $location" ); return json_decode( file_get_contents( $location ), true ); } else { Debug::warn( "Preferences json not found: $location" ); return false; } } /** * Retrieves the preference option for $name. * * @param {string} [$name] * @return {WILD} */ public static function get( $name ) { if ( self::$preferences === false ) { return Debug::warn( 'Preferences not loaded.' ); } if ( isset( self::$preferences[$name] ) ) { return self::$preferences[$name]; } return Debug::warn( "Preference not found: $name" ); } public function getType( $name ) { $pref = $this->get( $name ); if ( empty( $pref ) || empty( $pref['type'] ) ) { return Debug::warn( "Preference Type not found: $name" ); } return $pref['type']; } public function getDefault( $name ) { $pref = $this->get( $name ); if ( empty( $pref ) || empty( $pref['default'] ) ) { return Debug::warn( "Preference Default not found: $name" ); } return $pref['default']; } public function getOptions( $name ) { $pref = $this->get( $name ); if ( empty( $pref ) || empty( $pref['options'] ) ) { return Debug::warn( "Preference Options not found: $name" ); } return $pref['options']; } public function getPrettyName( $name ) { $pref = $this->get( $name ); if ( empty( $pref ) || empty( $pref['pretty'] ) ) { return Debug::warn( "Preference Pretty Name not found: $name" ); } return $pref['pretty']; } /** * Saves the current preferences. * * @param {bool} [$default] - Whether or not to save a default copy. * @return {bool} */ public function save( $backup = true ) { if ( self::$preferences === false ) { Debug::warn( 'Preferences not loaded.' ); return false; } if ( self::$location === false ) { Debug::warn( 'Preferences location not set.' ); return false; } if ( $backup ) { $locationArray = explode( '.', self::$location ); $locationArray[] = 'bak'; $backupLoction = implode( '.', $locationArray ); if ( !file_put_contents( $backupLoction, json_encode( self::$preferences ) ) ) { return false; } } if ( file_put_contents( self::$location, json_encode( self::$preferences ) ) ) { return true; } return false; } /** * Adds a new preference to the $preferences array. * * @param {string} [$name] * @param {string} [$value] * @return {bool} */ public function add( $name, $details ) { if ( !Check::simpleName( $name ) ) { Debug::error( "Preference name invalid: $name" ); return false; } if ( isset( self::$preferences[$name] ) ) { Debug::warn( "Preference already exists: $name" ); return false; } if ( self::$preferences === false ) { self::$preferences = []; } $prefsArray = $this->normalizePreferenceArray( $name, $details ); if ( false === $prefsArray ) { Debug::warn( 'Preference array failed to load properly.' ); return false; } self::$preferences[$name] = $prefsArray; return true; } public function getFormHtml( $populated = [] ) { $form = ''; foreach ( self::$preferences as $name => $details ) { $tempPrefsArray = $this->normalizePreferenceArray( $name, $details ); if ( isset( $populated[ $name ] ) ) { $tempPrefsArray['default'] = $populated[$name]; } $form .= Forms::getFormFieldHtml( $name, $tempPrefsArray['pretty'], $tempPrefsArray['type'], $tempPrefsArray['default'], $tempPrefsArray['options'] ); } return $form; } public function convertFormToArray( $fillMissing = true, $defaultsOnly = true ) { $prefsArray = []; foreach ( self::$preferences as $name => $details ) { if ( true === $fillMissing ) { if ( true !== $defaultsOnly && !empty( App::$activePrefs[$name] ) ) { $prefsArray[$name] = App::$activePrefs[$name]; } else { $prefsArray[$name] = $details['default']; } } if ( Input::exists( $name ) ) { $prefsArray[$name] = Input::post( $name ); } if ( 'file' == $details['type'] ) { if ( Input::exists( $name ) ) { $folder = IMAGE_UPLOAD_DIRECTORY . App::$activeUser->username . DIRECTORY_SEPARATOR; if ( !Upload::image( $name, $folder ) ) { Issues::add( 'error', [ 'There was an error with your upload.' => Check::systemErrors() ] ); } else { $route = str_replace( APP_ROOT_DIRECTORY, '', $folder ); $prefsArray[$name] = $route . Upload::last(); } } } if ( 'checkbox' == $details['type'] ) { if ( Input::exists( $name ) ) { $prefsArray[$name] = true; } else { $prefsArray[$name] = false; } } } return $prefsArray; } public function getDefaultPreferencesArray() { if ( self::$preferences === false ) { Debug::warn( 'Preferences not loaded.' ); return false; } $prefsArray = []; foreach ( self::$preferences as $name => $details ) { $tempPrefsArray = $this->normalizePreferenceArray( $name, $details ); $prefsArray[$name] = $tempPrefsArray['default']; } return $prefsArray; } public function normalizePreferenceArray( $name, $prefsArray ) { if ( !is_array( $prefsArray ) ) { Debug::warn( 'Preference array was not an array.' ); return false; } if ( !isset( $prefsArray['type'] ) ) { if ( isset( $prefsArray['options'] ) ) { $prefsArray['type'] = 'select'; } else { $prefsArray['type'] = 'text'; } } if ( !isset( $prefsArray['pretty'] ) ) { $prefsArray['pretty'] = ucfirst( $name ); } if ( !isset( $prefsArray['default'] ) ) { $prefsArray['default'] = ''; } if ( ( empty( $prefsArray['avatar'] ) ) || ( $prefsArray['avatar'] == 'defaultAvatar.png' ) ) { $prefsArray['avatar'] = IMAGE_DIRECTORY . 'defaultAvatar.png'; } if ( !isset( $prefsArray['options'] ) ) { $prefsArray['options'] = null; } return $prefsArray; } /** * Removes an existing preference from the $preferences array. * * @param {string} [$name] * @param {bool} [$save] * @return {bool} */ public function remove( $name, $save = false ) { if ( self::$preferences === false ) { Debug::warn( 'Preferences not loaded.' ); return false; } if ( !isset( self::$preferences[$name] ) ) { Debug::error( "Preference does not exist: $name" ); return false; } unset( self::$preferences[$name] ); if ( $save === true ) { return $this->save( true ); } return true; } }