How to insert custom inline javascript code in php phalcon framework - phalcon

I want to insert from Controller in the end of some view's code like this
"<"script> setInterval(function(){ alert("Hello"); }, 3000);"<"/script>
How to do this?

There's multiple ways to do this in PhalconPHP. Depending on your needs, you might be able to get away with just:
$js='setInterval(function(){alert("Hello");},3000);';
$this->view->js=$js;
then from the view, you'd do:
<script><?php echo $js; ?></script>
If you need to insert the JavaScript into your top-level index.phtml from a nested view, the way to accomplish this is to first edit your ControllerBase.php and define a new collection:
$this->assets->collection('footer');
Then add a reference to the footer collection from your top-level index.phtml file:
<body>
<?php echo $this->getContent(); ?>
<?php $this->assets->outputJs('footer'); ?>
</body>
You could also use output('footer') instead if you just wanted a dynamic place in the document you can insert HTML of any kind. Note that a collection specializes in managing CSS and JavaScript, hence the outputJS method, but can also handle miscellaneous code for other purposes. There's also outputCSS, outputInlineJs, outputInlineCss, outputInline, and simply output. See:
http://php-phalcon-docs.readthedocs.org/en/latest/api/Phalcon_Assets_Manager.html
Then edit your ControllerBase.php to initialize the asset to an empty collection:
$this->assets->collection('footer');
(if you skip this step, your code will see errors if your top-level index.phtml file tries to output the contents of a non-existent collection if you never write to it, so always initialize it to an empty collection so it exists.)
Finally, from the controller you want to use, you'd do:
$this->assets->collection('footer')->addInlineJs($js);
where $js is your inline JavaScript, excluding the script tags.
You can also work with external JavaScript files by using
->addJs(...). By default it will be relative to your application directory, inserting a / automatically. If you dislike this functionality, you can set the second parameter to false then it will allow you to specify your own leading / or point your resources at another domain.
As far as the assets you can add to a collection, see:
https://docs.phalconphp.com/en/latest/api/Phalcon_Assets_Collection.html
Note that you can also add assets to your collection from your view and the changes would still appear in your top-most index.phtml.
It is also worth noting that you don't need to actually use collections, you can simply use assets without a collection, but I think collections are more powerful in that you get to name them so there's another level of separation in case you need to manage more than one kind of collection of data.
In terms of what you're trying to accomplish, if you're just trying to give the user a message, this is what Phalcon's flash is for, not to be confused with Adobe Flash which is for playing videos. Phalcon's flash is for flashing messages to the user such as error messages, or your form submit successfully kind of messages. See:
https://docs.phalconphp.com/en/latest/reference/flash.html
If you're still confused what flash is, a demo of what it's output is, you can see in the screenshot here: https://docs.phalconphp.com/en/latest/_images/invo-2.png
That is output of ->error(...) and ->notice(...) respectively. The flash component keeps track of a collection of the messages you'd like to show the user. Then once you're ready to display them to the user you'd call:
<?php echo $this->flash->output(); ?> from your view. It is best to make this call from your top-most template or a template which is always included in your top-most template such as your navigation template so you can easily display messages to the user. It is also useful for debugging. I'd suggest using twitter bootstrap for styling the flash output.
Some sample applications which you might find useful:
https://github.com/phalcon/invo
https://github.com/phalcon/vokuro
https://github.com/phalcon/website
https://github.com/phalcon/forum
Further reading:
https://docs.phalconphp.com/en/latest/index.html

Related

Concrete5 hardcode External Form to template

I have managed to create an external form using the method: http://www.concrete5.org/community/forums/usage/external-form-email... The form worked when I embedded the block via cms. However, I need to put the form into my template. the following code did not work.
$b = Block::getByName('My Form');
$b->display();
It it probably because my external form did not have a block name. But how could I set its name?
Block names can only be set when the block is placed on a page (which means you're in a catch-22 here and have no way to achieve that).
Instead of attempting to output this block directly, try putting it in a global area in your template instead. You can then tell the global area to not show editing controls or the red dotted outline by disabling them. See http://concrete5tricks.com/blog/dont-hardcode-blocks-use-a-global-area-instead/
btw, you might be interested in this code I have which makes custom contact forms much easier and better than the built-in "external forms" block (in my humble opinion): https://github.com/jordanlev/c5_custom_contact_form

PrestaShop - Reload CMS page with additional parameters

Situation: I needed to add form with POST method to CMS page. I created custom hook and a module displaying the form successfully. Then I need to react to user input errors eg. when user doesn't enter email address I need to detect it, display the whole page again together with the form and with "errors" in user input clearly stated.
Problem: The problem is to display the WHOLE page again with connected information (eg. about errors etc.). In the module PHP file when I add this kind of code,
return $this->display(__FILE__, 'modulename.tpl');
it (naturally) displays ONLY the form, not the whole CMS page with the form.
In case of this code,
Tools::redirectLink('cms.php?id_cms=7');
I can't get to transfer any information by GET neither POST method.
$_POST['test'] = 1;
Tools::redirectLink('cms.php?id_cms=7&test');
I tried to assign to smarty variables too
$smarty->assign('test', '1');
(I need to use it in .tpl file where the form itself is created) but no way to get it work.
{if isset($test)}...,
{if isset($smarty.post.test)}...,
{if isset($_POST['test'])}... {* neither of these conditionals end up as true *}
Even assigning a GET parameter to url has no impact, because there is link rewriting to some kind of friendly url I guess, no matter I included other argument or not. ([SHOPNAME]/cms.php?id_cms=7&test -> [SHOPNAME]/content/7-cmspage-name)
My question is: is there a way to "redirect" or "reload" current page (or possibly any page generally) in prestashop together with my own data included?
I kind of explained the whole case so I'm open to hear a better overall solution than my (maybe I'm thinking about the case in a wrong way at all). This would be other possible answer.
The simplest method would be to use javascript to validate the form - you can use jQuery to highlight the fields that are in error; providing visual feedback on how the submission failed. In effect you don't allow the user to submit the form (and thus leave the page) until you're happy that the action will succeed. I assume that you will then redirect to another page once a successful submission has been received.
There's lots of articles and how-tos available for using javascript, and indeed jQuery for form validation. If you want to keep the site lean and mean, then you can provide an override for the CMS controller and only enqueue the script for the specific page(s) you want to use form validation on.
If the validation is complex, then you might be best using AJAX and just reloading the form section of your page via a call to your module. Hooks aren't great for this kind of thing, so you might want to consider using an alternative mnethod to inject your code onto the cms page. I've written a few articles on this alternative approach which can be found on my prestashop blog

separate header, content and footer in yii

I have a login form in my header section of the website. If user is logged in than insted of the login form user profile details will be shown. The question is how to separate header footer and content into different views and call them from one controller? Or maybe there is another solution...Thanks for help.
In your header view you could write something like this.
<?php if(Yii::app()->user->getId()): ?>
<?php $this->renderPartial('//world/_header_user')); ?>
<?php else: ?>
<?php $this->renderPartial('//world/_header_guest')); ?>
<?php endif; ?>
Using the Model-View-Controller (MVC) design pattern, the look of a Yii-based site is naturally controlled by the View files. These files are a combination of HTML and PHP that help to create the desired output. Specific pages in a site will use specific View files. In fact, the View files are designed to be broken down quite atomically, such that, for example, the form used to both create and edit an employee record is its own file, and that file can be included by both create.php and update.php. As with most things in OOP, implementing atomic, decoupled functionality goes a long way towards improving reusability. But the individual View files are only part of the equation for rendering a Web page. Individual view files get rendered within a layout file. And although I’ve mentioned layouts a time or two in my writings on Yii, it’s a subject that deserves its own post.
To be clear, layouts are a type of View file. Specifically, whereas other View files get placed within a directory for the corresponding Controller (i.e., the SiteController pulls from views/site), layout files go within views/layouts. But while the other View files are associated with individual Controllers (and therefore, individual pages), layouts are communal, shared by all the pages. Simply put, a layout file is the parent wrapper for the entire site’s templating system. I’ll explain
ypu can see more details
http://www.larryullman.com/2012/05/16/working-with-layouts-in-yii
The easiest way is probably to use a different layout, which you just switch on login. If not, showing partials / components based on Yii::app()->user->isGuest also works well.
Your default generated Yii application has a parent Controller in protected/components/Controller.php.
If you need to access additional parameters in layout, add public properties to Controller, set them in your child controller, and use them in your view/layout files.

How can I display Joomla modules within a component?

I have a component with several categories of games and I want to display a different banner depending on the category. I need the banner (and link) to be customizable so it's not simply a case of showing categoryX.jpg or whatever.
I saw this Joomla help page on another question, but it only seems to display a module type, not one specific module. I'd like to use various mod_custom modules and display the one appropriate to the category (I can store each module's ID if necessary).
Any ideas?
This can be done in a 2 step process:
Use the "Load module into article" plugin to allow yourself to access the module using a plugin call like:
{module [*mod_id*]} // where *mod_id* is the module id
Now that you can call a plugin to put your module anywhere, you now need to go to the view code of your/a component you wish to add the module to and add this code:
echo JHTML::_('content.prepare', '{module [*mod_id*]}');
Please see this link - http://www.alltogetherasawhole.org/group/developers/forum/topics/running-joomla-content-plugins - regarding point number 2. I was trying to do the same thing and I found it didn't work, and the reason for this was a comment you can find on the page link above:
"I noticed that some plugin actually expect option=com_content for them to process onPrepareContent properly, so you might actually want to "fool" them by callin JRequet::setVar and restoring the correct values after the trigger."
If you would like to show module within PHP code then use this code, it's worked for me :
<?php echo JHTML::_('content.prepare', '{loadposition position-2}'); ?>
If you want to show module inside html content then just put this code where you want to show module.
{loadposition position-2}
It loads all modules which are assigned to position-2.
You can also use the Joomla Module Renderer
$doc = JFactory::getDocument();
$renderer = $doc->loadRenderer('modules');
$position = 'custom_position_name';
$options = array('style' => 'raw');
echo $renderer->render($position, $options, null);
Just a heads up, Along with being assigned the module position, that module also has to be set to "Display on All pages".
Joomla has this functionality built in. You can use the {loadposition XX} command within your content items. You can specify custom module positions that do not appear in your template to insure that you have the correct module in the correct article.
http://docs.joomla.org/How_do_you_put_a_module_inside_an_article%3F
You could create a new module?
If each category appears in the querystring (catid=X) then you could take a similar approach to https://support.pillwax.com/open-source/doku.php?id=joomla:header_image.
Alternatively, that link might actually do it for you.

Keeping DRY with progressive enhancement

I'm building a website with very small amounts of Javascript, just to add things to the page (like a contact form) without having to go to a new page.
I understand that I should build the contact page anyways (just in case the user doesn't have javascript turned on) and then use javascript if they've got it.
So where do I store the HTML for the form if I don't want to have it in two places?
(Normally I'm not so picky, but I'm curious on this one.)
If you have access to a server-side language, you can keep a separate snippet of the form in an external page. Then, you can include the snippet into the HTML content page with an appropriate include call. This has the added benefit that, for your JavaScript, you can pull the contact form from this snippet file using AJAX. In fact, many plugins allow you to display DHTML windows with HTML content. For example, check out ThickBox.
Without a server-side language, you can do something similar with frames. Just display the form snippet in a frame when you need to reference it. Personally, I don't like frames very much, so this isn't a very attractive solution for me, but you can use it if you choose (and style the frames appropriately).
Just put your HTML for the contact form in a .html file. Assuming you're using PHP or something, just include the file in your contact page and include it in the section for your dynamic contact form. The form should still submit to the same server-side page and have the same look and feel..
e.g. contactForm.html
<div class="contact-form">
<input ....>
</div>