1

Trying to add an actionUrl to this plugin. When I set offset manually to a number, everything works, so I think the controller is working properly.

I can't seem to get the offset from the button using getParam (or anything else).

What's the best way to get a offset back into the template?

Controller

<?php

namespace Craft;

class TumblrController extends BaseController{

    public function actionOffset(){

        $offset = craft()->request->getRequiredParam('offset', null);

        craft()->tumblr->getSubmissionData($offset);

        $this->redirect($_SERVER['HTTP_REFERER']);
    }
}

Service

public function getSubmissionData($offset){

$submission_data = array();

// some variables that will be pretttty useful
$settings       = craft()->plugins->getPlugin('tumblr')->getSettings();
$publicKey      = 'example';
$secretKey      = 'example';
$token          = 'example';
$tokenSecret    = 'example';
$callback_url   = 'tumblr/callback';
$blogName       = 'example.tumblr.com';

$client = new \Tumblr\API\Client($publicKey, $secretKey, $token, $tokenSecret);

//SETTING UP OFFSET
//$offset = (!isset($offset) || is_null($offset)) ? '0' : $offset; 

$submissions = $client->getSubmissionPosts($blogName, array('offset' => $offset, 'limit' => 50));

$i = 0;

foreach($submissions->posts as $submission) {
    $i++;
    foreach($submission as $key => $value){
        if ($key == "timestamp" || $key ==  "publisher" || $key == "video_type" || $key == "thumbnail_url" || $key == "thumbnail_width" || $key == "thumbnail_height" ||     $key == "html5_capable" || $key == "id" || $key == "short_url" || $key == "video" || $key == "content_raw" || $key == "player" || $key == "highlighted" || $key     == "photos" || $key == "trail" || $key == "tags" || $key == "description" || $key == "reblog" || $key == "followed" || $key == "blog_name" || $key ==     "reblog_key" || $key == "liked" || $key == "note_count" || $key == "link_author" || $key == "excerpt" || $key == "can_send_in_message" || $key == "can_reply" ||     $key == "recommended_source" || $key == "recommended_color" || $key == "font" || $key == "background" || $key == "format" || $key == "post_url" || $key ==     "slug" || $key == "is_submission" || $key == "is_anonymous")
            continue;
        $submission_data[$i][$key][] = isset($value) ? $value : "Nothing set"; 

    } 
}


$charset = craft()->templates->getTwig()->getCharset();
$twig_html = new \Twig_Markup(json_encode($submission_data), $charset);

$tagdata = $submission_data;
foreach($submission_data as $key => $value){
    foreach($value as $k => $v){
        $tagdata = str_replace("{".$key."}", $v[0], $submission_data);
    }
}

// // // // replace the embed code with the Twig object
$tagdata['embed_code'] = $twig_html;


return $tagdata;
}

Template

{% extends "_layouts/cp" %}
{% set title = "Tumblr Submissions"|t %}
{% includeCssResource "tumblr/css/table.css" %}

{% set offset = craft.request.getParam('offset') %}
{% set submissions = craft.tumblr.submissions(offset) %}

{% block content %}

<pre>{{ dump(offset) }}</pre>
    <div id="page-wrap">
        {% for i in range(1, 10, 1) %}
            <a href="{{ actionUrl('tumblr/offset', {offset: i}) }}"><button type="button">{{i}}</button></a>
        {% endfor %}
        <table class="table">
            {% for key,submit in submissions %}
                {% if loop.index == 1 %}
                    <thead>
                        <tr class="row header">
                            {% for ref,items in submit %}
                                <th class="cell">{{ ref }}</th>
                            {% endfor %}
                        </tr>
                    </thead>
                    <tbody>
                {% endif %}
                    <tr class="row">
                        {% for ref,items in submit %}
                            <td class="cell">{{ items[0]|e }}</td>
                        {% endfor %}
                    </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>
{% endblock %}    
1

After some testing the answer to this was that I needed to setup custom CP routes to pass the offset variable from template to controller and back to template.

public function registerCpRoutes(){
    return array(
        'tumblr/offset' => array('action' => 'tumblr/offset')
    );
}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.