I don't get chosen-cakephp it working in simple application - cakephp

I am new to CakePHP, MVC and web developing in general (JQuery, etc), altough I have more than 15 years programming non web applications and some little knowledge of PHP.
In order to learn, I am developing a simple school project containing 3 tables (courses, professors, courses_professors).
I have "baked all" and got a nice CRUD application which works perfectly.
Now I am trying to improve select boxes userfriendliness provided by harvesthq/chosen by using Chosen-CakePHP.
I have followed the instructions for installation and setup of the plugin at https://github.com/paulredmond/chosen-cakephp, but I am not sure about what more steps should I follow in order to make it work. Btw. I understand creating a custom class is optional.
To be true, I expected that modifing /app/Controller/AppController.php as explained in would be enough to see the new select boxes working, but the application works just as it did before adding the plugin.
The code contained in /app/Controller/AppController.php is the following (comments removed):
App::uses('Controller', 'Controller');
class AppController extends Controller {
public $helpers = array('Chosen.Chosen');
}
app/Config/bootstrap.php contains:
./...
CakePlugin::load('DebugKit');
CakePlugin::load('Chosen');
./...
I have been googling for 2 days, but I don't get the idea.
Do I need to modify any code generated by bake? Do I need to modify anyother files?
I guess my lack of knowledge in the field is quite relevant. Should you reccomend me an essential previous reading or exercise, any reccomendation is welcome.
Any indications would be appreciated.
Thanks for your help,
ivan

I have never work with the plugin you mention, but look at the examples in the link you provided
(in the view)
echo $this->Chosen->select(
'Article.category_id',
array(1 => 'Category 1', 2 => 'Category 2'),
array('data-placeholder' => 'Pick categories...', 'multiple' => true)
);
The helper used is not $this->Form->input, as it comes with baking, it's $this->Chosen->select. So my guess is "yes, you need to modify the code generated by bake".
Try that, and if it doesn't work, update your question with the code you tried for the view and if it gives you any (new) error.

You don't really need a plugin to get such a small functionality for your forms.
First you need to download a Chosen package.
Open your webroot folder and put there all dependent js, css files that comes with package
Open default layout file app/Views/Layouts/default.php (if you "baked all" that should be default layout file)
Read about inserting js, and css files into your layout here Html Helper
Syntax is simple:
This code:
echo $this->Html->css('forms');
Outputs:
<link rel="stylesheet" type="text/css" href="/css/forms.css" />
Similar for js files..
Add this code to layout footer
$(".chzn-select").chosen();
or header (but then you need to wrap it with document.ready ;
In your baked view files add class .chzn-select to select form elements
echo $this->Form->input('Model.field', array(
'type' => 'select',
'multiple' => true,
'class' => 'chzn-select'
));
If you just started learning, it's to early to just "bake all"

Related

Creating a view (cakePHP noob)

I've been reading until my eyes are swollen, and having troubles finding what should be a simple answer.
I am not new to PHP, but I am new to cakePHP. Please bear with me, and have patience with my ignorance of lack of knowledge of the terminology.
I have been asked to help make some fixes on a cakePHP developed site, that was recently created.
The site is missing a page for "http://domain.com/logout". I can see the functions I need to access in the UserController, but I am not sure where to put the .ctp file to generate the view.
Let's just say I want the logout.ctl file to be something as simple as:
echo "Hello World";
Under my app/View folder I have the sub-folders Home, & User that I have tried to place this file into. I am assuming I have to do something else as well, but I have not been able to locate what that is.
Any assistance is appreciated. Thanks for reading!
1.By default, you should link your view and controller together by creating Views/Controller/action.ctp.
Since the url is linked to Controller by routes, views are not associated with it directly.
For example, if you have set
Router::connect('/logout/', array('controller' => 'User', 'action' => 'logout'));
, then you may want to create Views/User/logout.ctp.
If you have set
Router::connect('/logout/', array('controller' => 'Home', 'action' => 'logout'));
, then you may want to create Views/Home/logout.ctp.
2.You can change the view in your action with $this->view='sample' or $this->render('sample'); and then create the view file with name sample.ctp.
3.You can also read a view of another folder with $this->render('/Sample/logout');.
Reference: http://book.cakephp.org/2.0/en/controllers.html
4.If you use themes $this->theme = 'Example';, the default view file will be set to /app/View/Themed/Example/Posts/edit.ctp.
Reference: http://book.cakephp.org/2.0/en/views/themes.html
5.I think the default extension of cakephp view files is .ctp but not .ctl. .ctl is used by Microsoft Visual Studio? I'm not quite sure.

CakePHP default layout for xlsx

I have a method which exports riport data into an xlsx file in my CakePHP 2 project. It is working well. In the same time it seems it just ignores anything what I put into View/Layouts/xlsx/default.ctp
In my routes.php I have
Router::parseExtensions('json', 'xlsx');
In my controller I have
public $components = array('RequestHandler');
My View/Riports/xlsx/export.ctp is rendered, but View/Layouts/xlsx/default.ctp is ignored.
What do I miss?
I think you're out of luck here. From the documentation;
The data view classes don’t support layouts. They assume that the view file will output the serialized content.
http://book.cakephp.org/2.0/en/views/json-and-xml-views.html#using-a-data-view-with-view-files
But I may be wrong of course :)

Embed a Cakephp form into Wordpress

I have a Wordpress blog and I'm developing a backend application using Cakephp.
I want to develop the blog's contact form using cake (since the entered information will be available from the backend app).
Right now, I tried including the cake view into wp using ajax. The problem with this approach is that I either use a Js->submit, which makes attaching files to the form quite complicated, or I use a Form->submit, which makes displaying validation errors problematic. Also, it creates problems with the recaptcha plugin not showing up.
Is there a way of integrating the form using php? I don't need authentication (it is a public form), but I need to be able to show validation errors on the form and upload files on the form.
Actually, you can load any website into a string using CURL, if you load en empty page with a placeholder from your wordpress, you can then use it as your layout
<?php
$url = "http://www.yourdomain.com/emptypage";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($curl);
curl_close($curl)
$placeholder = "{{CONTENT}}"
$placeholderPos = strpos($output,$placeholder);
$beginning = substr($output,0,$placeholderPos);
$end = substr($output,$placeholderPos+strlen($placeholder));
echo $beginning;
////// your form //////
echo $end;
?>
You may have to deal with relative path after that, but this should get you started.
So, I finally found a way I think is the best since it uses cake classes.
I created a file new_file.php on webroot that is basically a copy of index.php but instead of using a generic request it requests the specific page I'm looking for:
$Dispatcher = new Dispatcher();
$Dispatcher->dispatch(new CakeRequest('CONTROLLER/ACTION'), new CakeResponse(array('charset' => Configure::read('App.encoding'))));
Of course, you'll have to change 'CONTROLLER/ACTION' to whatever your controller is.
After that, you only have to include the file in a WordPress plugin.
There is only one more change. There are some methods that conflict with WP declarations. PHP will complain when you include the above file. You have to find those method and wrap them into an if to make sure the method is not redeclared. This can break some functionality like localization (function __()) but it is ok if what you are including is not a very complex cake application.
if (!function_exists('methodName')) {
Hopefully the last issue will be solved once Cake 3 is out with namespaces support.

Drupal 7 not using the template suggestion

I have added the following to my template.php file, in the [themename]_preprocess_page function:
<?php
if ($variables['is_front'])
{
$variables['theme_hook_suggestions'] = array();
$variables['theme_hook_suggestions'][] = 'page__index';
}
if (isset($variables['node'])) {
// If the node type is "blog" the template suggestion will be "page--blog.tpl.php".
$variables['theme_hook_suggestions'][] = 'page__'. str_replace('_', '--', $variables['node']->type);
}
If I run a var_dump on the $variables array, I can see that, on my front page, the 'theme_hook_suggestions' is set to only use 'page__index'. I have a file named 'page--index.tpl.php'. Drupal still uses page.tpl.php.
I also commented out the code above, and renamed the file to 'page--front.tpl.php' and it still used page.tpl.php. I clear the caches after every change.
What am I missing?
Edit: To help clarify, I want to override the entire design of the page for the front - no columns or sidebars, different graphics, different backgrounds on some of the div's, etc. I don't want to override the 'node--' template files (yet).
In the end, I need to have a static front page with a different design than the rest of the site, then a custom node template for each content type.
I worked with the awesome folks on the #drupal IRC channel and found the issue. Not sure if this is a bug in the code or on purpose - but you cannot use the word 'index' for those theme suggestions. I changed the content type's name to 'homepage' and voila!

How to do form-based file uploads in CakePHP?

I have been looking into this for a while and can't figure it out. Basically I have an add page for my model which you can add a map from a URL or from a file upload. I have got all the fields and validation in but how and where do I manage the uploaded file?? There must be some easy way to do this. Thanks!
Firstly your form needs to be set up to allow file uploads.
<?php echo $form->create(Model, array('type' => 'file')); ?>
This will allow any file inputs to actually upload the file to your server with $form->file(field) or $form->input(field, array('type' => 'file')).
Once the file has been uploaded you should handle everything else from within the Model:
function beforeSave($created) {
extract($this->data[Model][field]);
if ($size && !$error) {
move_uploaded_file($tmp_name, destination);
$this->data[Model][field] = destination;
}
return true;
}
These are only the basics, so be sure to have a play around to find the solution that best fits your needs.
You can use Zend Components to handle the file upload. There is a good example here on my website:
CakePHP file upload using Zend Components
NOTE: MeioUploadBehavior has been deprecated. Instead jrbasso suggests the Upload Plugin.
In addition to the fine answers already given I want to hint about MeioUploadBehavior, currently maintained by jrbasso at github, which has been a great help for me in my own CakePHP project.
You simply add the behavior to your model using the $actsAs field and at the same time specifying any custom preferences. Then create necessary fields (described by supplied docs in detail) in your database, or configure the model to not use any database table. Finally setup the form in your add page, also described in the supplied documentation. The behavior will then take care of the rest for you.

Resources