How to set up GoogleCharts with cakePHP? - cakephp

I'm trying to display data in cakePHP with a Google Chart.
I downloaded the GoogleCharts plugin on https://github.com/scottharwell/GoogleCharts and put it in my App/Plugin directory. I load all plugins in my application's bootstrap file with
CakePlugin::loadAll();
In my Controller, I put
App::uses('GoogleCharts', 'GoogleCharts.Lib');
and
public $helpers = array('GoogleCharts.GoogleCharts');
, so cakePHP is able to detect I will work with this plugin. Of course, I created the GoogleChartHelper.php in App/View/Helper.
Before working with my data, what I want is just to display an example of chart to see if it is working. Which is not! I copied the example of the above-provided link (on github.com), so here is my Controller class:
<?php
App::uses ( 'AppController', 'Controller');
App::uses ('GoogleCharts', 'GoogleCharts.Lib');
class StatisticsController extends AppController{
public $helpers = array('GoogleCharts.GoogleCharts');
public $components = array (
'Paginator',
'Session'
);
public function index(){
//Setup data for chart
$chart = new GoogleCharts();
$chart->type("LineChart");
//Options array holds all options for Chart API
$chart->options(array('title' => "Recent Scores"));
$chart->columns(array(
//Each column key should correspond to a field in your data array
'event_date' => array(
//Tells the chart what type of data this is
'type' => 'string',
//The chart label for this column
'label' => 'Date'
),
'score' => array(
'type' => 'number',
'label' => 'Score',
//Optional NumberFormat pattern
'format' => '#,###'
)
));
//You can also manually add rows:
$chart->addRow(array('event_date' => '1/1/2012', 'score' => 55));
//Set the chart for your view
$this->set(compact('chart'));
}
}
In my View, I put the code
<div id="chart_div"><?php $this->GoogleChart->createJsChart($chart);?></div>
(without "s" to "GoogleChart" (not like on the download page, where is it written "GoogleCharts"), which took me 3 hours to notice)
My charts should be displayed on the page, but I got the following error:
Warning (512): Method GoogleChartHelper::createJsChart does not exist
[CORE\Cake\View\Helper.php, line 192]
(and if I put the "s" in the View, my page displays like without any error but without any chart...)
Am I missing something?
N.B. : I didn't copy the first method given on the github page, because with it a new worst error appears:
Error: Call to a member function find() on null
That method is:
//Get data from model
//Get the last 10 rounds for score graph
$rounds = $this->Round->find(
'all',
array(
'conditions' => array(
'Round.user_id' => $this->Auth->user('id')
),
'order' => array('Round.event_date' => 'ASC'),
'limit' => 10,
'fields' => array(
'Round.score',
'Round.event_date'
)
)
);
Please help me, I just want to display some random data in a chart and it should not be complicated (but with cakePHP, everything seems complex)...

The problem is that one step was missing:
you have to write
<?php echo $this->fetch('script'); ?>
in View/Layouts/bootstrap.ctp and if you followed all other steps, it should work!

Related

How to add find query in cakephp2.4 while loading a component using fly

How to import components in cakephp on the fly.
I am importing the component in my controller using:
// -----------------------------
// load component and action
// -----------------------------
$this->ThingToDo = $this->Components->load($ComponentName);
$WhatToReturn = $this->ThingToDo->$ActionName();
I am not able to add the find condition in the component. I want to find the records from the plugin created. so to fetch the result I have include the model on the top of the function
var $uses = array('PaypalIpn.PaypalInstantPaymentNotification');
and following is the code i have written in component file:-
function processnotifcation(){
$fetchNotificationDetail = $this->PaypalInstantPaymentNotification->find('all', array(
'fields' => array(
'id',
'ProcessedTimeStamp',
'ProcessedComment',
'txn_type'
),
'order' => 'id desc',
'conditions' => array(
"ProcessedTimeStamp" => '0'
)
));
pr( $fetchNotificationDetail); die;
}
but it throws an error that find() is not a function.
Can anybody tell me how to use find() in component file?

CakeDC Ratings stars do now appear and data is not saved

I am trying to add CakeDC ratings to my CakePHP website. I have followed the instructions in readme provided with the plugin, but the rating stars do not appear. Also the ratings are not saved when I press 'Rate'. Here is the code from the view file:
<h3><?php echo ($post['Post']['title']); ?></h3>
<?php echo $this->Rating->display(array(
'item' => $post['Post']['id'],
'type' => 'radio',
'stars' => 5,
'value' => $post['Post']['rating'],
'createForm' => array('url' => array_merge($this -> passedArgs, array('rate' => $post['Post']['id'], 'redirect' => true)))));?></h3>
After pressing "Rate", the url changes to '.../rate:2/redirect:1' , but the data does not appear in the database. Am I missing something?
i also encountered this problem, but what i did was, i added this
public $components = array('Ratings.Ratings');
public $actsAs = array('Ratings.Ratable');
and after that, it managed to save the data

CakePHP (2.1) Media Plugin - Multi File Upload

I'm using the cakephp media plugin in my project using the monolithic style attachments table, i.e. all the attachments go in the one table with foreign_key, model, group etc. saved with the file details. So my model looks like:
class ProjectProfile extends AppModel {
var $name = 'ProjectProfile';
var $useDbConfig = 'default';
var $useTable = 'project_profiles';
var $actsAs = array('Media.Transfer', 'Media.Generator');
public $belongsTo = array(
'Project' => array(
'className' => 'Project',
'foreignKey' => 'pjID'
)
);
var $hasMany = array(
'Photo' => array(
'className' => 'Media.Attachment',
'order' => 'Photo.basename, Photo.id',
'foreignKey' => 'foreign_key',
'conditions' => array('Photo.model' => 'ProjectProfile', 'Photo.group' => 'Photo'),
'dependent' => true)
);
Then a saveAll in the controller when saving my record saves the attached file.
This all works fine, however I'd really like to be able to upload multiple files at once, which the plugin does support by doing this in the form:
echo $this->Form->hidden('Photo.0.model', array('value' => 'Photo'));
echo $this->Form->input('Photo.0.file', array('type' => 'file');
echo $this->Form->hidden('Photo.1.model', array('value' => 'Photo'));
echo $this->Form->input('Photo.1.file', array('type' => 'file');
echo $this->Form->hidden('Photo.2.model', array('value' => 'Photo'));
echo $this->Form->input('Photo.2.file', array('type' => 'file');
But I think you'd agree that's a bit cumbersome to have to click browse for each individual file. The simplist method I could see to to allow multiple file uploads was to use the HTML5 multiple file section option - http://bakery.cakephp.org/articles/veganista/2012/01/31/html_5_multiple_file_upload_with_cake :
echo $this->Form->input('files.', array('type' => 'file', 'multiple'));
This allows you to shift click in the file browser to select multiple files then puts the files into an array to save... however, this field format isn't handled by the media plugin. Also, there'd be no way to add the model, group etc. fields on the save as far as I could see.
So, does anybody know how I can handle multi file uploads with the media plugin using the monolithic model? I'm open to all suggestions.
Thanks in advance.
Will this CakePHP 2.x plugin - AjaxMultiUpload - work for you? I think that does exactly what you need it to.

Multiple paginations in one controller

Is there a possibility to use more than one $paginate variable in the same controller?
My problem is that I have an hasAndBelongsToMany-Relation between Links and Tags.
I have customized the pagination options so that it works with the habtm:
public $paginate = array(
'limit' => 25,
'joins' => array(
array(
'table' => 'links_tags',
'type' => 'inner',
'alias' => 'LinkTag',
'conditions' => array(
'Link.id = LinkTag.link_id'
)
)
));
It works fine, but there is another action which should be also paginated. In this action there is no tag available so every links is shown as often as there is an entry in the links_tags table for that link.
The easiest way to solve this problem is to use a second $paginate variable, but I did not found a solution how can I do that.
You can customise your paginate methods in various ways after the $paginate variable has been set, as per examples such as this one in the Cake Cookbook:
function list_recipes() {
$this->paginate = array(
'conditions' => array('Recipe.title LIKE' => 'a%'),
'limit' => 10
);
$data = $this->paginate('Recipe');
$this->set(compact('data'));
);
I haven't tested it out, but I would have thought you could put stuff like your join, which needs to vary from one action to another, in the $this->paginate call, rather than the $paginate variable. Hmm, I'll have to see if this works - could be very useful for me to know how to do this kind of thing, for later use!
According to the Cake Book pagination page (HERE), you can set different paging variables for different models (all within the same controller):
In fact, you can define more than one
set of pagination defaults in the
controller, you just name the pieces
of the array after the model you wish
to configure:
class RecipesController extends AppController {
var $paginate = array(
'Recipe' => array (...),
'Author' => array (...)
);
}

Creating multiple fixtures in cakePhp

I've been trying, but in vain, to create multiple fixtures in cakePhP. Currently, I need to test a function that requires two different fixture database tables. If possible, could someone please post some simple snippet and show me create multiple fixtures.
Furnace,
have you taken a look at cake's bake utility? You can bake fixtures with it.
cake bake fixture all
bakes all fixtures at once in your app. Use with care in order not to overwrite existing code.
Edit0:
Use
cake bake fixture help
if you have further questions.
Here's a simple fixture: app/tests/fixtures/entity_fixtures.php
In your case you will create two of these files, one per model and with different names.
<?php
class EntityFixture extends CakeTestFixture {
var $name = 'Entity';
var $table = 'entities';
// Define the fields
var $fields = array(
'entity_id' => array('type' => 'integer', 'key' => 'primary'),
'type' => array('type' => 'string' , 'length' => 255),
'created' => 'datetime',
'modified' => 'datetime'
);
var $records = array(
array('entity_id' => 1, 'type' => 'entity', 'created'=>'2010-01-01', 'modified' => '2010-01-01')
);
}
?>
When in your test case include your fixtures at the top of your test case
class EntityTestCase extends CakeTestCase {
var $fixtures = array(
'plugin.myplugin.entity', // Including a sample plugin fixture
'entity', // The fixture above shown in code above
'blogs' // Including a third fixture (should be in app/tests/fixtures/blog_fixture.php)
);
}

Resources