/**
     * Livepub compatible: 'in-cart' or 'not-in-cart' based on session cart
     * TODO: this won't cover the case where a variation of this product is in the cart.
     * @return string
     */
    public function CartStateClass()
    {
        return LivePubHelper::eval_php('
			return (!empty($_SESSION["Cart"])
				&& !empty($_SESSION["Cart"]["Items"])
				&& !empty($_SESSION["Cart"]["Items"][' . $this->owner->ID . ']))
			? "product-in-cart"
			: "product-not-in-cart";
		');
    }
 public function LiveLink()
 {
     if ($this->owner->hasField('CloudStatus') && $this->owner->CloudStatus === 'Live') {
         $http = $this->owner->getCloudURL(CloudBucket::LINK_HTTP);
         $https = $this->owner->getCloudURL(CloudBucket::LINK_HTTPS);
         return LivePubHelper::eval_php('return empty($_SERVER["HTTPS"]) ? "' . $http . '" : "' . $https . '";');
     } else {
         return $this->owner->Link();
     }
 }
    /**
     * @return Session
     */
    public function getFreshSession()
    {
        // set up CSRF token stuff
        $placeholder = Config::inst()->get('LiveFilesystemPublisher', 'security_token_placeholder');
        if ($placeholder) {
            $sessionKey = SecurityToken::inst()->getName();
            // This duplicates the SessionToken functionality exactly
            // But only requires the one class from Silverstripe
            LivePubHelper::require_session();
            LivePubHelper::add_init_code('
				function new_security_token() {
					require_once("' . BASE_PATH . '/framework/security/RandomGenerator.php");
					$generator = new RandomGenerator();
					return $_SESSION["' . $sessionKey . '"] = $generator->randomToken("sha1");
				}
				$security_token = isset($_SESSION["' . $sessionKey . '"]) ? $_SESSION["' . $sessionKey . '"] : new_security_token();
			', 'LiveSecurityToken');
            return new Session(array($sessionKey => $placeholder));
        } else {
            return new Session(null);
        }
    }
 /**
  * If we're publishing, returns proper php
  */
 public function obj($fieldName, $arguments = null, $forceReturnedObject = true, $cache = false, $cacheName = null)
 {
     $value = parent::obj($fieldName, $arguments, $forceReturnedObject, $cache, $cacheName);
     // if we're publishing and this variable is qualified, output php code instead
     if ($this->failover && $this->liveVars && isset($this->varName) && LivePubHelper::is_publishing() && $this->isLiveVar($fieldName)) {
         $accessor = "get{$fieldName}";
         $php = '';
         // find out how we got the data
         if ($this->failover instanceof ArrayData) {
             $php = '$' . $this->varName . '["' . $fieldName . '"]';
         } elseif (is_callable(array($this->failover, $fieldName))) {
             // !@TODO respect arguments
             $php = '$' . $this->varName . '->' . $fieldName . '()';
         } elseif (is_callable(array($this->failover, $accessor))) {
             // !@TODO respect arguments
             $php = '$' . $this->varName . '->' . $accessor . '()';
         } elseif (isset($this->failover, $fieldName)) {
             $php = '$' . $this->varName . '->' . $fieldName;
         }
         // return the appropriate php
         if ($php) {
             if (is_object($value)) {
                 if ($value instanceof ViewableWrapper) {
                     LivePubHelper::$init_code[] = '<?php $' . $value->getVar() . ' = ' . $php . ' ?>';
                 }
                 // !@TODO: only other option is DataObjectSet - check that this is handled right
             } else {
                 $value = in_array($fieldName, $this->liveVarsUnescaped) ? '<?php echo ' . $php . '; ?>' : '<?php echo htmlentities(' . $php . '); ?>';
                 if ($forceReturnedObject) {
                     $value = new HTMLText($value);
                 }
             }
         }
     }
     return $value;
 }
 protected function getObjectWrapper()
 {
     $o = new stdClass();
     $o->a = 'b';
     $o->b = 5;
     $o->c = array('e' => 'f');
     $o2 = new stdClass();
     $o2->h = 'i';
     $o->d = $o2;
     $o->e = array(array('a' => 1), array('a' => 2));
     return LivePubHelper::wrap($o);
 }
 /**
  * @return string
  */
 public function LoggedInClass()
 {
     return LivePubHelper::eval_php('return !empty($_SESSION["LoggedInMember"]) ? "logged-in" : "";');
 }
    /**
     * if this is called, the published version of the page
     * will include and initialize the DB::query stub
     * and connect to the main silverstripe database.
     * This allows limited use of DB::query() in both a live
     * and published context.
     */
    public static function require_silverstripe_db()
    {
        global $databaseConfig;
        if (self::is_publishing() && !self::$silverstripe_db_included) {
            self::$silverstripe_db_included = true;
            self::$init_code[] = '
				$databaseConfig = ' . var_export($databaseConfig, true) . ';
				require_once "' . dirname(dirname(__FILE__)) . '/dummy_classes/LivePubDB.php";
				DB::init();
			';
        }
    }
 function LPH_EndIf()
 {
     if (LivePubHelper::is_publishing()) {
         return '<?php endif; ?>';
     }
 }