Ok i have been looking for a while now... and i wasnt able to find nothing related to jquery and cakephp when loading content to a div using the helper $this->Js->link... so i decided to post my question here in this awesome site... my first question and its so silly (i guess).
So... mechanics works fine, i mean.. it loads the content into a div called "algo" but now i try to add some effects (fadein) and im not being able to find the correct syntaxys for this! im brand new with cake... just 1 month old using it.
How do i add "fadeIn" effect when showing this damn div? i have tried lots of things but so far nothing makes the div load content with fadein effect whatsoever...
echo $this->Js->link('Categoria 1', array('controller' => 'Categories', 'action' => 'categorias1'), array('update' => '#algo'));
this perfectly loads a bunch of pictures that i have on category 1 inside the div that im asking... what should i do to add fadeIn effect? i have added some crazy things to see if they work but... nope... no luck
i have even tried this crazy one:
echo $this->Js->link('Categoria 1', array('controller' => 'Categories', 'action' => 'categorias1'), array('update' => '#algo', array('effect' =>array('fadeIn', array('speed' => 'slow'), true))));
any help will be apreciated! nobody shows examples of divs with cakephp.. or at least i wasnt able to find one that suits my needs!
great site by the way! it helped me a lot all this years!
i finally manage myself to accomplish what i was trying to do and i want to share with you guys what i did:
echo $this->Js->link('categoria 1', array('controller' => 'Categories', 'action' => 'categorias1'),array('update' => '#categorias', 'evalscripts' => true, 'before' => $this->Js->get('#algo,#categorias2')->effect('fadeOut', array('buffer' => false)), 'complete' => $this->Js->get('#categorias')->effect('fadeIn', array('buffer' => false))));
This huge piece of code is doing what i was expecting to, i also have added some editing to my jquery helper in order to pass them more than one div so i can have them all refreshed at the same time.
if you guys want to hack your helper here is the code:
cake/libs/view/helpers/jquery_engine.php
find this line:
$success .= $this-jQueryObject . '("' . $options['update'] . '").html(data);';
and have it replaced with:
if(is_array($options['update'])){
$success .= 'var temp = ' . $this->jQueryObject . '(" <div/>").html(data);';
foreach($options['update'] as $divId){
$success .= $this->jQueryObject . '("' . $divId . '").html(' . $this->jQueryObject . '("' . $divId . '", temp).html());';
}
} else {
$success .= $this->jQueryObject . '("' . $options['update'] . '").html(data);'; //linea que ya estaba
}
//termina agregado
As suggested by Dunhamzzz, I write all my jQuery by hand and never use the helpers. There doesn't seem to be any point tbh.
One useful tip, pass the base URL and any other useful cake variables into your page as a javascript variable. Add this to your layout:
<?php
$baseUrl = Router::url('/');
echo $this->Html->scriptBlock(<<<EOJS
var baseUrl = '{$baseUrl}';
EOJS
); ?>
Related
First time user of stack overflow so if I am posting incorrectly please let me know. So my website is developed in cakePHP 3.0. I currently have articles on the site and would like a way for users to move from article to article. I should mention I had this working with the get() method but because of the way the my controller is written I need to use a select query with a condition. I am able to create an article and not post it until a future date. So this code is supposed to ignore anything that hasn't been published yet to users don't see those articles.
Super newbie with PHP in general let alone the cakePHP MVC framework so bear with me :)
In my controller within the public view function I have this:
//get story id for next and previous buttons
$todays_date = date('Y-m-d H:i:s');
$this->loadModel('Story');
$storyID = $story->id;
$storyNextID = $storyID + 1;
$storyPreviousID = $storyID - 1;
$storyNext = $this->Story->find()->select('Story.id')->where(['Story.pub_date <' => $todays_date])->first();
$this->set('storyNext', $storyNext);
$storyPrevious = $this->Story->find()->select('Story.id')->where(['Story.pub_date <' => $todays_date])->first();
$this->set('storyPrevious', $storyPrevious);
And in my view.ctp I have this:
<div class="next row" align="center">
<?php
if(!empty($storyPrevious)) {
echo '<a class="btn btn-secondary" style="width:50%" href="' .BASE_URL. '/' .$storyPrevious->slug. '" role="button">Previous Story</a>';
}
if(!empty($storyNext)) {
echo '<a class="btn btn-secondary" style="width:50%" href="' .BASE_URL. '/' .$storyNext->slug. '" role="button">Next Story</a>';
}
?>
</div>
I feel like I'm super close since I am no longer getting any errors on my development site. But the link just sends me to the homepage of my website.
Thanks for the help!
there are probably many things that you can improve in your code**. But the main reason you are redirected to your main page is that you are just selecting the id of your Story but then in your view you need to echo the slug.
So when you are echoing $storyPrevious->slug PHP returns an empty string
So in the controller select the slug too
$this->Story->find()
->select(['id', 'slug')
->where(['Story.pub_date <' => $todays_date])
->first();
** such as using helpers in your view and using Cakephp naming conventions
Thanks for the input everyone. It was really helpful. I have no one to really get feedback from, until now. I took the above input and re coded my logic a bit. I am not sure why I was trying to use so many variables. I also found through some digging, someone who wrote something similar, they called it a neighbors table. I combined yours, mine and theirs to create this. Looks like its working on my dev site as of now and Ill update my answer if that changes when I bring it live.
In my StoryController:
$todays_date = date('Y-m-d H:i:s');
$this->loadModel('Story');
$id = $story->id;
$storyNext = $this->Story->find()
->select(['id', 'slug'])
->order(['id' => 'ASC'])
->where(['id >' => $id, 'Story.pub_date <' => $todays_date])
->first();
$this->set('storyNext', $storyNext);
$storyPrevious = $this->Story->find()
->select(['id', 'slug'])
->order(['id' => 'DESC'])
->where(['id <' => $id, 'Story.pub_date <' => $todays_date])
->first();
$this->set('storyPrevious', $storyPrevious);
And in my view.ctp:
<?php
if(!empty($storyPrevious)) {
echo 'Previous Story';
}
if(!empty($storyNext)) {
echo 'Next Story';
}
?>
I think I'm doing something silly... I've been following the tutorial on overriding renderers and had a google but can't work it out. (I was hopeful when I found this other stackoverflow question but it hasn't helped)
I'm trying to override the renderer for a module (studentquiz) but it doesn't seem to be working.
Below is what I've done so far... I've just copy and pasted a function in and then deleted a few lines from it - expecting to see the changes after purging the caches but nothing is happening. Have tried various setups but nothing is working.
Any help would be greatly appreciated!
Many thanks
Ollie
/theme/THEMENAME/config.php has the line:
$THEME->rendererfactory = 'theme_overridden_renderer_factory';
/theme/THEMENAME/renderers.php is as follows:
<?php
defined('MOODLE_INTERNAL') || die;
include_once($CFG->dirroot . "/mod/studentquiz/renderer.php");
class theme_THEMENAME_mod_studentquiz_renderer extends mod_studentquiz_renderer {
// place your overridden methods (functions) here.
public function render_stat_block($report) {
...
}
}
I'm deleting the following lines from the function:
. html_writer::div($this->render_progress_bar($info2), '', array('style' => 'width:inherit'))
. html_writer::div(get_string('statistic_block_approvals', 'studentquiz')
.html_writer::span('<b>' .$userstats->questions_approved .'</b>', '',
array('style' => 'float: right;color:#28A745;')))
. html_writer::div(get_string('statistic_block_created', 'studentquiz')
.html_writer::span('<b>' .$userstats->questions_created .'</b>', '',
array('style' => 'float: right;')))
To demove the last progress bar and two lines from this:
Change the renderer factory in config.php to:
$THEME->rendererfactory = 'theme_THEMENAME_renderer_factory';
It will need to reflect the frankenstyle name of your theme
tl;dr; Trying to link the 'save and return' button when editing/deleting a course to my local plugins index.php instead of moodles default redirect for these features, moodle allready has a returnTo query parameter so i was thinking if that could be used somehow.
Hey
I am creating a local plugin that has a administration panel, where you can access CRUD on all courses in the system as seen in the picture below:
The problem now is that whenever I click edit, I get into the course edit page of course, but when I return from there I click "save and return" I would like to get back to my own admin page instead of the course page or category manage page.
The code I have right now looks like this:
//edit
$edit_course_moodle_url = new moodle_url('/course/edit.php', array('id' => $course->id, 'returnto' => 'local/adminpanel/index.php'));
$edit_course_url = $edit_course_moodle_url->get_path() . '?id=' . $edit_course_moodle_url->get_param('id') . '&returnto=' . $edit_course_moodle_url->get_param('returnto');
//delete
$delete_course_moodle_url = new moodle_url('/course/delete.php', array('id' => $course->id, 'returnto' => 'local/adminpanel/index.php'));
$delete_course_url = $delete_course_moodle_url->get_path() . '?id=' . $delete_course_moodle_url->get_param('id') . '&returnto=' . $delete_course_moodle_url->get_param('returnto');
As you can see I use the "returnto" query parameter, normally moodle has a "catmanage" as "returnto" that returns you to the category management page, where moodle has its own CRUD for categories and courses. So my question is, can I create my own alias for a link and use it like moodle uses the catmanage link, but for my admin page instead.
Thanks a lot ! :)
EDIT:
Change code to the following:
if (empty($CFG->loginhttps)) {
$securewwwroot = $CFG->wwwroot;
} else {
$securewwwroot = str_replace('http:','https:',$CFG->wwwroot);
}
$returnurl = new moodle_url($securewwwroot . '/local/adminpanel/index.php');
$edit_course_moodle_url = new moodle_url($securewwwroot . '/course/edit.php', array(
'id' => $course->id,
'sesskey' => sesskey(),
'returnto' => 'url',
'returnurl' => $returnurl->out(false))
);
$edit_course_url = $edit_course_moodle_url->out();
But it looks like moodle took away the button from edit course called "save and return" now it only has "save and display" or "Cancel" , both of which brings me back to the course, sad times :(
According to the code I can see in course/edit.php, you should use the following URL arguments:
returnto: 'url'
returnurl: The url
sesskey: sesskey()
In code that gives us:
$returnurl = new moodle_url('/local/plugin/page.php');
$editurl = new moodle_url('/course/edit.php', array(
'id' => 2,
'sesskey' => sesskey(),
'returnto' => 'url',
'returnurl' => $url->out(false)
));
echo $editurl->out();
The page course/delete.php does not seem to support those arguments. But it's probably easier for your plugin to delete the course by itself, it's as simple as calling delete_course($courseid);.
I am starting to learn PHP framework, particulary cakephp. I know how to insert into database, but I am curious about displaying the entries on the same page. I don't want to reload or redirect the page just to display the results.
I have only three four fields in the database. id, name, email, and the date. I have used the same validation in the model page.
Controllers:
<?php
function index(){
if($this->request->is('Post')){
$this->Save->save(data);
}
//display the entries from the database
$this->set('saves', $this->Save->find('all'));
}
?>
Index.ctp:
<?php
echo $this->Form->create('Save');
echo $this->Form->input('name', array('class'=>'name', 'required' => true));
echo $this->Form->input('email', array('class'=>'email', 'required' => true));
echo $this->Form->input('message', array( 'required' => true));
echo $this->Form->submit('submit', array('formnovalidate' => true));
//i want to display the entries here
foreach($saves as $row){
echo $row['Save']['name'];
echo $row['Save']['comment'];
}
?>
The problem is that , it affects the textarea. It reduces the size to half. Need help. thanks a lot. I am new to cakephp and I have been googling about it, but have not find the any results. Thanks
From what I understand it seems that your problem is more CSS issue than CakePHP issue. I don't think printing the data will cut the field into half.
Try put after echo $this->Form->submit('submit', array('formnovalidate' => true)); because the submit method does not include afterwards.
You can this $this->Form->end if you don't want to add the form closing tag manually.
I have a delete link to delete a Comment object by ID /comment/:id/delete
In order to secure this link I add a csrf token to the link
$CSRFTokenForm = new BaseForm();
$link = url_for(..., array('_csrf_token' => $CSRFTokenForm->getCSRFToken()));
and in the executeDelete i use the checkCSRFProtection() method, and it all works fine.
The only thing is that each comment is displayed by a partial, and each partial creates it's own BaseForm() in order to create the token, which is waste of time since they're all the same..
Do you have a better idea on how to make it more efficient, like maybe a static getCSRFToken() method or creating a global BaseForm()?
Use SF's method => delete. It creates the CSRF token for you:
<?php
echo link_to('comment/' . $comment->getId() . '/delete',
array(
'method' => 'delete',
'confirm' => 'Do you really want to delete the comment??',
'title' => 'Delete'
)
);
?>
Yes it's a jQuery Plugin error. If you are using sfJqueryReloadedPlugin - 1.4.3 you need to change the source code of the file jQueryHelper in the plugin's directory and put "BaseForm" instead of "sfForm" in the "csrf => 1" sectuo
With the jQuery Plugin try:
jq_link_to_remote('comment/' . $comment->getId() . '/delete', array('csrf' => 1))
Found it in the sourcecode and they do it with a BaseForm instance, too.