I am new to CakePHP, and would like to create a calendar using this framework. I am having a difficult time, and am wondering if there is a tutorial or guide on how to create a simple calendar using CakePHP?
Here are two links: LINK1(calendar helper) and LINK2 to a implementation of FullCalendar.
I did not try both of them...
https://github.com/silasmontgomery/CakePHP-Full-Calendar-Plugin
This is a calendar plugin for cakephp via fullCalendar
Download js or css file
http://fullcalendar.io/download/
Controller code
function feeds(){
$this->layout = 'ajax';
if(isset($this->params->query['start'])){
$start = $this->params->query['start'];
}
if(isset($this->params->query['end'])){
$end = $this->params->query['end'];
}
$events = $this->{$this->modelClass}->find('all',array('conditions' => array('startdate >=' => $start,'enddate $end)));
$data = '';
foreach($events as $res ){
$data[] = array(
'id' => $res[$this->modelClass]['id'],
'title'=> $res[$this->modelClass]['title'],
'start'=> Date('Y-m-d H:m',$res[$this->modelClass]['startdate']),
'end' => Date('Y-m-d H:m',$res[$this->modelClass]['enddate']),
'start_time' => Date('h:ia',$res[$this->modelClass]['startdate']),
'end_time' => Date('h:ia',$res[$this->modelClass]['enddate'])
);
}
echo json_encode($data);
exit;
}
View file
Add this in .ctp file
<div class="" id="calendar_div">
Js code for view file
$('#calendar_div').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'agendaDay,agendaWeek,month'
},
defaultView: 'month',
events: '<?php echo $this->Html->url(array('action' => 'feeds')); ?>',
selectable: true,
selectHelper: true
});
Related
I canĀ“t get CakePdf to work, If I would get an error, I could work with it. But nothing really shows up.
CakePhp: 3.1.6
CakePdf: current Version
I added an InvoicesController with this code
<?php
namespace App\Controller;
class InvoicesController extends AppController
{
// In your Invoices controller you could set additional configs, or override the global ones:
public function view($id)
{
$this->pdfConfig = array(
'orientation' => 'landscape',
'download' => true,
'filename' => 'invoucne.pdf'
);
}
}
?>
Also added this to my bootstrap.php
use Cake\Event\EventManager;
EventManager::instance()
->on(
'Controller.initialize',
function (Cake\Event\Event $event) {
$controller = $event->subject();
if ($controller->components()->has('RequestHandler')) {
$controller->RequestHandler->config('viewClassMap.pdf', 'CakePdf.Pdf');
}
}
);
Plugin::load('CakePdf', ['bootstrap' => true, 'routes' => true]);
Configure::write('CakePdf', [
'engine' => 'CakePdf.dompdf',
'margin' => [
'bottom' => 15,
'left' => 50,
'right' => 30,
'top' => 45
],
'orientation' => 'landscape',
'download' => true
]);
This to my routes.php
Router::extensions(['pdf']);
Also I go my default.ctp in the src/Template/Layout/pdf
<h2>Rendered with default layout</h2>
<?php echo $this->fetch('content'); ?>
And my views in my Template/Invoices/pdf
<html>
<body>
<h1>test</h1>
</body>
</html>
My url looks like:
http://localhost/caketest/invoices/view/1.pdf
I installed it with composer and my plugins lie in vendor/dompdf and vendor/friendsofcake/cakepdf
I had this problem before the solution is to put
Router::extensions(['pdf']);
before
Router::scope('/', function ($routes) { //some code}
not after it
My form using custom module is not appearing on my contact us page.I created my .module file and .info file and then active the module as well. Following is the code I had included in my .module file
function custom_form_module_form($form,&$form_state) {
$form['name'] = array(
'#type' => 'textfield',
);
$form['company'] = array(
'#type' => 'textfield',
);
$form['phone'] = array(
'#type' => 'textfield',
);
$form['email'] = array(
'#type' => 'textfield',
);
$form['message'] = array(
'#type' => 'textfield',
);
return $form;
}
and in my template.php file
function custom_form_module_form_theme($existing, $type, $theme, $path) {
$items['custom_form_module_form'] = array(
'render element' => 'form',
'template' => 'page--contact-us',
'path' => drupal_get_path('theme', 'escorts').'/templates');
return $items;
}
and in my page--contact-us.tpl.php I used the following line to call separate fields of a form but its not working.
<?php echo drupal_render($form['name']); ?>
"here custom_form_module=my module name and page--contact-us.tpl.php=my template file and escorts=my theme name"
To print the form in the contact page, you may not use template.php.
Simply:
$my_form = drupal_get_form('custom_form_module_form');
print drupal_render($my_form);
Update
Change the form markup from from your template.php
function theme_my_form_1($form) {
$vars['element'] = $form;
$markup = '';
$markup .= '<div id="form-name-wrapper">'
. drupal_render($form['name'])
. '</div>';
// continue altering the markup
$vars['element']['#children'] = $output;
return theme_form($vars);
}
Then, in your .tpl.php file:
$my_form = drupal_get_form('custom_form_module_form');
print theme_my_form_1($my_form);
Please help I am making a wp plugin but my jQuery code does not work properly in loop that's why I can't make attribute in this plugin.
Here is the code please help me. Thanks in advance :)
function smooth_ticker_list_shortcode($atts){
extract( shortcode_atts( array(
'text' => 'Latest News',
'count' => '2',
'efect' => 'fade',
'color' => '#000',
'speed' => '2500',
'delay' => '1000',
), $atts, 'projects' ) );
$q = new WP_Query(
array('posts_per_page' => $count, 'post_type' => 'post')
);
$list = '
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery("#smoothticker").inewsticker({
speed: '.$speed.',
effect: '.$efect.',
color: '.$color.',
delay_after:'.$delay.',
});
});
</script>
<div><span>'.$text.'</span><ul id="smoothticker">';
while($q->have_posts()) : $q->the_post();
$idd = get_the_ID();
$list .= '
<li>'.get_the_title().'</li>
';
endwhile;
$list.= '</ul></div>';
wp_reset_query();
return $list;
}
add_shortcode('smooth_ticker', 'smooth_ticker_list_shortcode');
I set the FullCalendar plugin in CakePHP, i can see it, it's displayed, i'm trying to put the events from my PHP Controller (the view admin feed) to my Full Calendar Plugin . I don't know what's wrong , no events are displayed in my calendar . I will be thankfull if someone can find why my calandar is empty .
Look the screen, this is what i have in my console :
The answer is NULL .
The JS file :
// JavaScript Document
$(document).ready(function() {
$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
defaultView: 'agendaWeek',
firstHour: 8,
weekMode: 'variable',
aspectRatio: 2,
editable: true,
events: {
url: FcRoot + '/events/feed',
color: 'yellow', // an option!
textColor: 'black', // an option!
},
eventRender: function(event, element) {
///
}
});
});
EventsController
// The feed action is called from JS to get the list of events (JSON)
public function admin_feed($id=null) {
$this->layout = "ajax";
$vars = $this->params['url'];
$conditions = array('conditions' => array('UNIX_TIMESTAMP(start) >=' => $vars['start'], 'UNIX_TIMESTAMP(start) <=' => $vars['end']));
$events = $this->Event->find('all', $conditions);
foreach($events as $event) {
if($event['Event']['all_day'] == 1) {
$allday = true;
$end = $event['Event']['start'];
} else {
$allday = false;
$end = $event['Event']['end'];
}
$data[] = array(
'id' => $event['Event']['id'],
'title'=>$event['Event']['title'],
'start'=>$event['Event']['start'],
'end' => $end
);
}
$this->set("json", json_encode($data));
}
In View/Events/admin_feed
<?php
echo $json;
?>
The response now :
You should really take advantage of routing and views for this. It allows you to properly organize your code and keep things DRY.
public function admin_feed($id=null) {
$vars = $this->params['url'];
$conditions = array('conditions' => array('UNIX_TIMESTAMP(start) >=' => $vars['start'], 'UNIX_TIMESTAMP(start) <=' => $vars['end']));
$events = $this->Event->find('all', $conditions);
foreach($events as $event) {
if($event['Event']['all_day'] == 1) {
$allday = true;
$end = $event['Event']['start'];
} else {
$allday = false;
$end = $event['Event']['end'];
}
$data[] = array(
'id' => $event['Event']['id'],
'title'=>$event['Event']['title'],
'start'=>$event['Event']['start'],
'end' => $end
);
}
$this->set("events", $data);
}
Now, your admin_feed action is usable as more than just a json feed (even though that may be just want you want). This makes testing easier as well.
Add the following to your routes, to tell Cake that you want to allow the json extension:
Router::parseExtensions('json');
Then, add the RequestHandler to your controller components. This component will automatically switch to your json view and layout when a json extension is found.
Next, add a layout for all json views in /View/Layout/json/default.ctp:
<?php
echo $this->fetch('content');
Then, add your view in /View/Events/json/admin_feed.ctp:
<?php
echo json_encode($events);
That's it. Now, if you want to use admin_feed to view events in HTML, you can by adding a view for it.
Your full calendar url should now be: FcRoot + '/admin/events/feed.json'. Try just visiting it in the browser to see if you see the json.
do not set it, just echo it
echo json_encode($data);
set values are supposed to be used in view, in your case you are just 'returning' as ajax response
for ajax calls, view is not necessary at all, put $this->autoRender = false; in you function: this will prevent 'seeking' for view file.
I have a question about cakePHP. I create two drop down lists in my view. When the user changes the value in one list, I want the second to change. Currently, I have this working like this: An on click event fires when the user selects from list box one. This fires a jQuery ajax function that calls a function from my controller. This is all working fine, but how do I re-render my control, asynchronously (or, view)? I i know I could just serialize the array to json and then recreate the control in javascript, but there seems like there should be a more "CakePHP" way. Isn't that what render is for? Any help would be great. Here's the code I have so far:
jQuery:
function changeRole(getId){
$.ajax({
type: 'POST',
url: 'ResponsibilitiesRoles/getCurrentResp',
data: { roleId: getId },
cache: false,
dataType: 'HTML',
beforeSend: function(){
},
success: function (html){
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
}
});
View:
<?php
echo 'Roles:';
echo'<select name="myOptions" multiple="multiple">';
foreach ($rolesResponsibility as $role) {
echo' <option onclick="changeRole(this.value);" value="'; echo $role["ResponsibilitiesRole"]["role_id"]; echo '">'; echo $role["r"]["role_name"]; echo '</option>';
}
echo '</select>';
echo 'Responsbility:';
echo'<select name="myOptionsResp" multiple="multiple">';
foreach ($respResponsibility as $responsibility) {
echo' <option value="'; echo $responsibility["responsibility"]["id"]; echo '">'; echo $responsibility["responsibility"]["responsibility_name"]; echo '</option>';
}
echo '</select>';
?>
Controller function:
public function getCurrentResp(){
$getId = $this->request->data['roleId'];
$responsibilityResp = $this->ResponsibilitiesRole->find('all',
array("fields" => array('role.role_name','ResponsibilitiesRole.role_id','responsibility.*'),'joins' => array(
array(
'table' => 'responsibilities',
'alias' => 'responsibility',
'type' => 'left',
'foreignKey' => false,
'conditions'=> array('ResponsibilitiesRole.responsibility_id = responsibility.id')
),
array(
'table' => 'roles',
'alias' => 'role',
'type' => 'left',
'foreignKey' => false,
'conditions'=> array('ResponsibilitiesRole.role_id = role.id')
)
),
'conditions' => array ('ResponsibilitiesRole.role_id' => $getId),
));
$this->set('respResponsibility', $responsibilityResp);
//do something here to cause the control to be rendered, without have to refresh the whole page
}
The js event is change fired on the select tag and NOT click
You can use the Form Helper to build your form.
Pay attention Naming things following the cakephp way.
Because your code is a bit confused i will make other simple example:
Country hasMany City
User belongsTo Country
User belongsTo City
ModelName/TableName (fields)
Country/countries (id, name, ....)
City/cities (id, country_id, name, ....)
User/users (id, country_id, city_id, name, ....)
View/Users/add.ctp
<?php
echo $this->Form->create('User');
echo $this->Form->input('country_id');
echo $this->Form->input('city_id');
echo $this->Form->input('name');
echo $this->Form->end('Submit');
$this->Js->get('#UserCountryId')->event('change',
$this->Js->request(
array('controller' => 'countries', 'action' => 'get_cities'),
array(
'update' => '#UserCityId',
'async' => true,
'type' => 'json',
'dataExpression' => true,
'evalScripts' => true,
'data' => $this->Js->serializeForm(array('isForm' => false, 'inline' => true)),
)
)
);
echo $this->Js->writeBuffer();
?>
UsersController.php / add:
public function add(){
...
...
// populate selects with options
$this->set('countries', $this->User->Country->find('list'));
$this->set('cities', $this->User->City->find('list'));
}
CountriesController.php / get_cities:
public function get_cities(){
Configure::write('debug', 0);
$cities = array();
if(isset($this->request->query['data']['User']['country_id'])){
$cities = $this->Country->City->find('list', array(
'conditions' => array('City.country_id' => $this->request->query['data']['User']['country_id'])
));
}
$this->set('cities', $cities);
}
View/Cities/get_cities.ctp :
<?php
if(!empty($cities)){
foreach ($cities as $id => $name) {
?>
<option value="<?php echo $id; ?>"><?php echo $name; ?></option>
<?php
}
}
?>