How to use pagination with alaouy/youtube laravel package? - php

I'm using alaouy/youtube package for one of my projects, Its working just fine, But with this method I can't use pagination! Is there any way or I've to write my own code? There is a vendor folder in my resources with pagination folder but I can't get work with it!
// List videos in a given channel, return an array of PHP objects
$videoList = Youtube::listChannelVideos('UCk1SpWNzOs4MYmr0uICEntg', 40);

I'm the creator of that package, happy that it was helpful for you and sorry that the documentation wasn't clear enough.
The listChannelVideos is based on the searchAdvanced method, so you can paginate the channel videos like this :
$params = array(
'channelId' => 'UCk1SpWNzOs4MYmr0uICEntg',
'type' => 'video',
'part' => 'id, snippet',
'maxResults' => 40);
// Make intial call. with second argument to reveal page info such as page tokens
$search = Youtube::searchAdvanced($params, true);
print_r($search); // First page results
// Check if we have a pageToken
if (isset($search['info']['nextPageToken'])) {
$params['pageToken'] = $search['info']['nextPageToken'];
}
// Make another call and repeat
$search = Youtube::searchAdvanced($params, true);
print_r($search); // Second page results
Hope this will help you!

Related

creating a variable shortcode based on user input that returns (prechosen) variables Wordpress

I need to update values in all posts of a wordpress installation on a regular base. I was gooing to use shortcodes to insert the request into the wordpress post. Then use a custom functions.php that holds all the variables that need to be updated from time to time.
I got it working. Somehow but not the way I intended to use it. I'm a total beginner. Please consider this when answering my questions.
I want to have a function that reads what comes after honda_ and displays the correct value in wordpress without having to create a separate shortcode for each variable.
When entering [honda_link] wordpress should display the value from honda_link. When entering [honda_longlink] the value from honda_longlink variable should get displayed. I don't want to create a shortcode for each value.
I came up with this as a working solution...
// Honda
function honda() {
$honda_link = 'www.honda.com';
$honda_longlink = 'http://www.honda.com';
$honda_free = 'Free';
$honda_new = '23.688 $';
$honda_mileage = '00';
return $honda_link;
}
add_shortcode('neu', 'honda_link');
I tried some approaches by using an array but it ultimately failed all the time. I also tried it with if statements but wasn't able to get the right value displayed.
Someone willing to help a noob? I think I need to see a working example in order to understand it. The code snippets I have been looking at (that do something similiar but not the same I want to achieve) did confuse me more than they helped me.
I came up with this / Which works in a way but... This isn't very comfortable to use.
add_shortcode('HONDA','HONDA_TEST');
function HONDA_TEST($atts = array(), $content = null, $tag){
shortcode_atts(array(
'var1' => 'default var1',
'var2' => false,
'var3' => false,
'var4' => false
), $atts);
if ($atts['var2'])
return 'honda2';
else if ($atts['var3'])
return 'honda3';
else if ($atts['var4'])
return 'honda4';
else
return 'honda1';
}
So now when using:
[HONDA var1="novalue"][/HONDA]
[HONDA var2="novalue"][/HONDA]
[HONDA var3="novalue"][/HONDA]
[HONDA var4="novalue"][/HONDA]
it shows:
honda1
honda2
honda3
honda4
and so on.
Is there a better way to achieve the intended goal from post #1 ? Any way I could import the $variables from 1st post in bulk for example?
I don't have a working WP setup right now to test, but could you try this:
add_shortcode('HONDA','HONDA_TEST');
function HONDA_TEST($atts = array(), $content = null, $tag){
$atts = shortcode_atts(array(
'model' => 1,
), $atts);
$myHondas = [1 => 'honda1', 'honda2', 'honda3'];
return isset($myHondas[$atts['model']]) ? $myHondas[$atts['model']] : 'unknown model id';
}
And use it with [HONDA model="1"], [HONDA model="2"]

php function gives a loop - hostfact API

I'm using hostfact https://www.hostfact.nl for our billing. Now I want to extend the code and get a loop on executing the internalAPI in hooks.php. I tried to find an answer in the documentation https://www.hostfact.nl/developer/api/ but without success.
// When a client is edited, the bankParameters are checked and validated.
// That is only an example
function action_debtor_is_edited($parameters){
$params = array(
'DebtorCode' => $parameters['DebtorCode']
);
$hostfactResult = internalAPI('debtor', 'show', $params);
$bankParams = array(
'Identifier' => '2',
'AccountNumber' => 'AT6734080000000000',
'AccountBank' => 'test bank',
'AccountCity' => 'city',
'AccountBIC' => 'RLNODASD'
);
$bankResult = internalAPI('debtor', 'edit', $bankParams);
}
I've already asked the support and got follwoing answer:
One of the option is keeping track of the situation. For example, use
a static or global variable. When you call an (internal)API call from
a webhook, first flag that variable, so in the newly called webhook
you know that is not needed to execute the webhook again (at least not
the part which causes the loop). In the original call, after receiving
the result, the flag can be unset again.
My Problem is, I'm new to PHP and don't understand what I should do to avoid the loop. The $bankParams variable is intentionally hardcoded.
Can you help?

API pagination, how to implement page token paging method?

I am working on implementing an API interface for my project.
As i know, there are different forms to make pagination through the results, like the following:
https://example.com/api/purchaseorders?page=2&pagesize=25
But, i see many APIs like google use a different approach, in which they use a "pageToken" to let the user move between the pages of results, for example:
https://example.com/api/purchaseorders?pagesize=25&pageToken=ClkKHgoRc291cmNlX2NyZWF0ZWRfYXQSCQjA67Si5sr
So instead of page=2 they used pageToken=[token].
It is not clear for me the idea of pageToken and how to implement it.
It will be helpful if you guide me to any resources so i can get more knowledge.
Thank you.
Here's a very simple standalone example using the filesystem as a keyvalue store (since a filesystem will always be available).
$requestParameters = [];
if (($token = filter_input(INPUT_GET,"pageToken")) && is_readable("/tmp/$token")) {
$requestParameters = file_get_contents("/tmp/$token");
} else {
$requestParameters = [
"q" => filter_input(INPUT_GET,"q"),
"pageSize" => filter_input(INPUT_GET,"pageSize",FILTER_VALIDATE_INT),
"page" => filter_input(INPUT_GET,"page",FILTER_VALIDATE_INT)
];
}
$nextPageRequestParameters = $requestParameters;
$nextPageRequestParameters["page"]++;
$nextPageToken = md5(serialize($nextPageRequestParameters)); //This is not ideal but at least people can't guess it easily.
file_put_contents("/tmp/$nextPageToken", serialize($nextPageRequestParameters));
//Do request using $requestParameters
$result = [ "nextPageToken" => $nextPageToken, "data" => $resultData ];
echo json_encode($result);

WHMCS: How to get the current client in addon module clientarea page?

Given that I have a WHMCS addon that I call 'my_addon'. I created the main addon file 'my_addon.php' which does contain nothing than:
<?php
function my_addon_clientarea($vars) {
$client = null;
return array(
'pagetitle' => 'My Addon',
'breadcrumb' => array('index.php?m=my_addon'=>'My Addon'),
'templatefile' => 'views/myaddon_view',
'vars' => array(
'client' => $client
)
);
}
This does basically work. It does give me my template file, everything is passed through. My question is: How do I get the currently logged in client from within that function?
I didn't find any API method and I can't see any constant which does hold this information.
There must be a way to get the current client within the clientarea? Thanks for your help!
For those who do come after me and have the same problem: it's easy to solve. Turned out, that I just had to think it through... I found the client id to be available in the $_SESSION-variable.
So, if you are looking for the client's id:
<?php
function my_addon_clientarea($vars) {
$clientid = $_SESSION['uid'];
// And so on...
}
The official way to get current user information is:
$currentUser = new \WHMCS\Authentication\CurrentUser;
$user = $currentUser->user();
You can find more information here

Access Paginator object in controller

I am using cakePHP 2.5 and I want to access $this->Paginator->hasNext() in controller, but it through exception.
$type = array('featured', 'newest', 'trending');
foreach($type as $each)
{
$conditions = array('Product.status'=>'1', "is_$each" => '1');
$this->Product->recursive = 1;
//retrieve and set final result set
$this->paginate = array(
'fields' => array('Product.*'),
'conditions' => $conditions,
'limit' => $page_limit,
'page' => $page,
'order' => "$order_by $order_by_sort",
);
$products[$each] = $this->paginate('Product');
}
On my page, I want to display 3 type of product featured/trending/newest. Initially I load first page by default, then when user scroll down then I will call ajax and append next page like wise. But if last page is reached then I want to stop ajax call (because unknown page through 404 not found error).
To overcome that, I prevent AJAX call for unknown pages. And also make sure that first page isn't last!!
Hope my details make sense!
I could not find any answer on any forum/document. Please guide me or point me if it's duplicate of any.
You don't need to (and cannot*) access a helper in the controller.
Take a look at the source code for hasNext which is just calling the helper function _hasPage, which is simply checking the parameters array.
The paginator parameters are all available in the controller:
$paging = array(
'page' => $page,
'current' => count($results),
'count' => $count,
'prevPage' => ($page > 1),
'nextPage' => ($count > ($page * $limit)),
'pageCount' => $pageCount,
'order' => $order,
'limit' => $limit,
'options' => Hash::diff($options, $defaults),
'paramType' => $options['paramType']
);
So from the code in the question:
$this->request['paging']['Product']['nextPage']
Is the information you're looking for (it will be overwritten each time you call paginate.
Consider your application design.
Unless you have exactly the same number of featured, newest and trending records - it'd be better to have one ajax request for each type. Also see this related question, for more information on implementing infinite-scroll type pages.
* Almost anything is possible, you certainly should not need to use a helper in a controller though.
MVC Violation
$this->Paginator->hasNext()
That's a method of the helper. Helpers are not thought to be used in controllers because this is a violation of the MVC pattern and bad practice. If you think you have to you're wrong: Fix your architecture, it's broken by design.
Proper solution
Return the status of the pagination along with the data in the AJAX response:
$this->set('pagination', $this->request->paging['Product']);
$this->set('products', $result);
$this->set('_serialize', ['products', 'pagination']);
You can change the state of your view depending on the data then:
if (pagination.Product.nextPage == true) { /*... */ }

Categories