I have a page that is supposed to render as a pdf with the help of WKHTMLTOPDF engine. When I click the link to load the page, I get this error:
WKHTMLTOPDF didn't return any data
Error: An Internal Error Has Occurred.
The stack trace is as follows:
APP\Plugin\CakePdf\Pdf\CakePdf.php line 234 → WkHtmlToPdfEngine->output()
}
}
$output = $Engine->output();
APP\Plugin\CakePdf\View\PdfView.php line 97 → CakePdf->output(string)
prints this
$this->response->download($this->getFilename());
}
$this->Blocks->set('content', $this->renderer()->output($content));
return $this->Blocks->get('content');
then prints out the page exactly how i want it too
CORE\Cake\Controller\Controller.php line 956 → PdfView->render(null, null)
null
null
CORE\Cake\Routing\Dispatcher.php line 193 → Controller->render()
}
if ($render && $controller->autoRender) {
$response = $controller->render();
} elseif ($response->body() === null) {
CORE\Cake\Routing\Dispatcher.php line 161 → Dispatcher->_invoke(InvoicesController, CakeRequest, CakeResponse)
object(InvoicesController) {
helpers => array(
[maximum depth reached]
)
uses => array(
[maximum depth reached]
)
paginate => array(
[maximum depth reached]
)
components => array(
[maximum depth reached]
)
name => 'Invoices'
request => object(CakeRequest) {}
response => object(CakeResponse) {}
viewPath => 'Invoices'
layoutPath => 'pdf'
viewVars => array(
[maximum depth reached]
)
view => 'view'
layout => 'adminpdf'
autoRender => false
autoLayout => true
Components => object(ComponentCollection) {}
viewClass => 'Pdf'
View => object(PdfView) {}
ext => '.ctp'
plugin => null
cacheAction => false
passedArgs => array(
[maximum depth reached]
)
scaffold => false
methods => array(
[maximum depth reached]
)
modelClass => 'Invoice'
modelKey => 'invoice'
validationErrors => null
Toolbar => object(ToolbarComponent) {}
Session => object(SessionComponent) {}
Auth => object(AuthComponent) {}
RequestHandler => object(RequestHandlerComponent) {}
pdfConfig => array(
[maximum depth reached]
)
Invoice => object(Invoice) {}
FieldsInvoice => object(FieldsInvoice) {}
InvoicesItem => object(InvoicesItem) {}
}
object(CakeRequest) {
params => array(
[maximum depth reached]
)
data => array([maximum depth reached])
query => array([maximum depth reached])
url => 'Invoices/view/1.pdf'
base => '/pra'
webroot => '/pra/'
here => '/pra/Invoices/view/1.pdf'
}
object(CakeResponse) {
}
APP\webroot\index.php line 92 → Dispatcher->dispatch(CakeRequest, CakeResponse)
object(CakeRequest) {
params => array(
[maximum depth reached]
)
data => array([maximum depth reached])
query => array([maximum depth reached])
url => 'Invoices/view/1.pdf'
base => '/pra'
webroot => '/pra/'
here => '/pra/Invoices/view/1.pdf'
}
object(CakeResponse) {
}
here is my bootstrap.php
<?php
CakePlugin::loadAll();
CakePlugin::load('DebugKit');
CakePlugin::load('CakePdf', array('bootstrap' => true, 'routes' => true));
Configure::write('CakePdf', array(
'engine' => 'CakePdf.WkHtmlToPdf',
'download'=>true,
'binary'=>'C:\\Program Files (x86)\\wkhtmltopdf\\wkhtmltopdf.exe'));
// Enable the Dispatcher filters for plugin assets, and
// CacheHelper.
Configure::write('Dispatcher.filters', array(
'AssetDispatcher',
'CacheDispatcher'
));
// Add logging configuration.
CakeLog::config('debug', array(
'engine' => 'FileLog',
'types' => array('notice', 'info', 'debug'),
'file' => 'debug',
));
CakeLog::config('error', array(
'engine' => 'FileLog',
'types' => array('warning', 'error', 'critical', 'alert', 'emergency'),
'file' => 'error',
));
the view in my controller
public function view($id = null) {
$this->set('title_for_layout', 'Invoices');
$this->set('stylesheet_used', 'homestyle');
$this->set('image_used', 'eBOXLogoHome.png');
$this->layout='adminpdf';
$this->pdfConfig = array('engine' => 'CakePdf.WkHtmlToPdf');
$this->Invoice->id = $id;
if (!$this->Invoice->exists()) {
throw new NotFoundException(__('Invalid invoice'));
}
$this->pdfConfig = array(
'engine'=>'CakePdf.WkHtmlToPdf',
'orientation' => 'potrait',
'filename' => 'Invoice_' . $id
);
$this->set('invoice', $this->Invoice->read(null, $id));
//Retrieve Account Id of current User
$accountid=$this->Auth->user('account_id');
//Find all Invoices where $conditions are satisfied
$invoicedetails=$this->Invoice->find('first', array(
'conditions' => array('Invoice.id'=>$id)));
//prints fieldsInvoice details, including invoice and field information
$invoices=$this->FieldsInvoice->find('all',array(
'conditions'=>array(
'invoice_id'=>$id)));
$itemInvoice=$this->InvoicesItem->find('all',array('conditions'=>array('invoice_id'=>$id)));
//Set variables
$this->set('invoicedetails', $invoicedetails);
$this->set('invoice', $invoices);
$this->set('accountid', $accountid);
$this->set('itemInvoice', $itemInvoice);
}
//End of Controller
}
my app controller
class AppController extends Controller {
public $components = array(
'DebugKit.Toolbar',
'Session',
'Auth'=>array(
'loginRedirect'=>array('controller'=>'users', 'action'=>'login'),
'logoutRedirect'=>array('controller'=>'users', 'action'=>'login'),
'invoiceRedirect'=>array('controller'=>'invoices', 'action'=>'viewinvoice'),
'authError'=>"You can't access this page",
'authorize'=>array('Controller')
),
'RequestHandler'
);
routes.php
*/
CakePlugin::routes();
Router::mapResources(array('Invoices'));
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
/**
here is the view i want rendered as a pdf
<div id = "content">
<h2>View Invoice</h2>
<table id="data">
<?php
if($invoicedetails['Invoice']['scheduled']==1)
{
$status = 'Scheduled';
$fcol = 'Black';
$bgcol = '#EBD8E8';
$pay = NULL;
$dispute = NULL;
}
else if($invoicedetails['Invoice']['paid']==1)
{
$status = 'Paid';
$fcol = 'Black';
$bgcol = '#B9FAEA';
$pay = NULL;
$dispute = NULL;
}
else if($invoicedetails['Invoice']['sender_id']==$accountid)
{
$status = 'Sent';
$fcol = 'Black';
$bgcol = '#F8FAC0';
$pay = NULL;
$dispute = NULL;
}
else if($invoicedetails['Invoice']['receiver_id']==$accountid)
{
$status = 'Received';
$fcol = 'Black';
$bgcol = '#FAB9B9';
$pay = $this->Html->link('Pay', array('controller' => 'Invoices','action'=>'pay_admin',$invoicedetails['Invoice']['id'] )) ;
$dispute = $this->Html->link('Dispute', array('controller' => 'Disputes','action'=>'add_admin',$invoicedetails['Invoice']['id'] ));
}
?>
<tr>
<th>Sender: </th>
<td><?php echo $invoicedetails['SenderAccount']['account_name'];?> </td>
</tr>
<tr>
<th>Receiver: </th>
<td><?php echo $invoicedetails['ReceiverAccount']['account_name'];?> </td>
</tr>
<tr>
<th>Invoice ID: </th>
<td><?php echo $invoicedetails['Invoice']['id'];?> </td>
</tr>
<tr>
<th>Invoice Date: </th>
<td><?php echo date('d.m.Y', strtotime($invoicedetails['Invoice']['created'])); ?></td>
</tr>
<tr>
<th>Due Date: </th>
<td><?php echo date('d.m.Y', strtotime($invoicedetails['Invoice']['expiry_date'])); ?></td>
</tr>
<tr>
<th>Status: </th>
<td bgcolor='<?php echo $bgcol ?>'><?php echo $status ;?> </td>
</tr>
<tr>
<th>Actions: </th>
<td><?php echo $pay ?> <?php echo $dispute ?></td>
</tr>
</table>
<br>
<table id="data">
<tr>
<th>Item Name</th>
<th>Description</th>
<th>Price Per Unit</th>
<th>Quantity</th>
</tr>
<?php foreach($itemInvoice as $itemInvoices):?>
<tr>
<td><?php echo $itemInvoices['Item']['name']; ?></td>
<td><?php echo $itemInvoices['Item']['description']; ?></td>
<td>$<?php echo number_format($itemInvoices['Item']['price'], 2, '.', ','); ?></td>
<td><?php echo $itemInvoices['InvoicesItem']['quantity']; ?></td>
</tr>
<?php endforeach; ?>
</table>
<br>
<table id="data">
<tr>
<th>Field Name</th>
<th>Entered Value</th>
</tr>
<?php foreach($invoice as $invoices):?>
<tr>
<td><?php echo $invoices['Field']['name']; ?> :</td>
<td><?php echo $invoices['FieldsInvoice']['entered_value']; ?></td>
</tr>
<?php endforeach; ?>
</table>
<br><br>
One HUGE PROBLEM on Windows and the php proc_open that CakePdf is using are SPACES in the binary and the Exception in https://github.com/ceeram/CakePdf/blob/master/Pdf/Engine/WkHtmlToPdfEngine.php#L35 is only throwing back a generic message and NOT the error that occured.
On /WkHtmlToPdfEngine.php#L36 extend with content['stderr] to see the real error:
throw new CakeException("WKHTMLTOPDF didn't return any data. Returned Error: ".$content['stderr']);
Simple solution:
will not work
'binary'=> 'C:\\Program Files\\wkhtmltopdf\\wkhtmltopdf.exe'
WORKS
'binary'=> 'C:\\Progra~1\\wkhtmltopdf\\wkhtmltopdf.exe'
there was two issues with this code that needed resolving
was the binary in the bootstrap.php
Original:
Configure::write('CakePdf', array(
'engine' => 'CakePdf.WkHtmlToPdf',
'download'=>true,
'binary'=>'C:\Program Files (x86)\wkhtmltopdf\wkhtmltopdf.exe'));
new
Configure::write('CakePdf', array(
'engine' => 'CakePdf.WkHtmlToPdf',
'download'=>false,
'binary'=>'C:\\wkhtmltopdf\\wkhtmltopdf.exe',
'orientation'=>'portrait'));
in my invoices controller I had a spelling mistake with the word portrait.
Related
I created an application form using cakephp 2.
Now I want to know, How can users view only their application details using their user id. Here is the Form controller, the application form and the table for displaying the form
//Form controller
public function index() {
$this->set('posts', $this->Post->find('all'));
}
public function view($id = null) {
if (!$id) {
throw new NotFoundException(__('Invalid post'));
}
$post = $this->Post->findById($id);
if (!$post) {
throw new NotFoundException(__('Invalid post'));
}
$this->set('post', $post);
}
public function add() {
if ($this->request->is('post')) {
$this->Post->create();
if ($this->Post->save($this->request->data)) {
$this->Flash->success(__('Your post has been saved.'));
return $this->redirect(array('action' => 'index'));
}
$this->Flash->error(__('Unable to add your post.'));
}
}
//Create form
echo $this->Form->create('Post');
echo $this->Form->input('esta',['label'=>'New or Estabilished']);
echo $this->Form->end('Save Post');
//Form display
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Created</th>
</tr>
<?php foreach ($posts as $post): ?>
<tr>
<td><?php echo $post['Post']['id']; ?></td>
<td>
<?php echo $this->Html->link($post['Post']['describe_idea'],
array('controller' => 'posts', 'action' => 'view', $post['Post']['id'])); ?>
</td>
<td><?php echo $post['Post']['created']; ?></td>
</tr>
<?php endforeach; ?>
<?php unset($post); ?>
</table>
You said that you are using cakephp 2.x please find below code to find record
for Single Record
$posts = $this->Post->find('first', array(
'conditions' => array('id' => 1)
));
For Multiple record
$posts = $this->Post->find('all', array(
'conditions' => array('id' => 1)
));
Add filter in your action
CakePHP 3.x
$posts = $this->Posts->find()
->where([
'Posts.user_id' => $this->Auth->user('id')
]);
CakePHP 2.x
$posts = $this->Posts->find('all', [
'user_id' => $this->Auth->user('id')
]);
Note: Make sure to login user to set Auth data.
with CakePHP I want to use an array with the Paginator component. I'm using the Datasources plugin and I have created the Fake model:
<?php
/**
* A Fake model.
*/
class Fake extends AppModel {
public $useDbConfig = 'arraySource';
public $records = array(
array('id' => 1, 'name' => 'Alfa', 'height' => 300, 'width' => 300),
array('id' => 2, 'name' => 'Beta', 'height' => 200, 'width' => 100),
array('id' => 3, 'name' => 'Gamma', 'height' => 450, 'width' => 200),
array('id' => 4, 'name' => 'Omega', 'height' => 600, 'width' => 50)
);
}
On the controller:
<?php
class ExampleController extends AppController {
public $components = array('Paginator');
public $uses = array('Fake');
public function justatest() {
$this->Paginator->settings = array(
'order' => array('id' => 'desc'),
'limit' => 2
);
$records = $this->Paginator->paginate('Fake');
$this->set(compact('records'));
}
Now, the data is retrieved correctly by the component. The "limit" condition works correctly. What doesn't work is the "order" condition: it doesn't work or the condition that I have indicated, or sort the data according to user input.
I cannot understand if I've done something wrong or if I cannot sort the data obtained with ArraySource.
EDIT
The view:
<table>
<tr>
<th><?php echo $this->Paginator->sort('id'); ?></th>
<th><?php echo $this->Paginator->sort('name'); ?></th>
<th><?php echo $this->Paginator->sort('height'); ?></th>
<th><?php echo $this->Paginator->sort('width'); ?></th>
</tr>
<?php foreach($records as $v): ?>
<tr>
<td><?php echo $v['Fake']['id']; ?></td>
<td><?php echo $v['Fake']['name']; ?></td>
<td><?php echo $v['Fake']['height']; ?></td>
<td><?php echo $v['Fake']['width']; ?></td>
</tr>
<?php endforeach; ?>
</table>
Right from the top of my head, try this :
$this->Paginator->settings = array(
'order' => array('id DESC'),
'limit' => 2
);
Not sure at all
I'm trying to paginate Workers which belong(s)To Job within a JobsController.
class JobsController extends AppController {
var $name = 'Jobs';
var $helpers = array('Html', 'Form', 'Js');
var $paginate = array(
'Worker' => array(
'limit' => 5,
'recursive' => 0,
'model' => 'Worker',
'order' => array('age' => 'ASC')
),
);
function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid Job.'));
$this->redirect(array('action'=>'index'));
return;
}
$this->Job->id = $id;
$workers = $this->paginate('Worker', array('Worker.job_id' => $id));
if ($workers) {
$this->set('workers', $workers);
}
}
In view.ctp:
<?php
$this->Html->script(array('jquery.min'), array('inline' => false));
$this->Paginator->options(array(
'update' => '#content',
'evalScripts' => true,
));
?>
<?php if (isset($workers)): ?>
<?php echo $this->Paginator->numbers(array('model' => 'Worker')); ?>
<table>
<tr>
<th>Age</th>
<th>Info</th>
</tr>
<?php foreach ($workers as $worker): ?>
<tr>
<td>
<?php echo $worker['Worker']['age']; ?>
</td>
<td>
<?php echo $worker['Worker']['info']; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php echo $this->Paginator->numbers(array('model' => 'Worker')); ?>
<p>
<?php
echo $this->Paginator->counter(array(
'model' => 'Worker',
'format' => __('Page %page% of %pages%, showing %current% records out of %count% total.')
));
?></p>
<?php endif; ?>
<?php echo $this->Js->writeBuffer(); ?>
I'm getting the correct list of workers. But the links generated by numbers are not working. They look like /view/2/page:2/sort:Worker.age/direction:ASC
What am I doing wrong? cakephp version is 2.4.1.
Try this in your view action, by setting the order at run time.
$this->paginate['Worker']['order'] = array('Worker.age' => 'ASC')
Now your function look like this
function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid Job.'));
$this->redirect(array('action'=>'index'));
return;
}
$this->Job->id = $id;
$this->paginate['Worker']['order'] = array('Worker.age' => 'ASC');
$workers = $this->paginate('Worker', array('Worker.job_id' => $id));
if ($workers) {
$this->set('workers', $workers);
}
}
Hope this helps you.
i m newbie for cakephp and i want to log in with different MySQL table name "registrations". as according to cakephp book a users named table is needed to log in and registration and its User controller and model. but my question is. is there any way to login with registrations table ? Here i difine you what all pages for it.
Controller
AppController.php
OnlineController.php(this page is main controller for my site.)
Model
AppModel.php
Registrations.php
View
register.ctp
login.ctp
And here my login view page
<?php echo $this->Form->create('Registrations'); ?>
<table width="450" border="0" align="center" id="login_form">
<tr align="center">
<td colspan="2" align="left"><h3 style="padding-top:10px; padding-left:0px;">Login In</h3>
<hr /></td>
</tr>
<tr><td><?php echo $this->Session->flash(); ?></td></tr>
<tr>
<td width="245"><?php echo $this->Form->input('email'); ?></td>
<tr>
<td><?php echo $this->Form->input('password', array('type'=>'password')); ?></td>
</tr>
<tr>
<td align="right">Lost Username/Password</td>
</tr>
<tr>
<td align="right"><? echo $this->Form->end('Submit'); ?></td>
</tr>
<tr>
<td align="right"> </td>
<td> </td>
</tr>
</table>
And Controller >> OnlineController.php >> Code here
class OnlineController extends AppController {
/**
* Controller name
*
* #var string
*/
public $name = 'Online';
//component
public $components=array('Session','Paginator','Auth');
public $uses = array('Registrations','Contacts','User','RegistrationsModel');
function beforeFilter(){
parent::beforeFilter();
}
public function index(){
//$students=$this->Student->find('all');
}
public function login(){
if($this->request->is('post'))
{
// pr($_POST);
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirect());
// Prior to 2.3 use `return $this->redirect($this->Auth->redirect());`
}
else {
$this->Session->setFlash(__('Username or password is incorrect'));
}
}
}
public function logout() {
$this->redirect($this->Auth->logout());
}
public function profile()
{
}
Model Part>> Registrations.php and code is
class Registrations extends AppModel {
public $name='Registrations';
function beforeSave() {
if(isset($this->data['Registrations']['password']))
$this->data['Registrations']['password'] = Security::hash($this->data['Registrations']['password'], null, true);
return true;
}
public $validate = array(
'first_name'=>array(
'rule'=>'alphaNumeric',
'required'=> true,
'allowEmpty'=>false,
'message'=>'Please Enter Your Name'
),
'last_name'=>array(
'rule'=>'alphaNumeric',
'required'=>true,
'allowEmpty'=>false,
'message'=>"Please Enter Your Last Name."
),
'email'=>array(
'rule'=>'email',
'required'=>true,
'allowEmpty'=>false,
'message'=>"Please Enter Your Email address."
)
,
'password'=>array(
'rule'=>'alphaNumeric',
'required'=>true,
'allowEmpty'=>false,
'message'=>"Please Enter Your Password."
),
'phone'=>array(
'rule'=>'Numeric',
'required'=>true,
'allowEmpty'=>false,
'message'=>"Please Enter Your Phone No.."
)
,
'state'=>array(
'rule'=>'alphaNumeric',
'required'=>true,
'allowEmpty'=>false,
'message'=>"Please Enter Your Sate"
)
,
'city'=>array(
'rule'=>'alphaNumeric',
'required'=>true,
'allowEmpty'=>false,
'message'=>"Please Enter Your City"
)
);
}
In last i useed some logic for Appcontroller
class AppController extends Controller {
public $components = array(
'Auth' => array(
'loginAction' => array(
'controller' => 'Online',
'action' => 'login'),
'loginRedirect' => array(
'controller' => 'Online',
'action' => 'profile'),
'logoutRedirect ' => array(
'controller' => 'Online',
'action' => 'login'),
'authenticate' => array(
'Registrations' => array(
'userModel' => 'RegistrationsModel',
'fields' => array(
'username' => 'email',
'password' => 'password'
)
)
)
)
);
function beforeFilter() {
$this->Auth->allow('register','index','contact','quiz_zone','about','packages','online_test','test_gen','login');
}
}
Please give me a correct solution..Thanks in Advance
The name of the table for login is irrelevant. CakePHP doesn't empose a name for that.
This is a setting. When configuring your AuthComponent in AppController for example do:
$this->Auth->authenticate = array('Form' => array('userModel' => 'Registration'));
In your example you're passing the wrong name: RegistrationsModel.
So in your example this is very WRONG:
'authenticate' => array(
'Registrations' => array(
'userModel' => 'RegistrationsModel',
'fields' => array(
'username' => 'email',
'password' => 'password'
)
)
The first level key should be the authentication type (Form, Basic, Digest) not Registrations. Then as I explained you can do:
'authenticate' => array(
'Form' => array(
'userModel' => 'Registration',
'fields' => array(
'username' => 'email',
'password' => 'password'
)
)
This is all very well explained in the book - here and here.
Another very bad thing that you're NOT DOING is following the CakePHP Conventions!
Cake follows the philosophy of "Convention over configuration".
For example your models should be singular not plural... Read the book. Start with the turotioals.
When I click the search link in the cupcake forum plugin, enter the search criteria and click submit, I get a blank page in firefox and http 404 website cannot be found in IE.
I have no idea on what i'm doing wrong. Can someone help me? Thank you.
the following is the search_controller:
class SearchController extends ForumAppController {
/**
* Controller Name
* #access public
* #var string
*/
public $name = 'Search';
/**
* Models
* #access public
* #var array
*/
public $uses = array('Forum.Topic');
/**
* Pagination
* #access public
* #var array
*/
public $paginate = array(
'Topic' => array(
'order' => 'LastPost.created DESC',
'contain' => array('ForumCategory.title', 'User.id', 'User.username', 'LastPost.created', 'LastUser.username', 'Poll.id', 'FirstPost.content')
)
);
/**
* Search the topics
* #access public
* #param string $type
*/
public function index($type = '') {
$searching = false;
print_r($this->params['named']);
// Build
if (!empty($this->params['named'])) {
foreach ($this->params['named'] as $field => $value) {
$this->data['Topic'][$field] = urldecode($value);
}
}
if ($type == 'new_posts') {
$this->data['Topic']['orderBy'] = 'LastPost.created';
$this->paginate['Topic']['conditions']['LastPost.created >='] = $this->Session->read('Forum.lastVisit');
}
echo '<br/>';
print_r($this->data);
// Search
if (!empty($this->data)) {
$searching = true;
$this->paginate['Topic']['limit'] = $this->Toolbar->settings['topics_per_page'];
if (!empty($this->data['Topic']['keywords'])) {
if ($this->data['Topic']['power'] == 0) {
$this->paginate['Topic']['conditions']['Topic.title LIKE'] = '%'. $this->data['Topic']['keywords'] .'%';
} else {
$this->paginate['Topic']['conditions']['OR'] = array(
array('Topic.title LIKE' => '%'. $this->data['Topic']['keywords'] .'%'),
array('FirstPost.content LIKE' => '%'. $this->data['Topic']['keywords'] .'%')
);
}
}
if (!empty($this->data['Topic']['category'])) {
$this->paginate['Topic']['conditions']['Topic.forum_category_id'] = $this->data['Topic']['category'];
}
if (!empty($this->data['Topic']['orderBy'])) {
$this->paginate['Topic']['order'] = $this->data['Topic']['orderBy'] .' DESC';
}
if (!empty($this->data['Topic']['byUser'])) {
$this->paginate['Topic']['conditions']['User.username LIKE'] = '%'. $this->data['Topic']['byUser'] .'%';
}
$this->set('topics', $this->paginate('Topic'));
}
$this->Toolbar->pageTitle(__d('forum', 'Search', true));
$this->set('menuTab', 'search');
$this->set('searching', $searching);
$this->set('forums', $this->Topic->ForumCategory->getHierarchy($this->Toolbar->getAccess(), $this->Session->read('Forum.access'), 'read'));
}
}
?>
topic.php model:
class Topic extends ForumAppModel {
/**
* Belongs to
* #access public
* #var array
*/
public $belongsTo = array(
'ForumCategory' => array(
'className' => 'Forum.ForumCategory',
'counterCache' => true
),
'User' => array(
'className' => 'Forum.User'
),
'FirstPost' => array(
'className' => 'Forum.Post',
'foreignKey' => 'firstPost_id'
),
'LastPost' => array(
'className' => 'Forum.Post',
'foreignKey' => 'lastPost_id'
),
'LastUser' => array(
'className' => 'Forum.User',
'foreignKey' => 'lastUser_id'
)
);
/**
* Has one
* #access public
* #var array
*/
public $hasOne = array(
'Poll' => array(
'className' => 'Forum.Poll',
'dependent' => true
)
);
/**
* Has many
* #access public
* #var array
*/
public $hasMany = array(
'Post' => array(
'className' => 'Forum.Post',
'exclusive' => true,
'dependent' => true,
'order' => 'Post.created DESC',
)
);
index.ctp view:
<?php // Search orderbY
$orderBy = array(
'LastPost.created' => __d('forum', 'Last post time', true),
'Topic.created' => __d('forum', 'Topic created time', true),
'Topic.post_count' => __d('forum', 'Total posts', true),
'Topic.view_count' => __d('forum', 'Total views', true)
); ?>
<h2>Search</h2>
<?php echo $form->create('Topic', array('url' => array('controller' => 'search', 'action' => 'index'))); ?>
<div id="search">
<?php
echo $form->input('keywords', array('div' => false, 'label' => false, 'style' => 'width: 300px'));
echo $form->label('power', __d('forum', 'Power Search?', true));
echo $form->input('category', array('div' => false, 'label' => false, 'options' => $forums, 'escape' => false, 'empty' => true));
echo $form->input('orderBy', array('div' => false, 'label' => false, 'options' => $orderBy));
echo $form->label('byUser', __d('forum', 'By User (Username)', true) .':');
echo $form->input('byUser', array('div' => false, 'label' => false, 'style' => 'width: 150px'));
?>
</div>
<?php echo $form->end(__d('forum', 'Search Topics', true));
pr($this->validationErrors);
?>
<?php // Is searching
if ($searching === true) { ?>
<div class="forumWrap">
<?php echo $this->element('pagination'); ?>
<table cellspacing="0" class="table">
<tr>
<th colspan="2"><?php echo $paginator->sort(__d('forum', 'Topic', true), 'Topic.title'); ?></th>
<th><?php echo $paginator->sort(__d('forum', 'Forum', true), 'Topic.forum_category_id'); ?></th>
<th><?php echo $paginator->sort(__d('forum', 'Author', true), 'User.username'); ?></th>
<th><?php echo $paginator->sort(__d('forum', 'Created', true), 'Topic.created'); ?></th>
<th><?php echo $paginator->sort(__d('forum', 'Posts', true), 'Topic.post_count'); ?></th>
<th><?php echo $paginator->sort(__d('forum', 'Views', true), 'Topic.view_count'); ?></th>
<th><?php echo $paginator->sort(__d('forum', 'Activity', true), 'LastPost.created'); ?></th>
</tr>
<?php if (empty($topics)) { ?>
<tr>
<td colspan="8" class="empty"><?php __d('forum', 'No results were found, please refine your search criteria.'); ?></td>
</tr>
<?php } else {
$counter = 0;
foreach ($topics as $topic) {
$pages = $cupcake->topicPages($topic['Topic']); ?>
<tr<?php if ($counter % 2) echo ' class="altRow"'; ?>>
<td class="ac" style="width: 35px"><?php echo $cupcake->topicIcon($topic); ?></td>
<td>
<?php if (!empty($topic['Poll']['id'])) {
echo $html->image('/forum/img/poll.png', array('alt' => 'Poll', 'class' => 'img'));
} ?>
<?php echo $cupcake->topicType($topic['Topic']['type']); ?>
<strong><?php echo $html->link($topic['Topic']['title'], array('controller' => 'topics', 'action' => 'view', $topic['Topic']['id'])); ?></strong>
<?php if (count($pages) > 1) { ?>
<br /><span class="gray"><?php __d('forum', 'Pages'); ?>: [ <?php echo implode(', ', $pages); ?> ]</span>
<?php } ?>
</td>
<td class="ac"><?php echo $html->link($topic['ForumCategory']['title'], array('controller' => 'categories', 'action' => 'view', $topic['Topic']['forum_category_id'])); ?></td>
<td class="ac"><?php echo $html->link($topic['User']['username'], array('controller' => 'users', 'action' => 'profile', $topic['User']['id'])); ?></td>
<td class="ac"><?php echo $time->niceShort($topic['Topic']['created'], $cupcake->timezone()); ?></td>
<td class="ac"><?php echo number_format($topic['Topic']['post_count']); ?></td>
<td class="ac"><?php echo number_format($topic['Topic']['view_count']); ?></td>
<td>
<?php // Last activity
if (!empty($topic['LastPost'])) {
$lastTime = (!empty($topic['LastPost']['created'])) ? $topic['LastPost']['created'] : $topic['Topic']['modified']; ?>
<em><?php echo $time->relativeTime($lastTime, array('userOffset' => $cupcake->timezone())); ?></em><br />
<span class="gray"><?php __d('forum', 'by'); ?> <?php echo $html->link($topic['LastUser']['username'], array('controller' => 'users', 'action' => 'profile', $topic['Topic']['lastUser_id'])); ?></span>
<?php echo $html->image('/forum/img/goto.png', array('alt' => '', 'url' => array('controller' => 'topics', 'action' => 'view', $topic['Topic']['id'], 'page' => $topic['Topic']['page_count'], '#' => 'post_'. $topic['Topic']['lastPost_id']))); ?>
<?php } else {
__d('forum', 'No latest activity to display');
} ?>
</td>
</tr>
<?php ++$counter;
}
} ?>
</table>
<?php echo $this->element('pagination'); ?>
</div>
<?php } ?>
I added:
if (isset($this->Security)){
$this->Security->enabled=false;
}
to the beforeFilter function of search_controller.php and i'm able to get the search results now. No more HTTP 404 website not found error. :)