| 1 |
<?php if( !defined('WPINC') ) die; |
| 2 |
/** |
| 3 |
* Leyka Templates Controller class. |
| 4 |
**/ |
| 5 |
|
| 6 |
abstract class Leyka_Template_Controller extends Leyka_Singleton { |
| 7 |
|
| 8 |
protected static $_instance; |
| 9 |
|
| 10 |
protected $_global_template_data = array(); |
| 11 |
protected $_template_data = array(); |
| 12 |
protected $_campaigns = array(); |
| 13 |
|
| 14 |
public function __get($name) { |
| 15 |
return empty($this->_global_template_data[$name]) ? null : $this->_global_template_data[$name]; |
| 16 |
} |
| 17 |
|
| 18 |
public function __set($name, $value) { |
| 19 |
$this->_global_template_data[$name] = $value; |
| 20 |
} |
| 21 |
|
| 22 |
/** A wrapper for templates to get current campaign object without direct using of global vars. */ |
| 23 |
public function get_current_campaign() { |
| 24 |
|
| 25 |
if($this->current_campaign) { |
| 26 |
|
| 27 |
if(empty($this->_campaigns[$this->current_campaign->id]) && is_a($this->current_campaign, 'Leyka_Campaign')) { |
| 28 |
$this->_campaigns[$this->current_campaign->id] = $this->current_campaign; |
| 29 |
} |
| 30 |
|
| 31 |
return $this->current_campaign; |
| 32 |
|
| 33 |
} |
| 34 |
|
| 35 |
$campaign = get_post(); |
| 36 |
if( !$campaign ) { |
| 37 |
return false; |
| 38 |
} |
| 39 |
|
| 40 |
$campaign = leyka_get_validated_campaign($campaign); |
| 41 |
if($campaign && empty($this->_campaigns[$campaign->id])) { |
| 42 |
$this->_campaigns[$campaign->id] = $campaign; |
| 43 |
} |
| 44 |
|
| 45 |
return $campaign; |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Get an array of template data for the campaign given + cache the data for further use. |
| 51 |
* @param $campaign mixed |
| 52 |
* @return array |
| 53 |
*/ |
| 54 |
public function get_template_data($campaign = false) { |
| 55 |
|
| 56 |
if( !$campaign ) { |
| 57 |
$campaign = $this->get_current_campaign(); |
| 58 |
} |
| 59 |
|
| 60 |
if( !$campaign ) { |
| 61 |
return array(); /** @todo There is no such campaign. Mb, throw some exception here. */ |
| 62 |
} |
| 63 |
|
| 64 |
if(empty($this->_campaigns[$campaign->id])) { |
| 65 |
$this->_campaigns[$campaign->id] = $campaign; |
| 66 |
} |
| 67 |
if(empty($this->_template_data[$campaign->id])) { |
| 68 |
$this->_generate_template_data($campaign); |
| 69 |
} |
| 70 |
|
| 71 |
return empty($this->_template_data[$campaign->id]) ? array() : $this->_template_data[$campaign->id]; |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Get all template data from DB, do some calculations, create a array. |
| 77 |
* @param $campaign Leyka_Campaign |
| 78 |
* @return null |
| 79 |
*/ |
| 80 |
abstract protected function _generate_template_data(Leyka_Campaign $campaign); |
| 81 |
|
| 82 |
} |