Version 4.5.0
This commit is contained in:
1 parent
1a0c7fd3c2
commit
f79dcf067a
3791 files changed
+248032
-76372
No files matched your search
@@ -11,7 +11,7 @@
|
||||
namespace IPS\core;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
@@ -22,6 +22,21 @@ if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
*/
|
||||
class _Advertisement extends \IPS\Patterns\ActiveRecord
|
||||
{
|
||||
/**
|
||||
* @brief HTML ad
|
||||
*/
|
||||
const AD_HTML = 1;
|
||||
|
||||
/**
|
||||
* @brief Images ad
|
||||
*/
|
||||
const AD_IMAGES = 2;
|
||||
|
||||
/**
|
||||
* @brief Email ad
|
||||
*/
|
||||
const AD_EMAIL = 3;
|
||||
|
||||
/**
|
||||
* @brief Database Table
|
||||
*/
|
||||
@@ -43,11 +58,22 @@ class _Advertisement extends \IPS\Patterns\ActiveRecord
|
||||
*/
|
||||
public static $advertisementIds = array();
|
||||
|
||||
/**
|
||||
* @brief Advertisements sent via email (used to update impression count)
|
||||
* @see static::updateEmailImpressions()
|
||||
*/
|
||||
public static $advertisementIdsEmail = array();
|
||||
|
||||
/**
|
||||
* @brief Stored advertisements we can display on this page
|
||||
*/
|
||||
protected static $advertisements = NULL;
|
||||
|
||||
/**
|
||||
* @brief Stored advertisements we can send in emails
|
||||
*/
|
||||
protected static $emailAdvertisements = NULL;
|
||||
|
||||
/**
|
||||
* Fetch advertisements and return the appropriate one to display
|
||||
*
|
||||
@@ -67,7 +93,17 @@ class _Advertisement extends \IPS\Patterns\ActiveRecord
|
||||
{
|
||||
static::$advertisements = array();
|
||||
|
||||
foreach( \IPS\Db::i()->select( '*' ,'core_advertisements', array( "ad_active=1 AND ad_start < ? AND ( ad_end=0 OR ad_end > ? )", time(), time() ) ) as $row )
|
||||
$where[] = array( "ad_type!=?", static::AD_EMAIL );
|
||||
$where[] = array( "ad_active=1" );
|
||||
$where[] = array( "ad_start<?", time() );
|
||||
$where[] = array( "(ad_end=0 OR ad_end>?)", time() );
|
||||
|
||||
if( !isset( \IPS\Dispatcher::i()->dispatcherController ) OR !\IPS\Dispatcher::i()->dispatcherController->isContentPage )
|
||||
{
|
||||
$where[] =array( 'ad_nocontent_page_output=?', 1 );
|
||||
}
|
||||
|
||||
foreach( \IPS\Db::i()->select( '*' ,'core_advertisements', $where ) as $row )
|
||||
{
|
||||
foreach ( explode( ',', $row['ad_location'] ) as $_location )
|
||||
{
|
||||
@@ -81,7 +117,7 @@ class _Advertisement extends \IPS\Patterns\ActiveRecord
|
||||
{
|
||||
foreach( $ads as $index => $ad )
|
||||
{
|
||||
if ( $ad->exempt != '*' )
|
||||
if ( ! empty( $ad->exempt ) and $ad->exempt != '*' )
|
||||
{
|
||||
$groupsToHideFrom = array_diff( array_keys(\IPS\Member\Group::groups()), json_decode( $ad->exempt, TRUE ) );
|
||||
|
||||
@@ -95,18 +131,114 @@ class _Advertisement extends \IPS\Patterns\ActiveRecord
|
||||
}
|
||||
|
||||
/* No advertisements? Just return then */
|
||||
if( !count( static::$advertisements ) OR !isset( static::$advertisements[ $location ] ) OR !count( static::$advertisements[ $location ] ) )
|
||||
if( !\count( static::$advertisements ) OR !isset( static::$advertisements[ $location ] ) OR !\count( static::$advertisements[ $location ] ) )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Reset so doesn't throw error */
|
||||
static::$advertisements[ $location ] = array_values( static::$advertisements[ $location ] );
|
||||
return static::selectAdvertisement( static::$advertisements[ $location ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch advertisements for emails and return the appropriate one to display
|
||||
*
|
||||
* @param array|null $container The container that spawned the email, or NULL
|
||||
* @return \IPS\core\Advertisement|NULL
|
||||
*/
|
||||
public static function loadForEmail( $container=NULL )
|
||||
{
|
||||
/* If we know there are no ads, we don't need to bother */
|
||||
if ( !\IPS\Settings::i()->ads_exist )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Fetch our advertisements, if we haven't already done so */
|
||||
if( static::$emailAdvertisements === NULL )
|
||||
{
|
||||
static::$emailAdvertisements = array();
|
||||
|
||||
foreach( \IPS\Db::i()->select( '*' ,'core_advertisements', array( "ad_type=? AND ad_active=1 AND ad_start < ? AND ( ad_end=0 OR ad_end > ? )", static::AD_EMAIL, time(), time() ) ) as $row )
|
||||
{
|
||||
foreach ( explode( ',', $row['ad_location'] ) as $_location )
|
||||
{
|
||||
static::$emailAdvertisements[] = static::constructFromData( $row );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Whittle down the advertisements to use based on container limitations */
|
||||
$adsToCheckFrom = array();
|
||||
|
||||
/* First see if we have any for this specific configuration */
|
||||
if( $container !== NULL )
|
||||
{
|
||||
foreach( static::$emailAdvertisements as $advertisement )
|
||||
{
|
||||
if( isset( $advertisement->_additional_settings['email_container'] ) AND isset( $advertisement->_additional_settings['email_node'] ) )
|
||||
{
|
||||
if( $advertisement->_additional_settings['email_container'] == $container['className'] AND $advertisement->_additional_settings['email_node'] == $container['id'] )
|
||||
{
|
||||
$adsToCheckFrom[] = $advertisement;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* If we didn't find any, then look for generic ones for the node class */
|
||||
if( $container !== NULL )
|
||||
{
|
||||
if( !\count( $adsToCheckFrom ) )
|
||||
{
|
||||
foreach( static::$emailAdvertisements as $advertisement )
|
||||
{
|
||||
if( isset( $advertisement->_additional_settings['email_container'] ) AND ( !isset( $advertisement->_additional_settings['email_node'] ) OR !$advertisement->_additional_settings['email_node'] ) )
|
||||
{
|
||||
if( $advertisement->_additional_settings['email_container'] == $container['className'] )
|
||||
{
|
||||
$adsToCheckFrom[] = $advertisement;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* If we still don't have any, look for generic ones allowed in all emails */
|
||||
if( !\count( $adsToCheckFrom ) )
|
||||
{
|
||||
foreach( static::$emailAdvertisements as $advertisement )
|
||||
{
|
||||
if( !isset( $advertisement->_additional_settings['email_container'] ) OR $advertisement->_additional_settings['email_container'] == '*' )
|
||||
{
|
||||
$adsToCheckFrom[] = $advertisement;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* No advertisements? Just return then */
|
||||
if( !\count( $adsToCheckFrom ) )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return static::selectAdvertisement( $adsToCheckFrom );
|
||||
}
|
||||
|
||||
/**
|
||||
* Select an advertisement from an array and return it
|
||||
*
|
||||
* @param array $ads Array of advertisements to select from
|
||||
* @return static
|
||||
*/
|
||||
static protected function selectAdvertisement( $ads )
|
||||
{
|
||||
/* Reset so we don't throw an error */
|
||||
$ads = array_values( $ads );
|
||||
|
||||
/* If we only have one, that is the one we will show */
|
||||
if( count( static::$advertisements[ $location ] ) === 1 )
|
||||
if( \count( $ads ) === 1 )
|
||||
{
|
||||
$advertisement = static::$advertisements[ $location ][0];
|
||||
$advertisement = $ads[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -114,27 +246,27 @@ class _Advertisement extends \IPS\Patterns\ActiveRecord
|
||||
switch( \IPS\Settings::i()->ads_circulation )
|
||||
{
|
||||
case 'random':
|
||||
$advertisement = static::$advertisements[ $location ][ array_rand( static::$advertisements[ $location ] ) ];
|
||||
$advertisement = $ads[ array_rand( $ads ) ];
|
||||
break;
|
||||
|
||||
case 'newest':
|
||||
usort( static::$advertisements[ $location ], function( $a, $b ){
|
||||
usort( $ads, function( $a, $b ){
|
||||
return strcmp( $a->start, $b->start );
|
||||
} );
|
||||
|
||||
$advertisement = static::$advertisements[ $location ][0];
|
||||
$advertisement = $ads[0];
|
||||
break;
|
||||
|
||||
case 'oldest':
|
||||
usort( static::$advertisements[ $location ], function( $a, $b ){
|
||||
usort( $ads, function( $a, $b ){
|
||||
return strcmp( $b->start, $a->start );
|
||||
} );
|
||||
|
||||
$advertisement = static::$advertisements[ $location ][0];
|
||||
$advertisement = $ads[0];
|
||||
break;
|
||||
|
||||
case 'least':
|
||||
usort( static::$advertisements[ $location ], function( $a, $b ){
|
||||
usort( $ads, function( $a, $b ){
|
||||
if ( $a->impressions == $b->impressions )
|
||||
{
|
||||
return 0;
|
||||
@@ -143,7 +275,7 @@ class _Advertisement extends \IPS\Patterns\ActiveRecord
|
||||
return ( $a->impressions < $b->impressions ) ? -1 : 1;
|
||||
} );
|
||||
|
||||
$advertisement = static::$advertisements[ $location ][0];
|
||||
$advertisement = $ads[0];
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -154,12 +286,14 @@ class _Advertisement extends \IPS\Patterns\ActiveRecord
|
||||
/**
|
||||
* Convert the advertisement to an HTML string
|
||||
*
|
||||
* @param string $emailType html or plaintext email advertisement
|
||||
* @param \IPS\Email|NULL $email For an email advertisement, this will be the email object, otherwise NULL
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
public function toString( $emailType='html', $email=NULL )
|
||||
{
|
||||
/* Showing HTML or an image? */
|
||||
if( $this->html )
|
||||
if( $this->type == static::AD_HTML )
|
||||
{
|
||||
if( \IPS\Request::i()->isSecure() AND $this->html_https_set )
|
||||
{
|
||||
@@ -170,10 +304,14 @@ class _Advertisement extends \IPS\Patterns\ActiveRecord
|
||||
$result = $this->html;
|
||||
}
|
||||
}
|
||||
else
|
||||
elseif( $this->type == static::AD_IMAGES )
|
||||
{
|
||||
$result = \IPS\Theme::i()->getTemplate( 'global', 'core', 'global' )->advertisementImage( $this );
|
||||
}
|
||||
elseif( $this->type == static::AD_EMAIL )
|
||||
{
|
||||
$result = \IPS\Email::template( 'core', 'advertisement', $emailType, array( $this, $email ) );
|
||||
}
|
||||
|
||||
/* Did we just hit the maximum impression count? If so, disable and then clear the cache so it will rebuild next time. */
|
||||
if( $this->maximum_unit == 'i' AND $this->maximum_value > -1 AND $this->impressions + 1 >= $this->maximum_value )
|
||||
@@ -188,11 +326,28 @@ class _Advertisement extends \IPS\Patterns\ActiveRecord
|
||||
}
|
||||
|
||||
/* Store the id so we can update impression count and return the ad */
|
||||
static::$advertisementIds[] = $this->id;
|
||||
if( $this->type == static::AD_EMAIL )
|
||||
{
|
||||
static::$advertisementIdsEmail[] = $this->id;
|
||||
}
|
||||
else
|
||||
{
|
||||
static::$advertisementIds[] = $this->id;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the advertisement to an HTML string
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return $this->toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get images
|
||||
*
|
||||
@@ -248,7 +403,7 @@ class _Advertisement extends \IPS\Patterns\ActiveRecord
|
||||
public function delete()
|
||||
{
|
||||
/* If we have images, delete them */
|
||||
if( count( $this->_images ) )
|
||||
if( \count( $this->_images ) )
|
||||
{
|
||||
\IPS\File::get( $this->storageExtension(), $this->_images['large'] )->delete();
|
||||
|
||||
@@ -262,6 +417,9 @@ class _Advertisement extends \IPS\Patterns\ActiveRecord
|
||||
\IPS\File::get( $this->storageExtension(), $this->_images['medium'] )->delete();
|
||||
}
|
||||
}
|
||||
|
||||
/* Delete the translatable title */
|
||||
\IPS\Lang::deleteCustom( 'core', "core_advert_{$this->id}" );
|
||||
|
||||
/* Delete */
|
||||
parent::delete();
|
||||
@@ -280,9 +438,29 @@ class _Advertisement extends \IPS\Patterns\ActiveRecord
|
||||
*/
|
||||
public static function updateImpressions()
|
||||
{
|
||||
if( count( static::$advertisementIds ) )
|
||||
if( \count( static::$advertisementIds ) )
|
||||
{
|
||||
\IPS\Db::i()->update( 'core_advertisements', "ad_impressions=ad_impressions+1", "ad_id IN(" . implode( ',', static::$advertisementIds ) . ")" );
|
||||
|
||||
/* Reset in case execution continues and more ads are shown */
|
||||
static::$advertisementIds = array();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update ad impressions for advertisements sent in emails
|
||||
*
|
||||
* @param int $impressions Number of impressions (may be more than one if mergeAndSend() was called)
|
||||
* @return void
|
||||
*/
|
||||
public static function updateEmailImpressions( $impressions=1 )
|
||||
{
|
||||
if( \count( static::$advertisementIdsEmail ) )
|
||||
{
|
||||
\IPS\Db::i()->update( 'core_advertisements', "ad_impressions=ad_impressions+" . $impressions, "ad_id IN(" . implode( ',', static::$advertisementIdsEmail ) . ")" );
|
||||
|
||||
/* Reset in case execution continues and more ads are sent */
|
||||
static::$advertisementIdsEmail = array();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user