PHP functions in mini blocks

Post

Posted
Rating:
#6147 (In Topic #1492)
Avatar
Standard member
Terry is in the usergroup ‘Fan in action’
Are PHP functions restricted in mini blocks?

I created a miniblock that does some GD drawing, but it seems like functions disallow it from being a miniblock. There are various PHP errors that talk about a function already being declared. I checked this against the hit_counter example miniblock by adding a simple function to the code, and it bails with an error. Solved. Here's the code for a mini block to create a "Sigil" based on a string. Theban.ttf (a true type font) is available on the web for free. If you need a mini block to draw some stuff, this is a good head start.
 

<?php

$raw=$GLOBALS['FORUM_DRIVER']->get_username(get_member());

$s = strtolower(htmlentities($raw));
$mult = 5; // How big should this whole thing be?
$arrayx = array(
    "a" => 82,
    "b" => 67,
    "c" => 37,
    "d" => 52,
    "e" => 22,
    "f" => 7,
    "g" => 7,
    "h" => 67,
    "i" => 52,
    "j" => 37,
    "k" => 22,
    "l" => 82,
    "m" => 82,
    "n" => 22,
    "o" => 52,
    "p" => 37,
    "q" => 67,
    "r" => 7,
    "s" => 7,
    "t" => 22,
    "u" => 52,
    "v" => 37,
    "w" => 67,
    "x" => 82,
    "y" => 7,
    "z" => 67,
    "_" => 7,
    "1" => 82,
    "2" => 82,
    "3" => 82,
    "4" => 82,
    "5" => 82,
    "6" => 82,
    "7" => 82,
    "8" => 82,
    "9" => 82
);
$arrayy = array(
    "a" => 7,
    "b" => 82,
    "c" => 7,
    "d" => 82,
    "e" => 82,
    "f" => 7,
    "g" => 22,
    "h" => 22,
    "i" => 67,
    "j" => 67,
    "k" => 22,
    "l" => 67,
    "m" => 52,
    "n" => 37,
    "o" => 37,
    "p" => 37,
    "q" => 52,
    "r" => 52,
    "s" => 37,
    "t" => 52,
    "u" => 52,
    "v" => 52,
    "w" => 37,
    "x" => 37,
    "y" => 67,
    "z" => 67,
    "_" => 82,
    "1" => 82,
    "2" => 82,
    "3" => 82,
    "4" => 82,
    "5" => 82,
    "6" => 82,
    "7" => 82,
    "8" => 82,
    "9" => 82
    
);
$handle = ImageCreate (90 * $mult, 90 * $mult + 30) or die ("Cannot Create image");
imageantialias($handle, true);
$bg_color = ImageColorAllocate ($handle, 0, 0, 0);
$txt_color1 = ImageColorAllocate ($handle, 255, 255, 255);
$txt_color2 = ImageColorAllocate ($handle, 25, 55, 155);
$line_color = ImageColorAllocate ($handle, 255, 255, 255);
$Red = ImageColorAllocate($handle, 255, 0, 0);
$Gold = ImageColorAllocate($handle, 255, 215, 0);
$Goldenrod = ImageColorAllocate($handle, 238, 232, 170);
$degs = 360 / strlen($s);
$spokes = floor (360 / $degs);
$oreginx = (90 * $mult) / 2;
$oreginy = (90 * $mult) / 2;

imageellipse ($handle, (($arrayx[$s[0]] * $mult) / 2) + 115, (($arrayy[$s[0]] * $mult) / 2) + 115, 10, 10, $txt_color1); // Where the Sigil line first started
imageellipse ($handle, (($arrayx[$s[strlen($s)-1]] * $mult) / 2) + 115, (($arrayy[$s[strlen($s)-1]] * $mult) / 2) + 115, 10, 10, $txt_color1); // Sigil line ended here



ImageString ($handle, 3,  20, $mult * 90 - 20, "Agrippa Magic Square (Sol) Sigil of the name " . $raw, $txt_color1);
ImageString ($handle, 2,  20, $mult * 90 + 0, "Some other string", $Goldenrod);



$text = 'Testing…';
$font = 'Theban.ttf';


imageellipse ($handle, (90 * $mult) / 2, (90 * $mult) / 2 , 90 * $mult - 100 , 90 * $mult - 100 , $txt_color1 );
imageellipse ($handle, (90 * $mult) / 2, (90 * $mult) / 2 , 90 * $mult - 150 , 90 * $mult - 150 , $txt_color1 );

for ($i = 0; $i < $spokes; $i++) {
	$current_angle = $degs * $i; // this is the angle as we go around the circumference
	$px = $oreginx + (163 * cos(deg2rad($current_angle)));
	$py = $oreginy + (163 * sin(deg2rad($current_angle)));
	$lpx = $px; // lettering point x
	$lpy = $py; // lettering point y
	if ($lpx > $oreginx) {
		$lpx = $lpx - 5; // adjustments because the ttf font fucks the spacing up
	} else {
		$lpx = $lpx + 8; // everything left of center verticle
	}
	if ($lpy > $oreginy) {
		$lpy = $lpy - 5;
	} else {
		$lpy = $lpy + 8;
	}
	
	imagettftext($handle, 14, 360 - $current_angle + 270, $lpx, $lpy, $Gold, $font, $raw[$i]); // post the letters of their name around the circumference
}


# Draw all of the lines in the center. This is the Sigil itself
for ($i = 0; $i < strlen($s)-1; $i++) {
	ImageLine($handle, (($arrayx[$s[$i]] * $mult) / 2) + 115, (($arrayy[$s[$i]] * $mult) / 2) + 115, (($arrayx[$s[$i+1]] * $mult) / 2) + 115, (($arrayy[$s[$i+1]] * $mult) / 2) + 115, ImageColorAllocate($handle, rand(0, 255), rand(0,255), rand(0, 255)));
}


ImagePng ($handle, getcwd() . "/sigils/" . $s . ".png");
echo "<a href='../sigils/" . $s . ".png' target ='_BLANK'><img src='../sigils/" . $s . ".png' width='100%'></a>";
?>



On the main page of a site, there are full sized blocks (used for the middle panel) and side blocks (used for the side panels). I've been looking for hours… is there a way to divide the main panel into two, like a DIV? Like a comcode tag that will split the content into two boxes side-by-side, but in the main panel.

Is there already a comcode to list existing Clubs? I tried using several tags but nothing seems to just show a list of Clubs. It'd be similar to showing the Members, but it would show Clubs instead.






In a miniblock, is there a PHP include file required for accessing user data? This code, and even small parts of it, crash a mini block.
if (get_member() != $GLOBALS['CNS_DRIVER']->get_guest_id()) { // Logged in user
        $member_info = cns_read_in_member_profile(get_member(), true);
	$raw = $member_info['username'];
	echo $raw;
} else {
	echo "Guest";	
}

Last edit: by Terry

Online now: No Back to the top

Post

Posted
Rating:
#6166
Avatar
Site director
Chris Graham is in the usergroup ‘Administrators’
Thanks for posting your code.

Please try and keep very unrelated questions to different topics. It's hard for people to reply to multiple diverse questions at once, or to keep track in convoluted topics.

There are various PHP errors that talk about a function already being declared

Mini-blocks might be loaded more than once by Composr. If you define a function in a direct way inside them, that will result in PHP complaining about it being defined twice.

I just made a change for the next patch release that makes it less likely they'll be loaded twice.

You can avoid the issue completely by defining a function like…

Code (PHP)

if (!function_exists('some_function')) {
    function some_function()
    {
        ...
    }
}
 

is there a way to divide the main panel into two, like a DIV? Like a comcode tag that will split the content into two boxes side-by-side, but in the main panel.

The defined start page is using some Comcode like this to put blocks into columns…

Code (HTML)

[surround="fp_col_blocks_wrap"]
        [surround="fp_col_block left"]
                ...
        [/surround]

        [surround="fp_col_block right"]
                ...
        [/surround]
[/surround]
 

Is there already a comcode to list existing Clubs? I tried using several tags but nothing seems to just show a list of Clubs. It'd be similar to showing the Members, but it would show Clubs instead.

You can use main_multi_content with some simple Filtercode:

Code

[block param="group" filter="g_is_private_club=1" sort="title"]main_multi_content[/block]

But it's unwieldy without additional templating.

To make it a list…

Edit BLOCK_MAIN_MULTI_CONTENT.tpl:

Code (HTML)

{+START,IF,{$NEQ,{$COMMA_LIST_GET,{BLOCK_PARAMS},raw},1}}
        {+START,SET,links}
                {+START,IF_NON_EMPTY,{SUBMIT_URL}{ARCHIVE_URL}}
                        <ul class="horizontal_links associated_links_block_group">
                                {+START,IF_NON_EMPTY,{SUBMIT_URL}}
                                        <li><a rel="add" href="{SUBMIT_URL*}">{ADD_STRING*}</a></li>
                                {+END}
                                {+START,IF_NON_EMPTY,{CONTENT}}
                                        {+START,IF_NON_EMPTY,{ARCHIVE_URL}}
                                                <li><a href="{ARCHIVE_URL*}" title="{!ARCHIVES}: {TYPE*}">{!ARCHIVES}</a></li>
                                        {+END}
                                {+END}
                        </ul>
                {+END}
        {+END}

        {$,Example carousel layout if the 'carousel' GUID was passed}
        {$,With some basic templating you could also achieve simple lists or tables}
        {+START,IF,{$EQ,{_GUID},carousel}}
                {$REQUIRE_JAVASCRIPT,dyn_comcode}
                {$REQUIRE_CSS,carousels}

                {$SET,carousel_id,{$RAND}}

                {+START,IF_NON_EMPTY,{CONTENT}}
                        <div id="carousel_{$GET*,carousel_id}" class="carousel" style="display: none">
                                <div class="move_left" onkeypress="this.onmousedown(event);" onmousedown="carousel_move({$GET*,carousel_id},-30); return false;"></div>
                                <div class="move_right" onkeypress="this.onmousedown(event);" onmousedown="carousel_move({$GET*,carousel_id},+30); return false;"></div>

                                <div class="main raw_ajax_grow_spot">
                                </div>
                        </div>

                        <div class="carousel_temp" id="carousel_ns_{$GET*,carousel_id}">
                                {+START,LOOP,CONTENT}
                                        {_loop_var}
                                {+END}
                        </div>

                        <script>// <![CDATA[
                                add_event_listener_abstract(window,'load',function() {
                                        initialise_carousel({$GET,carousel_id});

                                        var current_loading_from_pos_{$GET*,carousel_id}={START%};

                                        function carousel_prepare_load_more_{$GET*,carousel_id}(carousel_id)
                                        {
                                                var ob=document.getElementById('carousel_ns_'+carousel_id);

                                                if (ob.parentNode.scrollLeft+find_width(ob)*2<ob.scrollWidth) return; // Not close enough to need more results

                                                current_loading_from_pos_{$GET*,carousel_id}+={MAX%};

                                                call_block(
                                                        '{$FACILITATE_AJAX_BLOCK_CALL;,{BLOCK_PARAMS},raw=.*\,cache=.*}'+'&{START_PARAM%}='+current_loading_from_pos_{$GET*,carousel_id},
                                                        'raw=1,cache=0',
                                                        ob,
                                                        true
                                                );
                                        }
                                });
                        //]]></script>
                {+END}

                {+START,IF_EMPTY,{CONTENT}}
                        <p class="nothing_here">{!NO_ENTRIES,{CONTENT_TYPE}}</p>
                {+END}

                {$GET,links}
        {+END}

        {+START,IF,{$EQ,{_GUID},list}}
                {+START,IF_NON_EMPTY,{TITLE}}
                        <h2>{TITLE*}</h2>
                {+END}

                {$SET,list_format,1}
                {+START,IF_NON_EMPTY,{CONTENT}}
                        <ul>
                                {+START,LOOP,CONTENT}
                                        {_loop_var}
                                {+END}
                        </ul>
                {+END}
                {$SET,list_format,0}

                {+START,IF_EMPTY,{CONTENT}}
                        <p class="nothing_here">{!NO_ENTRIES,{CONTENT_TYPE}}</p>
                {+END}
        {+END}

        {$,Normal sequential box layout}
        {$,With some very basic CSS you could also achieve grid layouts}
        {+START,IF,{$NEQ,{_GUID},carousel,list}}
                {+START,IF_NON_EMPTY,{TITLE}}
                        <h2>{TITLE*}</h2>
                {+END}

                {$SET,ajax_block_main_multi_content_wrapper,ajax_block_main_multi_content_wrapper_{$RAND%}}
                <div id="{$GET*,ajax_block_main_multi_content_wrapper}" class="box_wrapper">
                        <div class="float_surrounder cguid_{_GUID|*} raw_ajax_grow_spot">
                                {+START,IF_NON_EMPTY,{CONTENT}}
                                        {+START,LOOP,CONTENT}
                                                {_loop_var}
                                        {+END}
                                {+END}

                                {+START,IF_EMPTY,{CONTENT}}
                                        <p class="nothing_here">{!NO_ENTRIES,{CONTENT_TYPE}}</p>
                                {+END}
                        </div>

                        {+START,IF_PASSED,PAGINATION}
                                {+START,IF_NON_EMPTY,{PAGINATION}}
                                        <div class="pagination_spacing float_surrounder ajax_block_wrapper_links">
                                                {PAGINATION}
                                        </div>

                                        {+START,INCLUDE,AJAX_PAGINATION}
                                                ALLOW_INFINITE_SCROLL=1
                                                WRAPPER_ID={$GET,ajax_block_main_multi_content_wrapper}
                                        {+END}
                                {+END}
                        {+END}

                        {$GET,links}
                </div>
        {+END}
{+END}

{+START,IF,{$EQ,{$COMMA_LIST_GET,{BLOCK_PARAMS},raw},1}}
        {+START,LOOP,CONTENT}
                {_loop_var}
        {+END}

        {+START,IF_PASSED,PAGINATION}
                {PAGINATION}
        {+END}
{+END}
 

Edit SIMPLE_PREVIEW_BOX.tpl:

Code (HTML)

{+START,IF,{$GET,list_format}}
        <li>
                <a href="{URL*}">{TITLE*}</a>{+START,IF_PASSED,ENTRY_DETAILS} ({ENTRY_DETAILS}){+END}
        </li>
{+END}

{+START,IF,{$NOT,{$GET,list_format}}}
        <section class="box box___simple_preview_box"><div class="box_inner">
                {+START,IF_PASSED,TITLE}{+START,IF_NON_EMPTY,{TITLE}}
                        <h3>
                                {+START,IF_PASSED,FRACTIONAL_EDIT_FIELD_NAME}{+START,IF_PASSED,FRACTIONAL_EDIT_FIELD_URL}
                                        {+START,FRACTIONAL_EDITABLE,{TITLE_PLAIN},{FRACTIONAL_EDIT_FIELD_NAME},{FRACTIONAL_EDIT_FIELD_URL},0}{TITLE*}{+END}
                                {+END}{+END}

                                {+START,IF_NON_PASSED,FRACTIONAL_EDIT_FIELD_NAME}
                                        {TITLE*}
                                {+END}
                        </h3>
                {+END}{+END}

                {+START,IF_PASSED,REP_IMAGE}
                        <div class="right float_separation"><a href="{URL*}">{REP_IMAGE}</a></div>
                {+END}

                {+START,IF_NON_EMPTY,{SUMMARY}}
                        <div class="float_surrounder">
                                {$PARAGRAPH,{SUMMARY}}
                        </div>
                {+END}

                {+START,IF_PASSED,ENTRY_DETAILS}
                        <p class="associated_details">
                                {$,Displays summary of category contents}
                                ({ENTRY_DETAILS})
                        </p>
                {+END}
                {+START,IF_PASSED,ENTRY_DETAILS_PREBRACKETED}
                        <p class="associated_details">
                                {$,Displays summary of category contents}
                                {ENTRY_DETAILS}
                        </p>
                {+END}

                {+START,IF_PASSED,BREADCRUMBS}{+START,IF_NON_EMPTY,{BREADCRUMBS}}
                        <nav class="breadcrumbs" itemprop="breadcrumb"><p>
                                {!LOCATED_IN,{BREADCRUMBS}}
                        </p></nav>
                {+END}{+END}

                {+START,IF_PASSED,URL}
                        <p class="shunted_button">
                                <a class="button_screen_item buttons__more" href="{URL*}"><span>{!VIEW}</span></a>
                        </p>
                {+END}
        </div></section>
{+END}
 

And then change your Comcode to:

Code

[block param="group" filter="g_is_private_club=1" sort="title" guid="list"]main_multi_content[/block]


Become a fan of Composr on Facebook or add me as a friend. Add me on on Twitter. Follow me on Minds (where I am most active). Support me on Patreon

Was I helpful?
  • If not, please let us know how we can do better (please try and propose any bigger ideas in such a way that they are fundable and scalable).
  • If so, please let others know about Composr whenever you see the opportunity or support me on Patreon.
  • If my reply is too Vulcan or expressed too much in business-strategy terms, and not particularly personal, I apologise. As a company & project maintainer, time is very limited to me, so usually when I write a reply I try and make it generic advice to all readers. I'm also naturally a joined-up thinker, so I always express my thoughts in combined business and technical terms. I recognise not everyone likes that, don't let my Vulcan-thinking stop you enjoying Composr on fun personal projects.
  • If my response can inspire a community tutorial, that's a great way of giving back to the project as a user.
Online now: No Back to the top
1 guest and 0 members have just viewed this.

Statistics

Users online:

John Connor, Vaiva, babu, MVLipwig, Manu, gabriel58, Philip, Salman

Forum statistics:
  • 1,118 topics, 5,367 posts, 6,350 members
  • Our newest member is khelopcgames
Birthdays:
Back to Top