Jump to content

Learning to use the API


bwakad
 Share

Recommended Posts

this works: if ($user->hasRole("company"))

this only take the last into account: if ($user->hasRole("company | superuser"))

this works for both (removed spaces): if ($user->hasRole("company|superuser"))

So I guess the API is very strict on this...

In either case by the role - company or superuser - I can get to the 'member' page (also template/file). But there this code only display results for the role superuser?:

//members.php

$selects = $pages->find("template=user");
foreach($selects as $member) {

echo $member->name

}

EDIT --- solved it like this, at least I need to display contra according to role (but superuser needs to see all):

if($user->hasRole("member")) $selects = $users->find("roles=company");
if($user->hasRole("company")) $selects = $users->find("roles=member");
if($user->hasRole("superuser")) $selects = $users->find("template=user"); // should this be changed???

foreach($selects as $item) {
echo "<row><div class='medium-12 columns'>{$item->name}</div></row>";
}

Link to comment
Share on other sites

Just something I came across.

Currently my home link (in header) is this and correctly send me to the home page:

 <a href="<?php echo $config->urls->root; ?>">Client</a>

Now I am finishing my function which echoes the same line of code in a page. But apparently - used in a function (in function scope) I have to add a slash : /

<a href="<?php echo $config->urls->root; ?>/">Client</a>

Otherwise it will take url from the page you are on.

On a side note: using a function for a selector has really speed up loading time on pages.

Link to comment
Share on other sites

Like you were told before, $config (like $pages, $page, $fields, etc) doesn't work inside functions. You have to replace it by the method wire(), in this case wire('config'):

<a href="<?php echo wire('config')->urls->root; ?>/">Client</a>
Link to comment
Share on other sites

I am beginning to sound like britney "did it again". lol. Forgot that config also need wire in a function. Thanks for that.

My function is now this (which definitely improved speed)! I just echo selects() in any page I want...

function selects(){
    // get required values;
    $current = wire("page")->id;
    $field = wire("page")->parent->name;
    $noresult = wire("config")->urls->root; // single and double comma was giving issues below so use variable
    // test array for parent page ID (on browse tpl)
        if(in_array($current, array(1006,1008,1026,1027,1077,1138,1156) )) {
            $selector = "template=child-template,  limit=4, sort={$page->name}"; // example rootparent
        } else {
            $selector = "template=child-template, {$field}={$current}, limit=4, sort=-created"; // example rootparent-child
        }
    // need to define variable before count
    $selects = wire("pages")->find($selector);

        if(!count($selects)) {
            return "<div class='medium-12 columns'><h3>No items are found.</h3>
                    <p>try searching through our indexes at <a href='{$noresult}'>ClientC</a> 
                       or use the search form on the right.</p>
                    </div>";
        } else {
            // display quantity found
            $message = "Showing {$selects->count} out of {$selects->getTotal()}";
            $pagination = $selects->renderPager(array(
                'listMarkup' => "<ul class='MarkupPagerNav pagination'>{out}</ul>",
                'currentItemClass' => 'current'
            ));
            include("browse.inc"); // holds paginator, foreach, and layout. Include here so I can use this in function scope
        }

}
Link to comment
Share on other sites

bwakad,

the function wire('some-key') returns the respective instance of the object you are looking for. I am not sure if the instances are created on call, or just once, but if you are looking for performance you can at least save multiple function calls by assigning the returned object to a variable. Especially when in large loops the function calls can be expensive.

so instead of this:

wire('page')->someVal;
wire('page')->someOtherVal;
wire('page')->someMethod();

wire('pages')->find('selector');
wire('pages')->get('selector');

// and so on

you would use it like this:

// save page into variable
$page = wire('page');
// call methods and properties from variable
$page->someVal;
$page->someOtherVal;
$page->someMethod();

// save pages into variable
$pages = wire('pages');
// call methods and properties from variable
$pages->find('selector');
$pages->get('selector');

// and so on

Edit: Side note, just looked into the core code, looks like the instances of pages, page and so on are not created on each wire() call but merely accessed, but each wire() call also triggers multiple function and variable calls internally, so you would save those too, when saving into a var just once.

  • Like 2
Link to comment
Share on other sites

Those were good tips, saved me some lines of code/variables, is easier to read, and it is still working. It even saved me from a wrong ID in the array:

function selects(){
    // set required variables;
    $page = wire("page");
    $pages = wire("pages");
    $noresult = wire("config")->urls->root;
    // test array for parent page ID (on browse tpl)
    if(in_array($page->id, array(1006,1008,1026,1077,1127,1138,1156) )) {
        $selector = "template=child-template,  limit=4, sort={$page->name}"; // example rootparent
    } else {
        $selector = "template=child-template, {$page->parent->name}={$page->id}, limit=4, sort=-created"; // example rootparent-child
    }
    $selects = $pages->find($selector);
    if(!count($selects)) {
        return "<div class='medium-12 columns'><h3>No items are found.</h3>
                <p>try searching through our indexes at <a href='{$noresult}'>ClientC</a> 
                   or use the search form on the right.</p></div>";
    } else {
        // display quantity found
        $message = "Showing {$selects->count} out of {$selects->getTotal()}";
        $pagination = $selects->renderPager(array(
            'listMarkup' => "<ul class='MarkupPagerNav pagination'>{out}</ul>",
            'currentItemClass' => 'current'
        ));
        include("browse.inc"); // holds paginator, foreach, and layout
    }
}
Link to comment
Share on other sites

I am sure there must be a quicker way for my function below, but I started with $id = array(01, 02, 03) while I might as well used it directly in the $selector, or even more compact, use it in $pages->find("id=01|02|03").

I am referencing pages considering the names of objects ... Page Names are the most likely to change; Page Paths can change when page name changes; Template Names can change. However, page id, and template id are still valid. But if you suddenly use another template for a page then only page id or page path is valid. So, unless I start deleting things, my best bet (if I know which id I need) is to use id's in a selector. That's why I use this function for my menu:

function nav_browse() {

    // need this to use $page->id, or $page->name, etc...
    $page = wire("page"); 

    // need this since I am using $pages->find() below...
    $pages = wire("pages"); 

    // specify public pages for browse parents
    $selector = "id=1006|1008|1026|1077|1127|1138|1156|1183";

    $selects = $pages->find($selector);

    foreach($selects as $page) {
        echo "<li><a href='{$page->url}'>{$page->title}</a></li>";
    }
}

In my template i simply use: nav_browse();

Link to comment
Share on other sites

If you get pages with an ID, and those pages are deleted your script won't function anymore. But you know what pages you linked to, you know the behaviour of those pages.

That behaviour is giving to the page with the template. So if your selectors contains parents and templates and you delete those pages and add new ones, the script stays happy :-)

Link to comment
Share on other sites

Yes that is true as well... As I said "unless I start deleting things".

Since the function is used for my menu, it is less likely to delete one of those pages anyway.

My template is used for parents AND child's. But my menu pages are parent of the root. I could refer to template id/name and use parent=1 (as the very root parent home), but I would end up with pages in them which need to be restricted for members only, and in that, need to be restricted by member roles.

And what if I suddenly try to rename my template? Or use a different template? Page id would still hold, but I would need to change my code since I used template.

I guess it all depends on the situation.

Link to comment
Share on other sites

"wire" if used in a function. I know about:

wire("page"), wire("pages"), wire("user"), wire("users"), wire("config"), wire("input")

But, is there a rule for this? Does this has to do with get() or find() ?

Link to comment
Share on other sites

All of the standard PW variables must be converted to wire("variable") when you want to use them inside a function. I think you've probably already had variable scope mentioned to you, but you should read this (http://www.php.net/manual/en/language.variables.scope.php) at some point so you understand why the PW variables can't be used inside a function.

I don't understand what you mean about get() or find()

Link to comment
Share on other sites

I have read the part you mentioned. So PW 'wire' is doing what PHP 'global' is doing?

What I meant was: if I want to get or find a page(s), I need to use wire page or wire pages... then I can use the variables/fields from those pages?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...