CakeDC评级插件 - 任何教程/说明随处可见?

时间:2011-07-15 21:55:14

标签: cakephp plugins cakephp-1.3 cakedc

CakeDC评级插件是否有适当的教程/说明?我进站了4个小时,现场还没有结束 - 看书就没用了。

1 个答案:

答案 0 :(得分:1)

以下是我用来获得评级插件的一些代码。这都是Ajax,所以我在RatingHelper中添加了一个hack来避免显示“Rate!”按钮,只需在选项中添加submit => false。

    if ($options['createForm']) {
        if (!empty($options['target']) && !empty($options['createForm']['url']) && !empty($options['createForm']['ajaxOptions'])) {
            $ajaxOptions = array_merge(array('url' => $options['createForm']['url']), $options['createForm']['ajaxOptions']);
            $result .= $this->Js->submit(__d('ratings', 'Rate!'), $ajaxOptions) . "\n";
            $flush = true;
        } elseif ( !isset($options['submit']) || !empty($options['submit']) ) {
            if(empty($options['submit'])) {
                $result .= $this->Form->submit(__d('ratings', 'Rate!')) . "\n";
            } else {
                $result .= $this->Form->submit($options['submit']) . "\n";                  
            }
        }
        $result .= $this->Form->end() . "\n";
        if ($flush) {
            $this->Js->writeBuffer();
        }
    }

我有一个索引视图,显示了一堆汽车模型,我显示了整体评分和当前用户评分:

            <?php foreach ($carmodels as $carmodel): ?>
            <tr>
                <td><?php echo h($carmodel['Carmodel']['id']); ?>&nbsp;</td>
                <td><?php echo h($carmodel['Carmodel']['name']); ?>&nbsp;</td>              
                <td>
                    <?php echo $this->Html->link($carmodel['Make']['name'], array('controller' => 'makes', 'action' => 'view', $carmodel['Make']['id'])); ?>
                </td>
                <td>
                    <?php $overall_div = 'overall-rating' . $carmodel['Carmodel']['id']; ?>
                    <div id="<?php echo $overall_div; ?>">
                        <?php
                        echo $this->element('rating', array('rating' => $carmodel['Carmodel']['rating']));
                        ?>
                    </div>
                </td>   
                <td>

                    <?php
                    $msg_div = 'rating-msg' . $carmodel['Carmodel']['id'];
                    $rating = 0;
                    if (!empty($carmodel['Rating'])) {
                        $rating = $carmodel['Rating'][0]['value'];
                    }
                    echo $this->Rating->display(array(
                        'item' => $carmodel['Carmodel']['id'],
                        'type' => 'radio',
                        'stars' => 5,
                        'value' => $rating,
                        'submit' => false,
                        'createForm' => array(
                            'id' => 'CarmodelsRatingsDemoForm' . $carmodel['Carmodel']['id'],
                            'url' => array('action' => 'ratings_update', $carmodel['Carmodel']['id']),
                            'class' => 'rating',
                            'update-overall' => '#' . $overall_div,
                            'update-msg' => '#' . $msg_div,
                            )));
                    ?>  
                    <div id="<?php echo $msg_div; ?>" class="label label-info hidden">
                        <button class="close" data-dismiss="alert" type="button">×</button>
                        <div class="msg-default"><?php echo __('One moment...'); ?></div>
                        <div class="msg-result"></div>
                    </div>
                </td>

然后我有一些Jquery善良创造星星,然后对点击做出反应

    $(document).ready(function(){
    $('form.rating').stars({
        cancelShow:true,
        callback: function(ui, type, new_value) {
            var values = ui.$form.serializeArray();
            values.push({
                'name': 'rating',
                'value': new_value
            });
            values = jQuery.param(values);
            var msg = ui.$form.attr('update-msg');
            $(msg).removeClass('hidden');
            $(msg).show();
            $(msg).find('div.msg-default').show();
            $(msg).find('div.msg-result').hide();
            $.ajax({
                'type': 'POST',
                'dataType': 'json',
                'url': ui.$form.attr('action'),
                'data': values
            }).done(function(data) { 
                var overall_rating = ui.$form.attr('update-overall');
                if(overall_rating && data['overall_rating']){
                    $(overall_rating).html(data['overall_rating']);
                }
                if(msg){
                    $(msg).find('div.msg-default').hide();
                    if(data['msg']) {
                        $(msg).find('div.msg-result').show();
                        $(msg).find('div.msg-result').html(data['msg']);
                        window.setTimeout(function() {
                            $(msg).addClass('hidden');
                        }, 2000);
                    } else {
                        $(msg).addClass('hidden');
                    }
                }
                if(data['user_rating'] && data['user_rating']>0) {
                    ui.select(parseInt(data['user_rating']));
                }
            });
        }
    });
})

另外,我需要一小段css来隐藏Cake添加的单选按钮标签。

    form.rating > div.input {
    display: none;      
}

然后在控制器中,检查用户之前是否对该项目进行了评级,如果没有,则进行保存。我返回一个json数组,其中包含整体评级的渲染元素,用户评级(新的或现有的)以及在div中显示的字符串消息,该消息在2秒后消失。

    public function ratings_update($id = null) {
    $this->Carmodel->id = $id;
    if (!$this->Carmodel->exists()) {
        throw new NotFoundException(__('Invalid carmodel'));
    }
    $ratings = $this->Carmodel->isRatedBy($id, 1);
    if (!$ratings) {
        $this->Carmodel->rate($id, 1, $this->request->data['rating'], array('values' => array_combine(range(1, 5), range(1, 5))));
        $json['msg'] = __('Thank you');
    } else {
        if ($this->request->data['rating'] == 0) {
            $this->Carmodel->removeRating($id, 1);
            $json['msg'] = __('Rating removed');
        } else {
            $json['msg'] = __('Already rated');
        }
    }
    $this->set('rating', $this->Carmodel->field('rating'));
    $ratings = $this->Carmodel->isRatedBy($id, 1);
    $my_rating = 0;
    if (!empty($ratings)) {
        $my_rating = $ratings['Rating']['value'];
    }
    $this->autoRender = false;
    App::uses('View', 'View');
    $View = new View($this);

    $response = $View->render('/Elements/rating', 'ajax');
    $json['overall_rating'] = $response;
    $json['user_rating'] = $my_rating;
    return json_encode($json);
}

以下是显示整体评级星标的元素的代码,它可能应该是帮助者的一部分。

if (empty($rating)) {
    echo __('Be the first to rate!');
} else {
    echo str_repeat('<div class="ui-stars-star ui-stars-star-on"><a title=""></a></div>', $rating);
}

因此,虽然这不是一个超级详细的指令,但您应该能够弄清楚代码正在做什么并复制它。我花了一天的时间来完成它的工作。