* @copyright 2012 Stud.IP Core-Group * @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2 * @category Stud.IP * @package admin * @since 2.4 * * @property string $id alias column for ad_id * @property string $ad_id database column * @property string $banner_path database column * @property string|null $description database column * @property string|null $alttext database column * @property string $target_type database column * @property string $target database column * @property int $startdate database column * @property int $enddate database column * @property int $priority database column * @property int $views database column * @property int $clicks database column * @property int $mkdate database column * @property int $chdate database column * @property SimpleORMapCollection $banner_roles has_many BannerRoles */ class Banner extends SimpleORMap { protected static function configure($config = []) { $config['db_table'] = 'banner_ads'; $config['has_many']['banner_roles'] = [ 'class_name' => BannerRoles::class, 'assoc_foreign_key' => 'ad_id', 'on_delete' => 'delete' ]; parent::configure($config); } /** * Returns a random banner */ public static function getRandomBanner() { $query = "SELECT ad_id, priority, startdate, enddate FROM banner_ads WHERE priority > 0 AND (startdate = 0 OR startdate < UNIX_TIMESTAMP()) AND (enddate = 0 OR enddate > UNIX_TIMESTAMP())"; $statement = DBManager::get()->query($query); // array that contains banner ids and an offset // offsets start with 0 and increase by pow(2, priority) // a random number between 0 and sum(pow(2,priorities)) is // drawn and the banner with the highest offset smaller than // this number is chosen $banners = []; $sum = 0; // collect banners to consider, build banners array while ($row = $statement->fetch(PDO::FETCH_ASSOC)) { if (BannerRoles::checkUserAccess($row['ad_id'])) { $sum += pow(2, $row['priority']); $banners[] = [ 'ad_id' => $row['ad_id'], 'offset' => $sum ]; } } // draw random number and select banner $x = mt_rand(0, $sum); $ad_id = false; foreach ($banners as $i) { if ($i['offset'] >= $x) { $ad_id = $i['ad_id']; break; } } return new Banner($ad_id); } /** * Get all banners * * @return array() list of banners */ public static function getAllBanners() { $query = "SELECT ad_id FROM banner_ads ORDER BY priority DESC"; $statement = DBManager::get()->query($query); $ids = $statement->fetchAll(PDO::FETCH_COLUMN); $banners = []; foreach ($ids as $id) { $banners[$id] = new Banner($id); } return $banners; } /** * delete entry from database * the object is cleared and turned to new state * @return boolean */ public function delete() { if (!$this->isNew()) { // Remove banner file unlink($GLOBALS['DYNAMIC_CONTENT_PATH'] . '/banner/' . $this->banner_path); } return parent::delete(); } /* * Check the priority for a banner * @param Int $prio priority (1-10) * @return */ public function getViewProbability() { static $computed = false, $sum = null; if ($this->priority == 0) { return '--'; } if ($computed === false) { $sum = DBManager::get()->query("SELECT SUM(POW(2, priority)) FROM banner_ads WHERE priority > 0") ->fetchColumn(); $computed = true; } // return '1/' . (1 / (pow(2, $prio) / $sum)); return number_format(100 / (1 / (pow(2, $this->priority) / $sum)), 2, ',', '.') . '%'; } /** * Returns the appropriate link for this banner. * * @return string */ public function getLink($internal = false) { if ($this->isNew()) { return ''; } if ($internal) { return URLHelper::getLink('dispatch.php/banner/click/' . $this->ad_id); } if ($this->target_type === 'url') { return $this->target; } if ($this->target_type === 'seminar') { return URLHelper::getLink('dispatch.php/course/details/', ['sem_id' => $this->target]); } if ($this->target_type === 'user') { return URLHelper::getLink('dispatch.php/profile', ['username' => $this->target]); } if ($this->target_type === 'inst') { return URLHelper::getLink( 'dispatch.php/institute/overview', ['auswahl' => $this->target] ); } return ''; } /** * Returns the img-tag for this banner * * @return string */ public function toImg($attributes = []) { $attr = [ 'src' => $GLOBALS['DYNAMIC_CONTENT_URL'] . '/banner/' . $this->banner_path, 'border' => '0', ]; if ($this->alttext) { $attr['title'] = $attr['alt'] = $this->alttext; } $attr = array_merge($attr, $attributes); $attr_string = ''; foreach ($attr as $key => $value) { $attr_string .= sprintf(' %s="%s"', $key, htmlReady($value)); } return ''; } /** * Returns the complete html (link + img) for this banner * * @return string */ public function toHTML($internal = true) { if ($this->isNew()) { return ''; } if ($this->target_type === 'url') { $template = '%s'; } elseif ($this->target_type === 'none') { $template = '%2$s'; } else { $template = '%s'; } $link = sprintf($template, $this->getLink($internal), $this->toImg()); $this->views += 1; $this->store(); return sprintf('
%s
', $link); } }