function syncCommentsAction()
 {
     $id = (int) $_REQUEST['ID'];
     $page = Page::get()->byID($id);
     DisqusSync::sync($page->disqusIdentifier());
     $this->owner->response->addHeader('X-Status', sprintf('Synced successfuly'));
     return;
 }
 function sync_by_ident()
 {
     $returnmessage = Director::is_cli() || Director::isDev() ? 1 : 0;
     return DisqusSync::sync($this->request->param('ID'), $returnmessage);
 }
    function DisqusPageComments()
    {
        // if the owner DataObject is Versioned, don't display DISQUS until the post is published
        // to avoid identifier / URL conflicts.
        if ($this->owner->hasExtension('Versioned') && Versioned::current_stage() == 'Stage') {
            return '<p class="alert">' . _t("Disqus.NOTLIVEALERT", "Disqus comments are temporary OFF in Stage mode. Logout or turn in Live mode!") . '</p>';
        }
        $config = SiteConfig::current_site_config();
        $ti = $this->disqusIdentifier();
        if ($config->disqus_shortname && $this->owner->disqusEnabled()) {
            $script = '
			    var disqus_shortname = \'' . $config->disqus_shortname . '\';
				' . $this->owner->disqusDeveloperJsVar() . '
			    var disqus_identifier = \'' . $ti . '\';
			    var disqus_url = \'' . $this->owner->absoluteLink() . '\';
				' . $this->owner->disqusLocaleJsVar() . '
				
			    (function() {
			        var dsq = document.createElement(\'script\'); dsq.type = \'text/javascript\'; dsq.async = true;
			        dsq.src = \'https://\' + disqus_shortname + \'.disqus.com/embed.js\';
			        (document.getElementsByTagName(\'head\')[0] || document.getElementsByTagName(\'body\')[0]).appendChild(dsq);
			    })();				
			';
            Requirements::customScript($script);
            $templateData = array('SyncDisqus' => true);
            // Hide Local Comments -> we will use Disqus service
            $hideLocal = "\n\t\t\t\tfunction hideLocalComments() {\n\t\t\t\t\tdocument.getElementById('disqus_local').style.display = 'none';\n\t\t\t\t}\n\t\t\t\twindow.onload = hideLocalComments;\n\t\t\t";
            Requirements::customScript($hideLocal);
            // Get comments
            //$results = DataObject::get('DisqusComment',"isSynced = 1 AND isApproved = 1 AND threadIdentifier = '$ti'");
            $results = DisqusComment::get()->filter(array('isSynced' => '1', 'isApproved' => '1', 'threadIdentifier' => $ti));
            // Prepare data for template
            $templateData['LocalComments'] = $results;
            // Sync comments
            $now = time();
            $synced = strtotime($this->owner->LastEdited);
            if ($now - $synced > $config->disqus_synctime) {
                if ($config->disqus_syncinbg) {
                    // background process
                    // from here: https://stackoverflow.com/questions/1993036/run-function-in-background
                    // TODO: Windows check is not fully correct
                    // Debug
                    // echo "trying to sync in BG";
                    $cmd = "php " . Director::baseFolder() . DIRECTORY_SEPARATOR . "framework" . DIRECTORY_SEPARATOR . "cli-script.php /disqussync/sync_by_ident/" . $ti . "/";
                    // Debug
                    //echo $cmd;
                    if (substr(php_uname(), 0, 7) == "Windows") {
                        pclose(popen("start /B " . $cmd, "r"));
                    } else {
                        exec($cmd . " > /dev/null &");
                    }
                } else {
                    $returnmessage = Director::isDev() ? 1 : 0;
                    DisqusSync::sync($ti, $returnmessage);
                }
                // updates LastEdited data
                $this->owner->write();
                // saves the record
            } else {
                // Debug
                // echo "not needed to sync";
            }
            return $this->owner->customise($templateData)->renderWith(array('DisqusComments'));
        }
    }