CakePHP 2.7 GoogleChart Plugin - cakephp

First of all, sorry if I'm asking for help regarding this question but I have been working on this for almost a day now. I searched this site and was able to find a post similar to my question but I cannot get it to work.
Anyway, I am using CakePHP 2.x, and I need to show reports via charts. I saw this plugin for CakePHP CakePHP GoogleCharts Plugin by Scott Harwell. I did everything step-by-step and somehow my views keep on appearing empty. I made sure it wasn't because of conflicting files so I decided to make a new CakePHP project, unfortunately, it didn't work.
Here are my codes:
Controller:
<?php
App::uses('AppController', 'Controller');
App::uses('GoogleCharts', 'GoogleCharts.Lib');
class ChartsController extends AppController {
public $uses = array('Student');
public $helpers = array('GoogleCharts.GoogleCharts');
public function test_chart(){
$student= $this->Student->getStudentAges();
$studentAgesChart= new GoogleCharts();
$studentAgesChart->type('LineChart');
$studentAgesChart->options(array('title' => "Student Ages"));
$studentAgesChart->columns(array(
//Each column key should correspond to a field in your data array
'name' => array(
'type' => 'string',
'label' => 'Student Name'
),
'age' => array(
'type' => 'number',
'label' => 'Student Age'
)
));
foreach($student as $row){
$studentAgesChart->addRow(array('age' => $row['Student']['age'], 'name' => $row['Student']['name']));
}
$this->set(compact('studentAgesChart'));
debug($studentAgesChart);
}
}
?>
View:
<div id="chart_div" >
<?php
$this->GoogleCharts->createJsChart($studentAgesChart);
?>
</div>
Debug($studentAgesChart):
object(GoogleCharts) {
[private] type => 'LineChart'
[private] columns => array(
'name' => array(
'type' => 'string',
'label' => 'Student Name'
),
'age' => array(
'type' => 'number',
'label' => 'Student Age'
)
)
[private] rows => array(
(int) 0 => array(
(int) 0 => 'Student 1',
(int) 1 => '17'
),
(int) 1 => array(
(int) 0 => 'Student 2',
(int) 1 => '16'
)
)
[private] options => array(
'width' => (int) 400,
'height' => (int) 300,
'title' => 'Student Ages',
'titleTextStyle' => array(
'color' => 'red'
)
)
[private] callbacks => array()
[private] div => 'chart_div'
}
Model:
public function getStudentAges(){
$student_ages = $this->find('all',
array(
'order' => array('Student.name' => 'ASC'),
'limit' => 3,
'fields' => array(
'Student.name',
'Student.age'
)
)
);
return $student_ages;
}
The way I see it, it doesn't contain any errors but my view is empty.

#Scrappy, do you see any output (JavaScript or HTML) included in the DOM when you view source on your page? Or, is the plugin not generating any content at all? If you do see JavaScript code added, is your page reporting JS errors in the console that might prevent the chart from loading properly? If you do not see the JS code, then you likely have not loaded the plugin in your bootstrap file.
Is the page available on the public Internet for review?
The plugin has not seen updates from me in a few years, but there are a few developers that have forked it and updated it for CakePHP 3.0 too. But, this version should still be working unless something changed on the Google end, which I am sure I would have heard about if that was the case.

Related

How to access a related table field from a Controller?

I am trying to get an array structure of a database and a few of its fields from my controller.
The fields I want are: Document.id, Document.name, Document.submission_date, and the Requester.name which is joined with: Requester.id = Document.requester_id
In my Controller:
Method A:
$documents = $this->Document->find('list', array('fields' => array('Document.name', 'Requester.name', 'Document.submission_date')));
Can't seem to find 'Requester.name'
Method B:
$documents = $this->Document->find('list', array('fields' => array('Document.name', 'Requester.name', 'Document.submission_date'), 'recursive' => 0));
Gives me:
array(
'2012-08-17' => array(
'Document_A' => 'Requester_Z'
),
'2012-08-05' => array(
'Document_B' => 'Requester_Y'
),
'2012-07-09' => array(
'Document_C' => 'Requester_X'
)
)
But I need it to be in format:
array(
(int) 0 => array(
'id' => '16'
'submission_date' => '2012-08-17'
'name' => 'Document_A',
'requester_name' => 'Requester_Z'
),
(int) 1 => array(
'id' => '41'
'submission_date' => '2012-08-05'
'name' => 'Document_B',
'requester_name' => 'Requester_Y'
),
(int) 2 => array(
'id' => '213'
'submission_date' => '2012-07-09'
'name' => 'Document_C',
'requester_name' => 'Requester_X'
),
)
I can't seem to figure it out after going through the 2.0 CakeBook and on StackOverflow...
Any help would be appreciated? Sorry - I'm still a n00b with CakePHP (but REALLY loving it so far!) Thanks!
You need to do a find('all') instead of a find('list') and you can use the fields key like you already are to limit the amount of info that's returned.
It won't come back exactly as you need the data, each field will be in a key of the model it belongs to - if you really need it in that format you can get away with using a virtual field, a custom find, or an afterFind callback to modify the data.
Find list is generally for an id => label formatted array.
You can try this:
$documents = $this
->Document-
>find('all',
array(
'fields' => array('Document.name', 'Requester.name', 'Document.submission_date'),
'joins' => array(
'table' => 'requesters',
'alias' => 'Requester',
'type' => 'left',
'conditions' => array('Requester.id = Document.requester_id')
)
)
);

cakedc search plugin use pb

I use the plugin cakedc search, but there is one thing I can not seem to find, I have a form with two fields, and I would add for example "online = 1" to my search
in my controller, I trying:
public function find() {
$this->Prg->commonProcess();
$array = array('type'=>'post', 'online'=>'1', 'created'=> '<= NOW()');
$this->passedArgs = Set::merge($this->passedArgs, $array);
$this->paginate = array(
'conditions' => array($this->Post->parseCriteria($this->passedArgs)),
);
$this->set('posts', $this->paginate());
model:
public $displayField = array('name', 'category_id');
public $filterArgs = array(
array('name' => 'name', 'type' => 'query', 'method' => 'filterName'),
array('name' => 'category_id', 'type' => 'value')
);
.....
debug($this->passedArgs):
array(
'name' => 'mon',
'category_id' => '2',
'type' => 'post',
'online' => '1',
'created' => '<= NOW()'
)
but $array argument are not taken in my search
Can anyone help?
I am a beginner with cakephp,
Thanks a lot !
Instead of
'conditions' => array($this->Post->parseCriteria($this->passedArgs)),
try
'conditions' => $this->Post->parseCriteria($this->passedArgs),
I also recommend you to use DebugKit and to add the generated query to your question in the case it is still not working.

Saving data array in AppModel with CakePHP

I have this function in CakePHP's AppModel (This is used to install initial data in CakePHP) However, I can't seem to get it to save my data, and I get no errors.
Here is my function inside App/Model/AppModel.php:
public function importData() {
$initialOptionData = array(
array( 'Option' => array( 'name' => 'version', 'value' => '1.0.0', )),
array( 'Option' => array( 'name' => 'site-name', 'value' => 'Site Title', )),
);
$this->create();
$this->saveMany($initialOptionData);
}
From you posted code it seems you're trying to save you data to options table, and to do that you need to use Option model.
But you're code is within AppModel, so first import Option model and then execute your save statements.
Your code should look like:
public function importData() {
$initialOptionData = array(
array( 'Option' => array( 'name' => 'version', 'value' => '1.0.0', )),
array( 'Option' => array( 'name' => 'site-name', 'value' => 'Site Title', )),
);
App::import('model','Option'); // Import the Option Model
$Option = new Option(); // create instance of Option class
// save statements
$Option->create();
$Option->saveMany($initialOptionData);
}
Note
Code you're trying will work if you write that within app/model/Option.php file.
I suppose it would be enough to store the values as model property. No need to call $this->save...
public $initialOptionData = array(
array( 'Option' => array( 'name' => 'version', 'value' => '1.0.0', )),
array( 'Option' => array( 'name' => 'site-name', 'value' => 'Site Title', )),
);
all models will inherit initialOptionData...

Drupal allowed_values_function does not get called when creating a field

For some reason my allowed_values_function never gets called when showing a field on a user bundle. Code:
function get_business_units()
{
$options = entity_load('business_unit', FALSE, NULL, FALSE);
$opt = bu_to_list_values($options);
return $opt;
}
function MYMODULE_enable()
{
if (!field_info_field('field_user_business_unit')) {
$field = array(
'field_name' => 'field_user_business_unit',
'type' => 'text',
'settings' => array(
'allowed_values' => array(),
'allowed_values_function' => 'get_business_units',
)
);
field_create_field($field);
// Create the instance on the bundle.
$instance = array(
'field_name' => 'field_user_business_unit',
'entity_type' => 'user',
'label' => 'Business Unit',
'bundle' => 'user',
'required' => FALSE,
'settings' => array(
'user_register_form' => 1,
),
'widget' => array(
'type' => 'options_select',
),
);
field_create_instance($instance);
}
}
The field is created, and even displayed on the users "edit" page when editing their info. But the only value is "Select" or "None". My method is never called (I even placed a debug point). This is all in MYMODULE.install file.
The problem is: 'type' => 'text'.
You have to use: 'type' => 'list_text'.
Allowed values is meaningless for a text type.
Your get_business_units() function needs to be in the MYMODULE.module file; the .install files aren't included in a normal Drupal bootstrap.
Have you tried
drush features-revert MYMODULE ?

Cakephp Error using Miles J Uploader plugin

I am currently trying to use Miles J plugin located here http://milesj.me/code/cakephp/uploader Although I have made great progress learning CakePhp, I am currently having a problem using the plugin and I would appreciate any help provided.
I have followed all the necessary steps to use the plugin. It has been downloaded put on Plugin folder, I bootstrapped with CakePlugin::loadAll().
So far so good.
Next I have proceed to set up the table as indicated by the plugin developer.
Ok, now back to my own code. I have the following set up:
images_controller.php , image.php and their views.
My goal now is to use the plugin inside those files as such:
App::import('Vendor', 'Uploader.Uploader');
Class ImagesController extends AppController {
var $components = array('Auth');
var $helpers = array('Design');
var $uses = array('Image', 'Uploader.Upload');
function manage(){
//here I show a simple upload form that uses the action saveimage
}
function saveimage(){
$this->Uploader = new Uploader();
if(!empty($this->data)){
$this->Upload->save($this->data);
}
}
}
Now, my model is set as follows
Class Image extends AppModel {
public $useTable = 'uploads';
public $actsAs = array(
'Uploader.FileValidation' => array(
'file' => array(
'extension' => array(
'value' => array('gif', 'jpg', 'jpeg'),
'error' => 'Only gif, jpg and jpeg images are allowed!'
),
'minWidth' => 500,
'minHeight' => 500,
'required' => true
),
'import' => array(
'required' => false
)
),
'Uploader.Attachment' => array(
'file' => array(
'name' => 'uploaderFilename',
'uploadDir' => '/files/uploads/',
'dbColumn' => 'path',
'maxNameLength' => 30,
'overwrite' => true,
'stopSave' => false,
'transforms' => array(
// Save additional images in the databases after transforming
array(
'method' => 'resize',
'width' => 100,
'height' => 100,
'dbColumn' => 'path_alt'
)
),
'metaColumns' => array(
'size' => 'filesize', // The size value will be saved to the filesize column
'type' => 'type' // And the same for the mimetype
)
),
'import' => array(
'uploadDir' => '/files/uploads/',
'name' => 'uploaderFilename',
'dbColumn' => 'path',
'overwrite' => true,
'stopSave' => false,
'transforms' => array(
array(
'method' => 'scale',
'percent' => .5,
'dbColumn' => 'path' // Overwrite the original image
)
)
)
)
);
}
}
On my model for testing purposes I have not changed anything but just copied and pasted the very same array as shown in the the Test/Model folder inside the plugin, which is meant to show the functionality of the plugin.
The following confusion, errors or lack of understanding is taking place:
My file is not being uploaded to the webroot/files/uploads folder
My data is being inserted in the database, but not in a complete manner by leaving empty as shown:
id | caption | path | path_alt | created |
4 | | | |2012:02:..|
Above, I expect the path to be saved, but it doesn't.
I have to admit my confusion comes mainly from my inexperience using plugins, so I am aware I might be doing something wrong regarding my models or my configuration.
Any prompt help would be appreciated greatly as I have tried to work this out on my own without any success.
A few things:
1 - In your controller you do not need to import the Uploader class. You also don't need to use the Uploader.Upload model (it's merely an example test case). All you need to do in the controller is call $this->Image->save() which will upload the file and save the path as a row into the database (if you defined the Attachment in Image).
2 - In your view, create the file input. Pay attention to the input name.
echo $this->Form->input('FILE_INPUT_NAME', array('type' => 'file'));
3 - In your Image model, setup the AttachmentBehavior and its options for that specific input field:
'Uploader.Attachment' => array(
'FILE_INPUT_NAME' => array(
'uploadDir' => '/files/uploads/',
'dbColumn' => 'path'
),
'ANOTHER_INPUT' => array()
);
Be sure that the column "path" exists in your images table. And thats it.
For more information on what each option does, check out the following: http://milesj.me/code/cakephp/uploader#uploading-files-through-the-model

Resources