CakePHP: Custom Function in bootstrap that uses $ajax->link not working - php

Hello I have two questions:
(1) Is it best practice to create global custom functions in the bootstrap file? Is there a better place to store them?
(2) I am unable use the following line of code in my custom function located in my bootstrap.php file:
$url = $ajax->link ( 'Delete', array ('controller' => 'events', 'action' => 'delete', 22 ), array ('update' => 'event' ), 'Do you want to delete this event?' );
echo $url;
I receive the following error:
Notice (8): Undefined variable: ajax [APP\config\bootstrap.php, line 271]
Code
}
function testAjax () {
$url = $ajax->link ( 'Delete', array ('controller' => 'events', 'action' => 'delete', 22 ), array ('update' => 'event' ), 'Do you want to delete this event?' );
testAjax - APP\config\bootstrap.php, line 271
include - APP\views\event\queue.ctp, line 19
View::_render() - CORE\cake\libs\view\view.php, line 649
View::render() - CORE\cake\libs\view\view.php, line 372
Controller::render() - CORE\cake\libs\controller\controller.php, line 766
Dispatcher::_invoke() - CORE\cake\dispatcher.php, line 211
Dispatcher::dispatch() - CORE\cake\dispatcher.php, line 181
[main] - APP\webroot\index.php, line 91
However it works as intended if I place that same code in my view:
<a onclick=" event.returnValue = false; return false;" id="link1656170149" href="/shout/events/delete/22">Delete</a>
Please help :)
Thanks in advance!!

That depends: If it's a complete generic function that could be accessed from everywhere in your app, than yes. Otherwise I would place it in the parent-class from where you want to use it (app_model, app_controller)
$ajax is a helper class, that cannot be accessed from your bootstrap file. You would need to inculde the helper in the bootstrap, and that is the point from where it doesn't make sense to place the function there

Don't do it in bootstrap - no good place for that.
If You want to have this url on every page - put it in Your layout (http://book.cakephp.org/view/96/Layouts)

Related

Array to string conversion error in checkbox

Using CakePHP 3.5.3
Hi,
// WHAT I HAVE
I have a users index view which displays all the relevant users in the database. At the top of the page I have a select list which allows the user to select the deactivate option and on each users row I have a checkbox.
// WHAT I'M TRYING TO DO
The user can check the checkboxes they want then select the deactivate option from the select list and click submit. The submit sends the script
to my actions method in my users controller. In the actions method I check to see if the deactivate option has been selected and if it hasn't I send a
message back to the user to that effect. After this I check to see if a checkbox has been checked and if none have I send a message back to the user to that effect.
If at least one checkbox has been checked and the deactivate option has been selected I then identify which checkboxes have been checked and
update the database to deactivate for those choosen users.
// ON USERS INDEX VIEW I DECLARE MY CHECKBOX AS BELOW
<?= $this->Form->checkbox("checkboxes[]", ['value' => $user->id]); ?>
// ACTIONS METHOD
public function actions()
{
if (empty($this->request->getData('action'))) {
$this->Flash->sys_error('', [
'key' => 'message',
'params' => [
'p1' => __('Please select an action!')
]
]);
// Send to index action for page load
$this->setAction('index');
}
else {
$checkboxArray = $this->request->getData('checkboxes');
foreach($checkboxArray as $key => $val):
echo 'key is ' . $key . "<br />";
echo 'val is ' . $val . "<br />";
if (empty($val)) {
unset($checkboxArray[$key]);
}
endforeach;
if (empty($checkboxArray)) {
$this->Flash->sys_error('', [
'key' => 'message',
'params' => [
'p1' => __('Please select a user!')
]
]);
// Send to index action for page load
$this->setAction('index');
}
else {
foreach($this->request->getData('checkboxes') as $userID):
if ($userID != 0) {
// UPDATE THE DATABSE
echo 'in update the database ' . '<hr />';
}
endforeach;
// Send to index action for page load
$this->setAction('index');
}
}
}
Even though I'm not 100% sure if this the best or correct way to do this it does work except for the following error which occurs on the checkbox in the users index view each time I invoke $this->setAction('index'); The stacktrace from the log is as follows:
Notice (8): Array to string conversion in [C:\xampp\htdocs\app\vendor
\cakephp\cakephp\src\View\Widget\CheckboxWidget.php, line 81] Request
URL: /users/actions Referer URL: https://localhost/app/users Trace:
Cake\Error\BaseErrorHandler::handleError() -
CORE\src\Error\BaseErrorHandler.php, line 153
Cake\View\Widget\CheckboxWidget::_isChecked() -
CORE\src\View\Widget\CheckboxWidget.php, line 81
Cake\View\Widget\CheckboxWidget::render() -
CORE\src\View\Widget\CheckboxWidget.php, line 51
Cake\View\Helper\FormHelper::widget() -
CORE\src\View\Helper\FormHelper.php, line 2775
Cake\View\Helper\FormHelper::checkbox() -
CORE\src\View\Helper\FormHelper.php, line 1570 include -
APP/Template\Users\index.ctp, line 193 Cake\View\View::_evaluate() -
CORE\src\View\View.php, line 1196 Cake\View\View::_render() -
CORE\src\View\View.php, line 1157 Cake\View\View::render() -
CORE\src\View\View.php, line 765 Cake\Controller\Controller::render()
- CORE\src\Controller\Controller.php, line 623 Cake\Http\ActionDispatcher::_invoke() -
CORE\src\Http\ActionDispatcher.php, line 125
Cake\Http\ActionDispatcher::dispatch() -
CORE\src\Http\ActionDispatcher.php, line 93
Cake\Http\BaseApplication::__invoke() -
CORE\src\Http\BaseApplication.php, line 103
Cake\Http\Runner::__invoke() - CORE\src\Http\Runner.php, line 65
Cake\Http\Middleware\CsrfProtectionMiddleware::__invoke() -
CORE\src\Http\Middleware\CsrfProtectionMiddleware.php, line 106
Cake\Http\Runner::__invoke() - CORE\src\Http\Runner.php, line 65
Cake\I18n\Middleware\LocaleSelectorMiddleware::__invoke() -
CORE\src\I18n\Middleware\LocaleSelectorMiddleware.php, line 62
Cake\Http\Runner::__invoke() - CORE\src\Http\Runner.php, line 65
Cake\Routing\Middleware\RoutingMiddleware::__invoke() -
CORE\src\Routing\Middleware\RoutingMiddleware.php, line 107
Cake\Http\Runner::__invoke() - CORE\src\Http\Runner.php, line 65
Cake\Routing\Middleware\AssetMiddleware::__invoke() -
CORE\src\Routing\Middleware\AssetMiddleware.php, line 88
Cake\Http\Runner::__invoke() - CORE\src\Http\Runner.php, line 65
Cake\Error\Middleware\ErrorHandlerMiddleware::__invoke() -
CORE\src\Error\Middleware\ErrorHandlerMiddleware.php, line 95
Cake\Http\Runner::__invoke() - CORE\src\Http\Runner.php, line 65
DebugKit\Middleware\DebugKitMiddleware::__invoke() -
ROOT\vendor\cakephp\debug_kit\src\Middleware\DebugKitMiddleware.php,
line 52 Cake\Http\Runner::__invoke() - CORE\src\Http\Runner.php, line
65 Cake\Http\Runner::run() - CORE\src\Http\Runner.php, line 51
Cake\Http\Server::run() - CORE\src\Http\Server.php, line 81 [main] -
ROOT\webroot\index.php, line 40
// SOLUTION
I have managed to eliminate this error if I declare my checkbox as below:
// FROM
<?= $this->Form->checkbox("checkboxes[]", ['value' => $user->id]); ?>
// TO
<?= $this->Form->checkbox("checkboxes[$user->id]", ['value' => user->id]); ?>
// MY QUESTION
But this means I'm replacing the keys in the array with the user id and I don't know if this is considered as OK or if there's a better more professional way of doing it.
Any help would be greatly appreciated. Z.
#SamHecquet - Thanks for the edit, I'll know for next time how to format the stacktrace.
#Derek - As requested see below - I won't include the whole page as a lot of it I believe is unnecessary but if you want more than I have posted just let me know.
// Select list from the element
<!-- Start form -->
<?php
// Declare individual page forms
if ($page === 'usersPage') {
// Users form
echo $this->Form->create(null, [
'url' => ['controller' => 'Users', 'action' => 'actions'],
'novalidate' => true
]);
}
<?php
// Declare individual page select lists
if ($page === 'usersPage') {
// Users form
$options = [
'' => '--Select Action--',
'a' => 'Deactivate User/s'
];
}
else{
header ("Location: ".$this->Url->build(["controller" => "Pages", "action" => "display", 'blank']));
exit(0);
}
$this->Form->label('action', '');
echo $this->Form->input('action', [
'type' => 'select',
'options' => $options,
'class' => 'ns-typ-2 ix-fa',
'label' => false
]);
?>
<?= $this->Form->submit(__('Go'), [
'class' => 'submit-ix-go'
]);
?>
// INDEX USERS
<!-- Headings -->
<div class="grid-ix-1 ix-r-top">
<!-- Checkbox -->
<div class="ix-usr-checkbox">
<div class="ix-h">
<div class="ix-h-cb">
<input type="checkbox" onClick="toggle(this)" >
</div>
</div>
</div>
<!-- Role -->
<div class="ix-usr-role">
<div class="ix-h">
<h2 class="ix-h2"><?= $this->Paginator->sort('Users.role', __('Role')) ?></h2>
</div>
</div>
</div>
<!-- Cells-->
foreach ($users as $user): ?>
<div class="grid-ix-1">
<!-- Checkbox -->
<div class="ix-usr-checkbox">
<div class="ix-c-cb">
<?= $this->Form->checkbox('checkboxes[]', ['value' => $user->id]); ?>
</div>
</div>
<!-- Role -->
<div class="ix-usr-role">
<div class="ix-usr-role-text"><?= h($user->role) ?></div>
</div>
</div>
<?php endforeach; ?>
<!-- Form ends -->
<?= $this->Form->end() ?>
#Derek - Please see the multiple checkbox issue.
When I used the solution from Dave the multiple checkbox selection worked on page load but not after I invoked the below:
// Send to index action for page load
$this->setAction('index');
I fixed this by redirecting instead of using setAction as below:
// Redirect to index page
return $this->redirect(['action' => 'index']);
But with your solution the multiple checkbox selection functionality did not work on page load or after any of the redirects. I use the following code to achieve this.
// Javascript
// Check and uncheck all check boxes on index pages
function toggle(source) {
checkboxes = document.getElementsByName('checkboxes[]');
for(var i=0, n=checkboxes.length;i<n;i++) {
checkboxes[i].checked = source.checked;
}
}
// And then the heading checkbox is:
<input type="checkbox" onClick="toggle(this)" >
// And of course the cells checkbox
<?= $this->Form->checkbox('checkboxes.' . $k, ['value' => $user->id]); ?>
If you help with the javascript great but if not if you advise that a solution can be found I'm happy to post in the stackoverflow javascript section for a solution which would mean I could go with your solution and not edit the framework code.
Thanks in advance for any help or advice. Z
Try changing the foreach to:
<?php foreach ($users as $k => $user): ?>
<div class="grid-ix-1">
<!-- Checkbox -->
<div class="ix-usr-checkbox">
<div class="ix-c-cb">
<?= $this->Form->checkbox('checkboxes.' . $k, ['value' => $user->id, 'id' => 'checkbox' . $k, 'class' => 'userCheckbox']); ?>
</div>
</div>
<!-- Role -->
<div class="ix-usr-role">
<div class="ix-usr-role-text"><?= h($user->role) ?></div>
</div>
</div>
<?php endforeach; ?>
Javascript function
function toggle(source) {
checkboxes = document.getElementsByClassName('userCheckbox');
for(var i=0, n=checkboxes.length;i<n;i++) {
checkboxes[i].checked = source.checked;
}
}
It's providing a pretty clear answer right there in the top of the error:
Array to string conversion in [C:\xampp\htdocs\app\vendor
\cakephp\cakephp\src\View\Widget\CheckboxWidget.php, line 81
Simply find line 81 in your CheckboxWidget, and fix the specified problem.
If you're not sure how, simply searching "php array to string conversion error", provides many results, one of which describes your problem very specifically regarding inputs named checkboxes[].

Wordpress Plugin in error for user Pro

Hey can anyone explain to me why I get this error when I try to save the registration form.
Fatal error: Cannot unset string offsets in /home/wolf/public_html/wp-content/plugins/userpro/admin/admin-functions.php on line 252
Here is the line in question for the php.
/** List all groups **/
function userpro_admin_list_groups(){
global $userpro;
$output = null;
$groups = $userpro->groups;
unset($groups['register']);
unset($groups['edit']);
unset($groups['view']);
unset($groups['login']);
unset($groups['social']);
$array = array(
'register' => __('Registration Fields','userpro'),
'edit' => __('Edit Profile Fields','userpro'),
'login' => __('Login Fields','userpro'),
'social' => __('Social Fields','userpro')
);
IF anyone can shed some liht on this that would be great as far as i see theres nothing written wrong, and even the plugin maker is scrating his head on this....
the config for the php is 5.4
And the Apache is 2.4
Mysql is running 5.6

Cannot declare phptemplate_settings() - Drupal error when trying to configure template

There are two templates, whitejazz and a modified whitejazz (forumwhitejazz) - both are enabled.
I'm trying to configure the modified whitejazz, and I am getting this error - what does it mean?
Should I post anything else to help deduce this? I'm utterly stumped - my googling of this issue has not given me any solid leads.
This is in Drupal 6.25
Fatal error: Cannot redeclare phptemplate_settings() (previously declared in /home/domain/public_html/drupal-6.25/sites/all/themes/whitejazz/theme-settings.php:3) in /home/domain/public_html/drupal-6.25/sites/all/themes/forumwhitejazz/theme-settings.php on line 135
The problem is that function phptemplate_settings() was defined twice. This goes to fatal php error.
What I suggest to do is:
Create file theme-setting.php in root folder of forumwhitejazz template
There define function as in example and put there code settings.
Example:
function forumwhitejazz_settings($saved_settings) {
$form = array();
$form['forumwhitejazz_example'] = array(
'#type' => 'checkbox',
'#title' => t('Use this sample setting'),
'#default_value' => $saved_settings['forumwhitejazz_example'],
'#description' => t("This option doesn't do anything; it's just an example."),
);
return $form;
}

"Constant CRLF already defined" error when using php-resque with Laravel

I'm running php-resque together with Laravel 3 on Ubuntu 12.04.
Problem: When a job is enqueued in resque, I get the following error:
Unhandled Exception
Message:
Constant CRLF already defined
Location:
/var/www/dev/vendor/chrisboulton/php-resque/lib/Redisent/Redisent.php on line 10
What does the error mean and how can we solve it?
PHP
// Enqueue in Resque
$data = array(
'name' => $name,
'email' => $email,
'created_at' => DB::raw('NOW()')
);
Resque::enqueue('queue', 'exampleWorker', $data);
However when I try to enqueue a job using artisan, it works!
Task
class Queue_Task
{
public function run()
{
// Autoload composer vendors.
require path('composer').DS.'autoload.php';
$args = array('name' => 'John Smith');
Resque::enqueue('MyAppName', 'ExampleWorker', $args);
echo "Resque job queued.\n";
return;
}
}
Output
PHP Notice: Constant CRLF already defined in /var/www/dev/vendor/chrisboulton/php-resque/lib/Redisent/Redisent.php on line 10
Notice: Constant CRLF already defined in /var/www/dev/vendor/chrisboulton/php-resque/lib/Redisent/Redisent.php on line 10
Resque job queued.
It's a guess, but I'd say in /var/www/dev/vendor/chrisboulton/php-resque/lib/Redisent/Redisent.php there's a second define('CRLF', 'something') on line 10 that should probably be commented out if you want these two pieces of software to work together.

Problems trying to use the Kohana pagination module

I have downloaded the Kohana Pagination module from Git and have been having some issues trying to use it.
I moved it into the modules folder and have updated my bootstrap to include the module...
'pagination' => MODPATH.'kohana-pagination',
I have the following code that loads some messages from a simple table with pagination...
public function action_index()
{
$content = View::factory('welcome')
->bind('messages', $messages)
->bind('pager_links', $pager_links);
$message = new Model_Message;
$message_count = $message->count_all();
$pagination = Pagination::factory(array(
'total_items' => $message_count,
'items_per_page' => 3,
));
$pager_links = $pagination->render();
$messages = $message->get_all($pagination->items_per_page, $pagination->offset);
$this->template->content = $content;
}
When I run load this in my browser, I get the following error message...
ErrorException [ Fatal Error ]: 1Call to undefined method Kohana::config()
MODPATH\kohana-pagination\classes\kohana\pagination.php [ 87 ]
82 * #return array config settings
83 */
84 public function config_group($group = 'default')
85 {
86 // Load the pagination config file
87 $config_file = Kohana::config('pagination');
88
89 // Initialize the $config array
90 $config['group'] = (string) $group;
91
92 // Recursively load requested config groups
{PHP internal call} ยป Kohana_Core::shutdown_handler()
If I remove the code relating to the pagination, the page loads the data fine from the database. Any pointers here would be great.
Update
Found this link: https://github.com/kloopko/kohana-pagination, on the back of Ikke, so cheers for the help folks.
Appears that the previously bundled pagination module has been removed, found the kohana-pagination module which has been updated to cater for 3.2.
Hope that helps someone else just starting out. :)
The issue is just the config system has changed for version 3.2. Most modules can be fixed by simply updating
$config_file = Kohana::config('pagination');
to
$config_file = Kohana::$config->load('pagination');
Let me update you the steps to resolve with Kohana paging.
Go to modules/pagination/classes/kohana/pagination.php
Change the
$config_file = Kohana::config('pagination');
to
$config_file = Kohana::$config->load('pagination');
in the same page at the line 199
change this line to
return URL::site(Request::current()->uri).URL::query(array($this->config['current_page']['key'] => $page));
to
return URL::site(Request::current()->uri()).URL::query(array($this->config['current_page']['key'] => $page));
Add uri braces.
This resolved my issue. thank you.

Categories