How can include component js after templete js - joomla

I added javascript on templete index.php
$doc = JFactory::getDocument();
$doc->addScript($this->baseurl . '/templates/' . $this->template . '/js/jquery.js', 'text/javascript');
and added another on component below
$document = JFactory::getDocument();
$document->addScript($this->baseurl . '/templates/' . $this->template . '/js/validation.js');
but always js (validation.js) of component getting add before tempelete js(jquery.js)
How can i add component js(validation.js) after templete js(jquery.js).

If you are using jquery.js as main jQuery library on your project then you can simple include the jquery.js before the head module in index.php. like below.
<script src="templates/js/jquery.js" type="text/javascript"></script>
<jdoc:include type="head" />
Then in your component you can simply use same as below.
$document = JFactory::getDocument();
$document->addScript($this->baseurl . '/templates/' . $this->template . '/js/validation.js');
An alternate option is Addcustom tag , helps to adding scripts to the document.
$document = JFactory::getDocument();
$document->addCustomTag('<script src="'.$this->baseurl . '/templates/' . $this->template . '/js/validation.js" type="text/javascript"></script>');
Otherwise you have to load the component js also in index.php with second order but that will load the js file for all pages.
Hope its helps..

I am going through google and found one solution that i can use custom tag function for component like below:
$document = JFactory::getDocument();
$document->addCustomTag('<script src="'.$this->baseurl . '/templates/' . $this->template . '/js/validation.js" type="text/javascript"></script>');
and for template as it below:
$doc = JFactory::getDocument();
$doc->addScript($this->baseurl . '/templates/' . $this->template . '/js/jquery.js', 'text/javascript')

Related

Custom Base Url Codeigniter

in config.php , its possible to make another variable and call it in view
$root="http://".$_SERVER['HTTP_HOST'];
$root.=str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = $root;
$config['img_url']=$root.'assets/img/';
$config['jsctrl_url']=$root.'application/JsController/';
and call it like
<script type="text/javascript" src="<?php echo jsctrl_url();?>Account/register.js"></script>
What you need is a constant and not a function like base_url.
Head on to your config/constants.php and create a new one like:
$root ="http://".$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = $root;
define('IMG_URL', $root . 'assets/img/');
define('JS_URL', $root . 'application/JsController/');
Then in your views you would use it as a normal constant.
<script type="text/javascript" src="<?php echo JS_URL ?>Account/register.js"></script>

Laravel 5.1 Snappy pdf image not rendering in pdf file

I am using barryvdh/laravel-snappy to generate pdf file. I have two image files 1. yourlogohere.png is in public/image/ folder and 2. logo2.png is in folder other than public i.e. storage/app/logo and to get this file I defined a route (www.example.org/logo/logo.png) and use following code to access it.
public function logo($filename)
{
$file = Storage::disk('local_logo')->get($filename);
$mime = 'image/png';
return (new Response($file, 200))->header('Content-Type', $mime);
}
Problem:
When I use following code to generate pdf from the html containing the first file, pdf contains the yourlogohere.png image
$snappy = App::make('snappy.pdf');
$html='<img src="http://www.example.org/images/yourlogohere.png" class="img-responsive" alt="Your Logo Here">';
$snappy->generateFromHtml($html, $path,[],$overwrite = true);
But when I do exact same thing for the second file, pdf does not render the image.(When I open the link http://www.example.org/logo/logo2.png in browser I get the image). What am I missing?
$snappy = App::make('snappy.pdf');
$html='<img src="http://www.example.org/logo/logo2.png" class="img-responsive" alt="Your Logo Here">';
$snappy->generateFromHtml($html, $path,[],$overwrite = true);
Thanks,
K
You can also do:
<img src="data:image/jpeg;base64,
{{ base64_encode(#file_get_contents(url('your.image.url'))) }}">
I think I got the what the problem is, the route to access the image is via auth, even when user is logged in while accessing the snappy, the wkhtmltopdf exe runs in a shell that is totally different session. Now the right fix would be to be embed the image in the html that is sent to snappy instead of the link, Which I am not sure how I will do? Any suggestions welcome there.
Update:
I as able to convert the image to data:image/png;base64, and embed it in html.
$html = view('mytemplate.default', compact('variable1', 'variable2'))->render();
/*Convert logo image to base64 before pdf conversion*/
//search for <img src="http://example.org/mytemplate/logo/logo1.png">" and replace the src with data:image/png;base64,
$search = '/(<img\s+src=["\'])([^"\']+)(\/mytemplate\/logo\/)(.*)(\.)(.*?)(["\']\s+[^>]+>)/';
$html = preg_replace_callback($search, function ($matches) use ($invoicedetail) {
$filename = $matches[4] . $matches[5] . $matches[6];
$file = Storage::disk('local_logo')->get('yourlogohere.png');
$mime = "image/png";
$mytemplate = MyTemplate::where('logo_filename', '=', $filename)->first();
if (!empty($mytemplate)) {
$file = Storage::disk('local_logo')->get($mytemplate->logo_filename);
$mime = $mytemplate->logo_mime;
}
$base64 = 'data:' . $mime . ';base64,' . base64_encode($file);
return $matches[1] . $base64 . $matches[7];
}, $html);
$pdf_filename = 'template' . $mytemlpate->id . '.pdf';
$path = storage_path('app' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $filename);
$snappy = App::make('snappy.pdf');

Display Joomla component within a module

I'm trying to display a Joomla 2.5 component in a module.
In my module entrypoint I have:
JLoader::import('joomla.application.component.model');
JLoader::import( 'quizzes', JPATH_SITE . '/components/com_questions/models' );
JLoader::import('joomla.application.component.controller');
if (!class_exists('QuestionsControllerQuizzes')) {
require_once (JPATH_SITE . DS . 'components' . DS . 'com_questions/controllers' . DS . 'quizzes.php');
}
$quiz_controller = new QuestionsControllerQuizzes(false, $params->get('poll'));
$quiz_controller->execute( JRequest::getVar('task','load') );
I tried to debug it and it seems that can't load the correct view. Seems to looking for a generic com_content in $this->basePath.
The strange thing is that if I load the module in a page where the component is loaded, it display correctly.
Any idea how to succesfully display the component output in a module?
Try this: How to display Joomla component in a module
and remember your controller should include a function for the task!

Joomla routing engine

I have written a custom landing page that I want to incorporate to my website.
the website is using Joomla as a CMS.
so when I type a UTL www.mysite.com/myLandingPahe.php i get a 404 becuse the Joomla ruting engine kiks in and searches for the article in the DB..
this is the skeleton of the landing page (basic contac form):
<?php
$to = "somebody#example.com, somebodyelse#example.com";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <webmaster#example.com>' . "\r\n";
$headers .= 'Cc: myboss#example.com' . "\r\n";
mail($to,$subject,$message,$headers);
?>
can I reserve an address to point to my custom PHP file ?
Is there another way that this can be achieved ?
You have options,
Use com_contact with Overrides
copy the entire the components/com_contact/view/ to your templates/your_templates/html/com_contact/.
Then add your custom styles with following methods inside your view page
$doc = JFactory::getDocument();
$doc->addScript('js file full path');
$doc->addStyleSheet('css path');
It will load your custom styles Then you have to avoid header and footer for feeling it as a different page. this can be achieved by using appending tmpl=component with the url .
like your contact url is www.yourdomain.com/contact&tmpl=component.
Custom Page with Article
create an article page with any name and set the alias with required url. then add the form and its required CSS within the article content. Here you required some plugin for including styles and script inside your article content like this.
Then in your form action will be like follows.
<form action = "index.php?option=com_contact&task=contact.contactinfo">...</form>
This stands for inside your components/com_contact/controllers/contact.php have a function with contactinfo() when the form submit you will get all the post data inside this function.
for retrieving form data use Joomla default library like
$jinput = JFactory::getApplication()->input;
$name = $jinput->get('name', 'default_value', 'filter');
Also for sending email you can use.
$mail = JFactory::getMailer();
$mail->addRecipient($contact->email_to);
$mail->addReplyTo(array($email, $name));
$mail->setSender(array($mailfrom, $fromname));
$mail->setSubject($sitename.': '.$subject);
$mail->setBody($body);
$sent = $mail->Send();
after sending you can redirect back to the page with .
$this->setRedirect('url','message success/failed','type of message eg: error,info');
after setting this you can just append &tmpl=component like above for avoiding header and footer
Hope it make sense..

Including css files through getDocument doesn't work

I'm trying to include some css files through $document = JFactory::getDocument(); or through JHtml::stylesheet($stylesheet); but none of this works. Why it's not working ?
$document = JFactory::getDocument();
$reset = '/media/com_event/css/reset.css';
$document->addStyleSheet($reset );
JHtml::stylesheet($reset);
I'm adding this files in component's view.
You need to include the full path using JUri::root() like so:
$reset = JUri::root() . 'media/com_event/css/reset.css';
$document->addStyleSheet($reset);
Hope this helps
Include in your template's index.php
<jdoc:include type="head" />
And it will start working.

Resources