* @copyright 2011 Benjamin Carl * @license http://www.opensource.org/licenses/bsd-license.php The BSD License */ /** * Amazon Product Widget Front - Front Class * * This class is responsible for displaying the data in the frontend (template) e.g. within a sidebar. * * @category Wordpress * @package Wordpress_Apw * @subpackage Wordpress_Apw_Front * @author Benjamin Carl * @copyright 2011 Benjamin Carl * @license http://www.opensource.org/licenses/bsd-license.php The BSD License */ class Apw_Front { /** * contains an instance of the wordpress oop interface * * @var object An instance of the wordpress oop interface * @access private */ private $_wordpress; /** * contains an instance of the apw config (defaults ...) * * @var object An instance of the apw config (defaults ...) * @access private */ private $_configuration; /** * contains an instance of the data class * * @var object An instance of the data class * @access private */ private $_data; /** * contains an instance of the template class * * @var object An instance of the template class * @access private */ private $_template; /** * displays the widget in the sidebar/template * * This method is responsible for retrieving data and generating HTML for * displaying it in the sidebar. * * @param array $arguments The arguments for display * @param array $instance The instance * * @return void * @access public * @author Benjamin Carl */ public function displayWidget($arguments, $instance) { $beforeWidget = $arguments['before_widget']; $afterWidget = $arguments['after_widget']; $beforeTitle = $arguments['before_title']; $afterTitle = $arguments['after_title']; // user-selected settings for widget $title = $this->_wordpress->filters->apply('widget_title', $instance['title']); $list = $instance['list']; $template = $instance['template']; // get a random ASIN from list of available ASINs $asin = $list[array_rand($list)]; // before widget (defined by themes). echo $beforeWidget; // title of widget (before and after defined by themes) if ($title) { echo $beforeTitle . $title . $afterTitle; } // the HTML-template if ($template) { // try to get product information try { $itemData = $this->_data->getItemData($asin); } catch (Exception $e) { // empty data $itemData = array(); } // add additional data to item-data $itemData = array_merge($itemData, $this->_getAdditionalData($asin)); // prepare additional data for template parser $template = $this->_template->parse($template, $itemData); // echo HTML-Code echo $template; } // after widget (defined by themes) echo $afterWidget; } /** * returns the additional data like tracking-image-url, * associate-id and product-url as array * * This method is responsible for returning the additional data like tracking-image-url, * associate-id and product-url as array. * * @param string $asin The current ASIN to use * * @return array The array containing the additional data * @access private * @author Benjamin Carl */ private function _getAdditionalData($asin) { // get associate id $associateId = $this->_configuration->getOption('aws_associate_id'); // construct tracking-image-url $trackingImageUrl = str_replace( '[[ASIN]]', $asin, str_replace( '[[ASSOCIATE-ID]]', $associateId, $this->_configuration->getOption('aws_image_tracking_url') ) ); // construct product-url $productUrl = str_replace( '[[ASIN]]', $asin, str_replace( '[[ASSOCIATE-ID]]', $associateId, $this->_configuration->getOption('aws_product_url') ) ); // return additional data return array( 'associate-id' => $associateId, 'image-tracking-url' => $trackingImageUrl, 'product-url' => $productUrl ); } /******************************************************************************************************************* * Dependency-Injection (DI) ******************************************************************************************************************/ /** * sets the dependency for wordpress * * This method is intend to set the dependency for wordpress. * * @param object $wordpress An instance of the wordpress class * * @return void * @access public * @author Benjamin Carl * @PdInject wordpress */ public function setWordpress($wordpress) { // store wordpress oop interface instance $this->_wordpress = $wordpress; } /** * sets the dependency for configuration * * This method is intend to set the dependency for configuration. * * @param object $configuration An instance of the configuration class * * @return void * @access public * @author Benjamin Carl * @PdInject configuration */ public function setConfiguration($configuration) { // store instance of configuration (for reading configuration) $this->_configuration = $configuration; } /** * sets the dependency for data * * This method is intend to set the dependency for data. * * @param object $data An instance of the data class * * @return void * @access public * @author Benjamin Carl * @PdInject data */ public function setData($data) { // store instance of data (for retrieving product/asin information) $this->_data = $data; } /** * sets the dependency for template * * This method is intend to set the dependency for template. * * @param object $template An instance of the template class * * @return void * @access public * @author Benjamin Carl * @PdInject template */ public function setTemplate($template) { // store instance of template $this->_template = $template; } } ?>