Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Yii寄存器JS变量_Javascript_Php_Yii - Fatal编程技术网

Javascript Yii寄存器JS变量

Javascript Yii寄存器JS变量,javascript,php,yii,Javascript,Php,Yii,我正在使用基于Yii的HumHub,并尝试使用从丰富文本的函数中提取的URL设置JS变量 目前,模型中似乎没有设置变量,所以我甚至还没有真正开始编写脚本 它最终应该会获取OpenGraph数据,但我甚至无法获取要调试和使用的脚本的URL 基本enrichText函数 /** * Converts an given Ascii Text into a HTML Block * @param boolean $allowHtml transform user names i

我正在使用基于Yii的HumHub,并尝试使用从丰富文本的函数中提取的URL设置JS变量

目前,模型中似乎没有设置变量,所以我甚至还没有真正开始编写脚本

它最终应该会获取OpenGraph数据,但我甚至无法获取要调试和使用的脚本的URL

基本enrichText函数

   /**
     * Converts an given Ascii Text into a HTML Block
     * @param boolean $allowHtml transform user names in links
     * @param boolean $allowEmbed Sets if comitted video links will embedded
     *
     * Tasks:
     *      nl2br
     *      oembed urls
     */
    public static function enrichText($text, $from = 'default', $postid = '')
    {

        if ( $from == 'default' ) {
            $maxOembedCount = 3; // Maximum OEmbeds
            $oembedCount = 0; // OEmbeds used

            // Parse bbcodes before link parsing
            $text = self::parseBBCodes($text);

            $text = preg_replace_callback('/(?<!\])(https?:\/\/.*?)(\s|$)(?!\[)/i', function ($match) use (&$oembedCount, &$maxOembedCount) {

                // Try use oembed
                if ($maxOembedCount > $oembedCount) {
                    $oembed = UrlOembed::GetOembed($match[0]);
                    if ($oembed) {
                        $oembedCount++;
                        return $oembed;
                    }
                }

                $regurl = "/(http|https)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";

                // Check if there is a url in the text independently and render to JS var
                if(preg_match($regurl, $text, $url)) {

                    if (!empty($postid)) {      
                        Yii::app()->clientScript->setJavascriptVariable("ourl".$postid, $url[0]);
                    }

                }

                return HHtml::link($match[1], $match[1], array('target' => '_blank')).$match[2];

            }, $text);

            // get user and space details from guids
            $text = self::translateMentioning($text, true);

            // create image tag for emojis
            $text = self::translateEmojis($text);

            return nl2br($text);

        } else {

            // Parse bbcodes before link parsing
            $text = self::parseBBCodes($text, $from);

            return $text;

        }
    }

它会抛出错误吗?在
$post->id处回显的内容
,在您的“脏尝试”中,$postid始终是
'
脏尝试传递变量,日志上的第一个变量是
ourl93
,我似乎对添加JS杀死HumHub的live feed有问题,这是一个AJAX响应。因此,也许我的脚本不如另一个脚本的一部分好用。@johnSmith我更新了这篇文章,试图获取数据,我正在处理一个错误。可能与数据回调处理有关,因为我还不确定对象会是什么样子。
<?php
/**
 * This view represents a wall entry of a post.
 * Used by PostWidget to show Posts inside a wall.
 *
 * @property User $user the user which created this post
 * @property Post $post the current post
 *
 * @package humhub.modules.post
 * @since 0.5
 */
?>
<div class="panel panel-default post" id="post-<?php echo $post->id; ?>">
    <div class="panel-body">
        <?php $this->beginContent('application.modules_core.wall.views.wallLayout', array('object' => $post)); ?>
        <span id="post-content-<?php echo $post->id; ?>" style="overflow: hidden; margin-bottom: 5px;">
            <?php print HHtml::enrichText($post->message, 'default', $post->id); ?>
        </span>
        <a class="more-link-post hidden" id="more-link-post-<?php echo $post->id; ?>" data-state="down"
           style="margin: 20px 0 20px 0;" href="javascript:showMore(<?php echo $post->id; ?>);"><i
                class="fa fa-arrow-down"></i> <?php echo Yii::t('PostModule.widgets_views_post', 'Read full post...'); ?>
        </a>
        <div id="opengraph-<?php echo $post->id; ?>" class="opengraph-container">
            <div class="opengraph-img-<?php echo $post->id; ?>"></div>
            <div class="opengraph-body">
                <h2 class="opengraph-heading-<?php echo $post->id; ?>"></h2>
                <div class="opengraph-content-<?php echo $post->id; ?>"></div>
            </div>
        </div>
        <?php $this->endContent(); ?>
    </div>
</div>


<script type="text/javascript">


    console.log('Oembed URL for <?php echo $post->id; ?>: '+ourl<?php echo $post->id; ?>);

// ... etc
   /**
     * Converts an given Ascii Text into a HTML Block
     * @param boolean $allowHtml transform user names in links
     * @param boolean $allowEmbed Sets if comitted video links will embedded
     *
     * Tasks:
     *      nl2br
     *      oembed urls
     */
    public static function enrichText($text, $from = 'default', $postid = '')
    {

        if ( $from == 'default' ) {
            $maxOembedCount = 3; // Maximum OEmbeds
            $oembedCount = 0; // OEmbeds used

            // Parse bbcodes before link parsing
            $text = self::parseBBCodes($text);

            $text = preg_replace_callback('/(?<!\])(https?:\/\/.*?)(\s|$)(?!\[)/i', function ($match) use (&$oembedCount, &$maxOembedCount) {

                // Try use oembed
                if ($maxOembedCount > $oembedCount) {
                    $oembed = UrlOembed::GetOembed($match[0]);
                    if ($oembed) {
                        $oembedCount++;
                        return $oembed;
                    }
                }

                return HHtml::link($match[1], $match[1], array('target' => '_blank')).$match[2];

            }, $text);

            // get user and space details from guids
            $text = self::translateMentioning($text, true);

            // create image tag for emojis
            $text = self::translateEmojis($text);

            $regurl = "/(http|https)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";

            // Check if there is a url in the text independently and render to JS var
            if(preg_match($regurl, $text, $url)) {

                if (!empty($postid)) {      
                    $text .= '<script type="text/javascript"> var ourl'.$postid.' = \''.$url[0].'\'; </script>';
                }

            }

            return nl2br($text);

        } else {

            // Parse bbcodes before link parsing
            $text = self::parseBBCodes($text, $from);

            return $text;

        }
    }
<?php
/**
 * This view represents a wall entry of a post.
 * Used by PostWidget to show Posts inside a wall.
 *
 * @property User $user the user which created this post
 * @property Post $post the current post
 *
 * @package humhub.modules.post
 * @since 0.5
 */
?>
<div class="panel panel-default post" id="post-<?php echo $post->id; ?>">
    <div class="panel-body">
        <?php $this->beginContent('application.modules_core.wall.views.wallLayout', array('object' => $post)); ?>
        <span id="post-content-<?php echo $post->id; ?>" style="overflow: hidden; margin-bottom: 5px;">
            <?php print HHtml::enrichText($post->message, 'default', $post->id); ?>
        </span>
        <a class="more-link-post hidden" id="more-link-post-<?php echo $post->id; ?>" data-state="down"
           style="margin: 20px 0 20px 0;" href="javascript:showMore(<?php echo $post->id; ?>);"><i
                class="fa fa-arrow-down"></i> <?php echo Yii::t('PostModule.widgets_views_post', 'Read full post...'); ?>
        </a>
        <div id="opengraph-<?php echo $post->id; ?>" class="opengraph-container">
            <div class="opengraph-img-<?php echo $post->id; ?>"></div>
            <div class="opengraph-body">
                <h2 class="opengraph-heading-<?php echo $post->id; ?>"></h2>
                <div class="opengraph-content-<?php echo $post->id; ?>"></div>
            </div>
            <script type="text/javascript">
                $(document).ready(function(){
                    (function() {
                        var opengraph = "http://bfxsocial.strangled.net/resources/Opengraph/getInfo.php?callback=?";
                        $.getJSON( opengraph, {
                            href: ourl<?php echo $post->id; ?>,
                            format: "json"
                        })
                        .done(function( data ) {
                            console.log('<?php echo Yii::t('PostModule.widgets_views_post', 'Opengraph: Response from: '); ?>'+ourl-<?php echo $post->id; ?>+"\n\n"+data);
                            var img = $('<img />',{ id: 'og:img-<?php echo $post->id; ?>', src: data['og:image'], alt:'data.title'}).appendTo($('.opengraph-img-<?php echo $post->id; ?>'));
                            $('.opengraph-heading-<?php echo $post->id; ?>').html(data.title);
                            $('.opengraph-body-<?php echo $post->id; ?>').html(data.description);
                            $('#opengraph-<?php echo $post->id; ?>').show();
                        });
                    })();   
                });
            </script>
        </div>
        <?php $this->endContent(); ?>
    </div>
</div>

<!-- Opengraph Temp Style -->
<style type="text/css">
.opengraph-container 
  display: none;
  width: 100%;
  padding: 3px;
  margin: 5px;
  background-color: rgba(0,0,0,0.1);
  border: 1px solid rgba(150,150,150,0.1);
}
.opengraph-img {
  display: block;
  min-width: 99%;
  max-height: 350px;
  margin: 0 auto;
}
.opengraph-body {
  width: 99%;
  padding-top: 5px;
  border-top: 1px solid rgba(0,0,0,0.1);
}
.opengraph-heading {
  display: block;
  width: 250px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.opengraph-content {
  font-size: 12px;
  color: #7F7F7F;
}
</style>
<!-- End: Opengraph Temp Style -->