How will you add any java-script file or block to the front page only in drupal-7??
i know just one function function drupal_is_front_page()?? but it not helpful as per my requirements
<?php if ($if_front) { ?>
<script>......</script>
<?php } ?>
Not working
You can add this to your theme's template.php file:
function yourtheme_preprocess_page(&$vars) {
if(drupal_is_front_page()) {
drupal_add_js(drupal_get_path('theme', 'yourtheme') . '/js/your-front-script.js');
}
}
Related
I am using cakephp 2.4.1 for my project.I am having two tables casts and castimage. Now castimage having two columns cast id and cast_image_path. now i have to upload image with respective to cast id and store it in different folders according to cast ids? so how would i do that and how i store the image path in database?
You add in your view file
view.ctp
<?php if (!empty($this->data['Contact']['filepath'])): ?>
<div class="input">
<label>Uploaded File</label>
<?php
echo $this->Form->input('filepath', array('type'=>'hidden'));
echo $this->Html->link(basename($this->data['Contact']['filepath']), $this->data['Contact']['filepath']);
?>
</div>
<?php else: ?>
<?php echo $this->Form->input('filename',array(
'type' => 'file'
)); ?>
In your controller
public function add(){
if ($this->request->is('post')) {
// create
$this->Castimage->create();
// attempt to save
if ($this->Castimage->save($this->request->data)) {
$this->Session->setFlash('image has been successfully saved!');
$this->redirect(array('action' => 'index'));
// form validation failed
} else {
// check if file has been uploaded, if so get the file path
if (!empty($this->Castimage->data['Castimage']['filepath']) && is_string($this->Castimage->data['Castimage']['filepath'])) {
$this->request->data['Castimage']['filepath'] = $this->Castimage->data['Castimage']['filepath'];
}
}
}
}
This plugin will do what you want: https://github.com/josegonzalez/cakephp-upload
$cast_id = $this->request->data['Image']['cast_id];
if directory is not created, create new one
if(!is_dir($cast_id) {
$oldumask = umask(0);
mkdir($cast_id, 0777);
umask($oldumask);
}
$uniq = mt_rand();
move_uploaded_file($this->requset->data['Image']['tmp_name'],WWW_ROOT.DS.$cast_id.DS.$uniq.'.'$this->request->data['Image']['name']);
I hope it gives you some idea
EDIT:
I am getting the JSON results from the dropbox chooser, but they are not being parsed properly to be put into the database. I can't figure out what I am doing wrong, if I should be doing a JSON decoder in my controller or if it is something else.
Controller Code:
<?php class DropboxfilesController extends AppController {
public function add() {
if ($this->request->is('post')) {
$this->File->create();
if ($this->File->save($this->request->data)) {
$this->Session->setFlash(__('Your file is now available :)'));
$this->redirect($this->referer());
} else {
$this->Session->setFlash(__('Something went wrong!'));
}
}
}}?>
View Code:
<?php echo $this->Form->create('File'); ?>
<input type="Dropboxfilechooser" name="selected-file" style="visibility: hidden;"/>
<?php echo $this->Form->end('Finish'); ?>
Model Code:
<?php class File extends AppModel {}?>
The actual problem is looking at the source code, the form action is not going to the proper controller, it is just going to the cake/homes. Just change that and the problem is solved.
I am pretty new to cakephp, I have an uploader form and would like to pass all of the data using POST to the controller and model so that it will save to the database. So far there are no cakephp errors occuring but no data is being passed.
Controller Code:
<?php class Controller extends AppController {
public function add() {
if ($this->request->is('post')) {
$this->File->create();
if ($this->File->save($this->request->data)) {
$this->Session->setFlash(__('Your file has been liberated :)'));
$this->redirect($this->referer());
} else {
$this->Session->setFlash(__('Something went wrong!'));
}
}
}
}?>
View Code:
<?php echo $this->Form->create('File'); ?>
<input type="uploader" name="selected-file" style="visibility: hidden;"/>
<?php echo $this->Form->end('Finish'); ?>
Model Code:
<?php class File extends AppModel {}
?>
Please, check input specification you can see there, that there are no "uploader" value for type attribute. Maybe you mean type="file".
Also, if you want to upload files, please, use
echo $this->Form->create('File', array('type' => 'file'));
this will produce correct enctype attribute enctype="multipart/form-data". such enctype needed if you send files by POST.
Also, you use style: style="visibility: hidden;" which should hide file input field, so, maybe you use some flash uploader or ajax, but this is not demonstrated in your code. If you provide more code, we can provide more concrete help to you.
and counter question: how you debug your code?
I have tried many times to use this plugin and I failed.
I am following documentation, but it does not work for me.
I am posting the simple code here, to know what wrong I am doing.
1-I put this plugin in this folder app/plugins
2- I add TinyMce helper to articles_controller
<?php
class ArticlesController extends AppController {
// good practice to include the name variable
var $name = 'articles';
// load any helpers used in the views
var $helpers = array('Html', 'Form','TinyMce.TinyMce');
/**
* index()
* main index page of the formats page
* url: /formats/index
*/
function index(){
// get all formats from database where status = 1
$articles = $this->Article->find("all") ;
$this->set('articles', $articles);
}
function admin_add() {
// if the form data is not empty
if (!empty($this->data)) {
// initialise the format model
$this->Article->save($this->data);
// set a flash message
$this->Session->setFlash('The Format has been saved');
// redirect
$this->redirect(array('action'=>'index'));
} else {
// set a flash message
$this->Session->setFlash('The Format could not be saved. Please, try again.','default', array('class' => 'flash_bad'));
}
}
}
?>
3- in the view file articles/admin_add.ctp I added the editor
// i think the problem in this code
<?php $this->TinyMce->editor(array(
'theme' => 'advanced'
)); ?>
<div class="formats form">
<?php echo $form->create('Article');?>
<fieldset>
<legend>Add a article</legend>
<?php
// create the form inputs
echo $this->Form->input('title');
echo $this->Form->input('content'); ?>
</fieldset>
<?php echo $form->end('Add');?>
</div>
<ul class="actions">
<li><?php echo $html->link('List Articles', array('action'=>'index'));?></li>
</ul>
You need to put tinymce files into your js assets
Then you have to add into section of your layout.
Then you'll need to init tinymce according to example provided on tinymce website (ex: full tinymce layout) and configure it according to your requirements.
I'd personally would not rely on such cake plugins, when actions required to get things working are not many and they are simple enough.
I am using cakePHP 1.26.
The web page turned out blank when I tried to use requestAction to access a function in a COntroller from a .ctp.
Here is the code:
<?php
class TestingController extends AppController {
function hello($id=null){
$IfLoggedIn=$this->Session->check('user');
if($IfLoggedIn){
//search the database
//$result=doing something from the search results
$this->set('userInfo',$result);
return "2";
}
else if(!$IfLoggedIn && $id!=null){
return "1";
}
else if($id==null){
return "0";
}
}
}
and then in default.ctp file, I made use of the function defined above:
<?php
$u = $this->requestAction('/hello');
if($u=="2"){
echo "welcome back, my friend";
}
else{
echo "Hello World";
}
?>
But when I load a web page, it was blank page.
I have no idea what's wrong in the code.
Try to add
$u = $this->requestAction('/hello', array('return'=>true));
Check this
You might try including the controller in the url param of requestAction.
If you spend more time debugging and reading the manual, you'll learn more, more quickly.
I'm new to Cakephp myself, I'm using 2.0 which may be different in your version of Cake.
I found that the following code from the manual was wrong for me:
<?php
class PostsController extends AppController {
// ...
function index() {
$posts = $this->paginate();
if ($this->request->is('requested')) {
return $posts;
} else {
$this->set('posts', $posts);
}
}
}
You need a slight modification (seems the manual was wrong in this case). The following code worked for me:
<?php
class PostsController extends AppController {
// ...
function index() {
$posts = $this->paginate();
if ( !empty($this->request->params['requested']) ) { // line here is different
return $posts;
} else {
$this->set('posts', $posts);
}
}
}
We shouldn't be checking for the request HTTP verb, we should be checking if the request parameter is true.
Here's another relevant link to the manual about request parameters: http://book.cakephp.org/2.0/en/controllers/request-response.html#accessing-request-parameters
Hope that helps.