Settings page and multi edit rows - cakephp

I would like to make a configuration page like this for my CMS
http://img4.hostingpics.net/pics/72087272ok.png
I want this page (admin/settings/index) gets me the various settings (ID 1 to 21) came to my table and in case of change, I can, in this form, do an update
After 3 days of not especially fruitful research I found something. I put in my SettingsController:
admin_index public function () {
if (!empty($this->data)) {
$this->Setting->saveAll($this->request->data['Setting']);
} else {
$this->request->data['Setting'] = Set::combine($this->Setting->find('all'),
'{n}. Setting.id', '{n}. Setting');
}
}
and my view:
<?php
echo $this->Form->create('Setting', array ('class' => 'form-horizontal'));
foreach($this->request->data['Setting'] as $key => $value) {
echo $this->Form->input('Setting.' . $key . '.pair');
echo $this->Form->input('Setting.' . $key . '.id');
}
echo $this->Form->end(array('class' => 'btn btn-primary', 'label' => 'Save',
'value' => 'Update!', 'div' => array ('class' => 'form-actions')));
?>
he is recovering well all my information, I can even update. The trouble is that I do not really know how I can do to get the same result as my first screenshot. He puts everything in the textarea while in some cases I want the checkbox and / or drop-down.
please help me or explain to me how to make a page that retrieves configuration information from my table and allows me to edit without use an address like admin/settings/edit/ID
My table settings is something like this here
http://img4.hostingpics.net/pics/724403settings.png

If you are asking about how to format the output to produce a "settings" page, you will need to do it manually or add some kind of metadata to your table schema, such as "field_type".
Now Cake will automatically generate a checkbox for a boolean or tiny int, for example, but as you have varchars, ints, texts and so on, this won't work for you (pair is presumably varchar or text to allow for the different possible values.
You cannot really automagically generate the outputs you want without telling Cake.
One option would be to add a field_type to the table, so:
key value field
site_name My Site input
site_online 1 checkbox
meta_desc [some text] textarea
and something like
foreach($settings as $setting) {
echo $this->Form->{$setting['field']}($setting['key'],
array('value' => $setting['value']));
}
might do what you are after.
Or if($key=='site_name') // output a textbox
but this is not exactly ideal.

Related

How to strip HTML from a field in drupal views

I am trying to add a function to that strips the html from a field in drupal views. I found a function for sql server called "udf_StripHTML" that does that.
http://blog.sqlauthority.com/2007/06/16/sql-server-udf-user-defined-function-to-strip-html-parse-html-no-regular-expression/
I am using the following code:
/**
* Implements hook_views_query_alter().
*/
function cviews_views_query_alter(&$view, &$query) {
// Add a strip html tags from content.
$fields = array('field_data_body.body_value');
foreach ($query->where as $key1 => $value) {
foreach ($value['conditions'] as $key2 => $coditions) {
if (in_array($coditions['field'], $fields)) {
$query->where[$key1]['conditions'][$key2]['field'] = 'dbo.udf_StripHTML(' . $coditions['field'] . ')';
}
}
}
}
When views module converts the query object to a string the field become
from:
'dbo.udf_StripHTML(field_data_body.body_value)';
to:
[dbo].[udf_StripHTMLfield_data_body.body_value]
My question is, how can I add a function there?
Thank you,
You are going way too deep here friend. I'm going to assume that you're using Drupal 7, but for Drupal 8 this should be similar (since views is in core for both).
A few things about your approach:
That function is a user defined function which means that it needs to be defined at a much lower-level (in the SQL database) before you can use it in your query.
This is a red-herring approach, however, because you don't need to even touch the SQL to accomplish what you want (you can do this with PHP with strip_tags!)
You don't need a query alter hook here (we don't need to go to the database to do this). You could do this with one of the preprocess or field hooks from the field API or the views API using the function linked in my previous point.
Even better, you don't even have to touch the code to accomplish this. You can do it right in the Drupal UI.
Under the field settings for the view, select rewrite results and then Strip HTML tags. Presto, no more HTML tags in that field.
Image source: https://www.drupal.org/node/750172
Here is the solution that worked for me:
// Traverse through the 'where' part of the query.
foreach ($query->where as &$condition_group) {
foreach ($condition_group['conditions'] as &$condition) {
if (in_array($condition['field'], $fields)) {
$value = $condition['value'];
$field = $condition['field'];
$condition = array(
'value' => array(),
'field' => t('dbo.udf_StripHTML(!field) like \'#value\'', array(
'!field' => $field,
'#value' => $value)),
'operator' => 'formula',);
}
}
}

hard-code Form input field in cakephp so user can't edit it?

I have following form field in a registration form in cakephp. I want to make it 'hard-coded', so user can't edit it
echo $form->input('name', array('label' => __('Name *', true)));
Then don't add it to the form.
Those fields should be added in the controller (or even beforeValidate/beforeSave model layer) then right before saving:
if ($this->request->is('post')) {
$this->User->create();
// add the content before passing it on to the model
$this->request->data['User']['status'] = 1;
if ($this->User->save($this->request->data)) {
...
}
}
See "default values - hidden" here.
You can set the readonly property:
echo $form->input('name', array('label' => __('Name *', true), 'readonly' => true));
However, this only affects the UI, and so you still have to apply mark's answer to ensure the value doesn't get changed by the user.
Two options:
hard code the value before the save
use white list
If you want the field to be a read-only, from the moment it is set. use white list. this way - it doesn't matter if the user will submit the field or not. cake won't save it.
$white_list = array('title', 'category');
$this->Model->save($data,$validate,$white_list);
The other solution is as mark coded it:
$this->request->data['User']['status'] = 1;
if ($this->User->save($this->request->data)) {
...
}
Any solution should mix a UI indication that the field will not be changed. tho a good UX will not allow it in the first-place.

CakePHP SaveMany not saving

I have a issue with the SaveMany call in my CakePHP function.
The code below is my current attempt. This now sort of works, it does not make any errors at all but only the savefield will save any data, the saveMany, although it does not come back with errors does NOT save the data?
Please Help???
$InvoiceArrayData = array(
array('Invoicedata' => array('workdes' => $WorkHolder)),
array('Invoicedata' => array('price' => $PriceHolder)),
);
foreach($InvoiceArrayData as $InvoiceArrayKey => $InvoiceArrayValue) {
debug($InvoiceArrayValue);
$this->Invoicedata->saveMany($InvoiceArrayValue['Invoicedata']);
$this->Invoicedata->saveField('invoicejoblists_id', $MyJobIDInput);
$this->Invoicedata->saveField('invoicejoblists_id', $MyJobIDInput);
$this->Session->setFlash('Invoice Data Saved', 'default', array(), 'good');
//$this->redirect($this->Auth->redirect('/AdminInvoiceSystem/'));
}
When the code runs it does redirect with the saved meessaged but when the database is checked, only the invoicejoblist_id is saved.
Have not not done something right?
Glenn
UPDATE ::
Ok so what I am trying to do is to build an invoice system, I have a number of tables one of which 'invoicedatas' which has 'id, workdes, price, invoicejoblists_id' contained within it. ID is the key and is sent to auto count.
When the user starts to make a new invoice they fill in a form which saves in the other tables. But also in this form they input a work detail and price for that work.
This is then sent over to the function I am working on now, which returns no errors but does not save. Or I should say the saveMany call does not work, the saveField works and saves the ID number which I call from one of my other tables.
A bit of code I forget to add are the vars, which are just basic holders for the $this->data I am trying to save.
$WorkHolder = $this->data['workdes'];
$PriceHolder = $this->data['price'];
Please Help......
Glenn.
OK Here goes, I try and see if I can explain myself in more detail.
I am going to re-do so of my work to make it just plan easier for the UI apart form anything. To explain, I will now have a form which sets the invoice job, e.g jobnumber per user, user id, and other bits. This information will go into a table call invoicejoblist
The second table invoicedata holds just workdes and price along with invociejoblists_id (this links each row with the invoice job details).
But no matter what I do I can not get it to save into workdes and price. This is how my current test setup looks :
MY .cpt file:
$InvoiceMake = $this->Form->create('Invoice', array('url'=>'/InvoiceSet/', 'id' => 'InvoiceSet', 'inputDefaults' => array('div' => false) ) );
$InvoiceMake .= "<div id=\"InvoiceDataInput\">Please Input Description<br />";
$InvoiceMake .= $this->Form->textarea('Invoicedata.0.workdes');
$InvoiceMake .= $this->Form->textarea('Invoicedata.1.workdes');
$InvoiceMake .= $this->Form->textarea('Invoicedata.2.workdes');
$InvoiceMake .= $this->Form->textarea('Invoicedata.3.workdes');
$InvoiceMake .= $this->Form->textarea('Invoicedata.4.workdes');
$InvoiceMake .= "</div>";
$InvoiceMake .= $this->Form->submit('Make Invoice', array('div' => false, 'class' => 'ButtonInvoice'));
$InvoiceMake .= $this->Form->end();
echo $InvoiceMake;
My test function in controller:
function test() {
debug($this->data);
$HoldeME = $this->data;
$this->loadModel('Invoicedata');
$this->Invoicedata->saveAll($HoldeME);
}
These return not errors but the workdes information submitted will not save. The only thing I put in my model was:
public $belongsTo = array(
'Invoicejoblists' => array(
'className' => 'Invoicejoblists',
'foreignKey' => 'invoicejoblists_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
I did have some checks to make sure the inputs where not empty but remove them to see if that was my problem, not sure it that was right or not.
Hope that is better explained, I have gone down to the right basic level to just test save any data set but it still will not let me, is that because I should have something in my model?
Any help most welcome
Glenn.
I really have no clue what you're asking, but I'm giving it a blind-shot.
You should be able to save an item and as many sub/related items as you want in a single saveAll().
Just make sure your form (or the data) is formatted correctly per the copious instructions found in the CakePHP book.
As an example:
echo $this->Form->create('Invoice');
echo $this->Form->input('name');
echo $this->Form->input('Price.0.amount');
echo $this->Form->input('Price.1.amount');
echo $this->Form->end('Submit');
Then, when you do a saveAll() (or saveMany() - whatever), your Invoice will save as well as the two Prices (assuming Invoice is related to Price).
If you're trying to build the data manually, but don't know how it should be formatted, you can always create a form like above, submit it, and debug() the results to see how CakePHP would format it.
Hope that helps clarify for you.

cakephp echo count

Hi I have a like button which updates the likes table field type with like. I want to count the number of likes in the table, in my controller I have this,
// Load Likes Model and retrive number of likes and dislikes
$this->loadModel('Like');
$related_likes = $this->Like->find('count', array(
'conditions' => array('uploadid' => $id)
));
$this->set('likes', $related_likes);
}
How do I go about echoing the number of likes in my view??
Would really appreciate any help
When you use set(), the name you define becomes a variable in the view.
In your view:
echo $likes;

CakePHP AutoComplete Question

I am working on a book review application and I am using autoComplete to search for titles in when creating a review. The review model has an associated book_id field and the relationship is setup as the review hasOne book and a book hasMany reviews.
I am trying to pass the Book.id (into the book_id field), but I want to display Book.name for the user to select from. With the default setup (accomplished via CakePHP's tutorial), I can only pass Book.name. Is it possible to display the name and pass the id?
Also, I am passing it via the following code in the create() action of the review controller:
$this->data['Review']['book_id'] = $this->data['Book']['id'];
Is that the proper way to do it in CakePHP? I know in Ruby on Rails, it is automatic, but I can't seem to make it work automagically in CakePHP. Finally, I am not using the generator because it is not available in my shared hosting environment... so if this is the wrong way, what do I need other than associates in my models to make it happen automatically?
Thanks for the help and I promise this is my question for awhile...
UPDATE- I tried the following, but it is not working. Any ideas why?
function autoComplete() {
$this->set('books', $this->Book->find('all', array(
'conditions' => array(
'Book.name LIKE' => $this->data['Book']['name'].'%'
),
'fields' => array('id','name')
)));
$this->layout = 'ajax';
}
The problem is that when I use the code above in the controller, the form submits, but it doesn't save the record... No errors are also thrown, which is weird.
UPDATE2:
I have determine that the reason this isn't working is because the array types are different and you can't change the array type with the autoComplete helper. As a workaround, I tried the follow, but it isn't working. Can anyone offer guidance why?
function create() {
if($this->Review->create($this->data) && $this->Review->validates()) {
$this->data['Review']['user_id'] = $this->Session->read('Auth.User.id');
$this->Book->find('first', array('fields' => array('Book.id'), 'conditions' => array('Book.name' => $this->data['Book']['name'])));
$this->data['Review']['book_id'] = $this->Book->id;
$this->Review->save($this->data);
$this->redirect(array('action' => 'index'));
} else {
$errors = $this->Review->invalidFields();
}
}
FINAL UPDATE:
Ok, I found that the helper only takes the find(all) type or array and that the "id" field wasn't passing because it only applied to the autoComplete's LI list being generated. So, I used the observeField to obtain the information and then do a database lookup and tried to create a hidden field on the fly with the ID, but that didn't work. Finally, the observeField would only take the characters that I put in instead of what I clicked, due to an apparent Scriptaculous limitation. So, I ended up going to a dropdown box solution for now and may eventually look into something else. Thanks for all of the help anyway!
First of all, $this->data will only contain ['Book']['id'] if the field exists in the form (even if it's hidden).
To select something by name and return the id, use the list variant of the find method, viz:
$selectList = $this->Book->find('list', array(
'fields' => array(
'id',
'name'
)));
$this->set('selectList', $selectList);
In the view, you can now use $selectList for the options in the select element:
echo $form->input('Book.id', array('type' => 'hidden'));
echo $form->input('template_id', array(
'options' => $selectList,
'type' => 'select'
));

Resources