* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2 * @category Stud.IP * * @property int $id alias column for background_id * @property int $background_id database column * @property string $filename database column * @property int $mobile database column * @property int $desktop database column * @property int $in_release database column * @property int|null $mkdate database column * @property int|null $chdate database column */ class LoginBackground extends SimpleORMap { /** * Configures this model. * * @param Array $config */ protected static function configure($config = []) { $config['db_table'] = 'loginbackgrounds'; $config['registered_callbacks']['after_delete'][] = function ($pic) { if (file_exists($pic->getPath())) { unlink($pic->getPath()); } }; parent::configure($config); } /** * @return string The full URL to this picture. */ public function getURL() { return URLHelper::getURL( $GLOBALS['DYNAMIC_CONTENT_URL'] . self::getRelativePath() . "/{$this->id}.{$this->getExtension()}", null, true ); } /** * @return string the full file system path to this picture. */ public function getPath() { return self::getPictureDirectory() . "/{$this->id}.{$this->getExtension()}"; } /** * @return int The file size in bytes. */ public function getFilesize() { if (file_exists($this->getPath())) { return filesize($this->getPath()); } return false; } /** * @return int The picture dimensions as provided by getimagesize. */ public function getDimensions() { if (file_exists($this->getPath())) { return getimagesize($this->getPath()); } return false; } /** * @return string The picture's extension */ public function getExtension() { return pathinfo($this->filename, PATHINFO_EXTENSION); } /** * Provides a random picture for the given view. * @param string $view one of 'desktop', 'mobile'. * @return LoginBackground One of the available pictures. */ public static function getRandomPicture($view = 'desktop') { if (!in_array($view, ['desktop', 'mobile'])) { throw new Exception('Unknown view mode'); } $pic = self::findOneBySQL("{$view} = 1 ORDER BY RAND() LIMIT 1"); return $pic; } /** * @return string The relative path to the Stud.IP web root. */ public static function getRelativePath() { return '/loginbackgrounds'; } /** * @return The directory where all available background pictures live. */ public static function getPictureDirectory() { return $GLOBALS['DYNAMIC_CONTENT_PATH'] . self::getRelativePath(); } }