Combining date and time input data into one field with WPAlchemy metaboxes - meta-boxes

I'm trying to convert a metabox for date and time I'm currently using into a WPAlchemy metabox.
I am currently combining the start date and start time into one field upon save.
This is the old save function:
add_action ('save_post', 'save_event');
function save_event(){
global $post;
// - still require nonce
if ( !wp_verify_nonce( $_POST['event-nonce'], 'event-nonce' )) {
return $post->ID;
}
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;
// - convert back to unix & update post
if(!isset($_POST["startdate"])):
return $post;
endif;
$updatestartd = strtotime ( $_POST["startdate"] . $_POST["starttime"] );
update_post_meta($post->ID, "startdate", $updatestartd );
if(!isset($_POST["enddate"])):
return $post;
endif;
$updateendd = strtotime ( $_POST["enddate"] . $_POST["endtime"]);
update_post_meta($post->ID, "enddate", $updateendd );
Here are the new functions and fields for reference:
$custom_event_metabox = new WPAlchemy_MetaBox(array
(
'id' => '_custom_event_meta',
'title' => 'Event Information',
'template' => /event_meta.php',
'types' => array('event'),
'context' => 'normal',
'priority' => 'high',
'mode' => WPALCHEMY_MODE_EXTRACT,
'save_filter' => 'event_save_filter',
'prefix' => '_my_' // defaults to NULL
));
<li><label>Start Date</label>
<?php $mb->the_field('startdate'); ?>
<input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>" class="tsadate" />
</li>
<li><label>Start Time</label>
<?php $mb->the_field('starttime'); ?>
<input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>" class="tsatime" />
<span><em>Use 24h format (7pm = 19:00)</em></span>
</li>
<li><label>End Date</label>
<?php $mb->the_field('enddate'); ?>
<input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>" class="tsadate" />
</li>
<li><label>End Time</label>
<?php $mb->the_field('endtime'); ?>
<input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>" class="tsatime" />
<span><em>Use 24h format (7pm = 19:00)</em></span>
The issue I'm facing is I'm not entirely sure if I should be using the save_filter or the save_action, or how I should handle doing this ala WPAlchemy.
This is what I have thus far:
function event_save_filter($meta, $post_id)
{
// the meta array which can be minipulated
var_dump($meta);
// the current post id
var_dump($post_id);
// fix: remove exit, exit here only to show you the output when saving
//exit;
// - convert back to unix & update post
if(!isset($_POST["startdate"])):
return $post;
endif;
$updatestartd = strtotime ( $_POST["startdate"] . $_POST["starttime"] );
update_post_meta($post->ID, "startdate", $updatestartd );
if(!isset($_POST["enddate"])):
return $post;
endif;
$updateendd = strtotime ( $_POST["enddate"] . $_POST["endtime"]);
update_post_meta($post->ID, "enddate", $updateendd );
// filters must always continue the chain and return the data (passing it through the filter)
return $meta;
}
Will this work? And should it be a save_filter or a save_action?
Any insight appreciated ;-)

If you are using WPAlchemy, and all you need is to add new values or update values in your meta data. You can achieve this by adding additional values to the $meta array. When you return it as you should always do when using the save_filter, WPAlchemy will handle the saving of the data.
The main difference between save_filter vs save_action is that with the filter, you must pass back the $meta value, but you can modify the array before doing so, which allows you to save hidden values.
The power in using any of these options is that you can manipulate other aspects of WordPress during a post update and per the values that a user enters in.
Passing back false in the save_filter tells WPAlchemy to stop and not save. An additional difference between the two is also that save_filter happens before the save and save_action happens afterwards.
Here is my attempt at adjusting your code above, obviously you will have to touch it up to make it work for you, please read the comments I've included.
function event_save_filter($meta, $post_id)
{
// the meta array which can be minipulated
var_dump($meta);
// the current post id
var_dump($post_id);
// fix: remove exit, exit here only to show you the output when saving
//exit;
// at this time WPAlchemy does not have any field validation
// it is best to handle validation with JS prior to form submit
// If you are going to handle validation here, then you should
// probably handle it up front before saving anything
if( ! isset($meta['startdate']) OR ! isset($meta['enddate']))
{
// returning false stops WPAlchemy from saving
return false;
}
$updatestartd = strtotime($meta['startdate'] . $meta['starttime']);
// this is an example of setting an additional meta value
$meta['startdate_ts'] = $updatestartd;
// important:
// you may or may not need the following, at this time,
// WPAlchemy saves its data as an array in wp_postmeta,
// this is good or bad depending on the task at hand, if
// you need to use query_post() WP function with the "startdate"
// parameter, your best bet is to set the following meta value
// outside of the WPAlchemy context.
update_post_meta($post_id, "startdate", $updatestartd );
$updateendd = strtotime ($meta['enddate'] . $meta['endtime']);
// similar note applies
update_post_meta($post_id, "enddate", $updateendd );
// filters must always continue the chain and return the data (passing it through the filter)
return $meta;
}

Related

Activate default tabs based on condition

I have the following code a page that holds 3 tabs as follow:
<div id="tab-container">
<button class="tablink" onclick="openPage('Wall', this, '#F06078')" id="defaultOpen">Wall</button>
<button class="tablink" onclick="openPage('Profile', this, '#F06078')">Profile</button>
<button class="tablink" onclick="openPage('Gallery', this, '#F06078')">Gallery</button>
The idea is to make whatever tab that's called on from a controller the default and land on it. I try passing $data to the page but it's telling me it's undefined. I also think maybe storing the info in a session and calling it on the page where the tabs are, but I'm not that flexible yet with the coding.
function index ($page='wall') {
$data['defaultOpen'] = 'wall';
$data['images_model'] = $this->images_model->get_images();
$this->load->view($page, $data);
}
I know the codes are not all there right now, but hopefully you get the idea. I will probably need to use an IF statement either in php/js and I was hoping someone might give me some feedback on that. Thanks in advance for all input.
controller :
public function index ($page = 'wall') {
$this->load->helper('url'); // only if you haven't load helper in autoload.
$data['images_model'] = $this->images_model->get_images();
$this->load->view('viewName', $data);
}
view :
<?php $active_tab = end($this->uri->segment_array());?>
<button class="tablink<?php echo ($active_tab == 'wall')? ' active':''; ?>" onclick="openPage('Wall', this, '#F06078')" id="defaultOpen">Wall</button>
<button class="tablink<?php echo ($active_tab == 'profile')? ' active':''; ?>" onclick="openPage('Profile', this, '#F06078')">Profile</button>
<button class="tablink<?php echo ($active_tab == 'gallery')? ' active':''; ?>" onclick="openPage('Gallery', this, '#F06078')">Gallery</button>
The first method is to use parameters in the URL like http://localhost/example.com/home?tab=wall
Then use this URL parameter to active your tab
$tab = $this->input->get('tab'); //wall
<button class="tablink <?php if($tab == 'wall'){ echo 'active';}?>" onclick="openPage('Wall', this, '#F06078')" id="defaultOpen">Wall</button>
Apply same for other tabs as well
The second method is to pas variable to view. Use variable not a parameter in a function
function index() {
$data['defaultOpen'] = 'wall';
$data['images_model'] = $this->images_model->get_images();
$this->load->view($page, $data);
}
Use $defaultOpen like this
<button class="tablink <?php if($defaultOpen == 'wall'){ echo 'active';}?>" onclick="openPage('Wall', this, '#F06078')" id="defaultOpen">Wall</button>
Try something like this
// controller
public function index ($page = 'wall') {
// pass the selected data to view
$data['selectedPage'] = $page;
$data['images_model'] = $this->images_model->get_images();
$this->load->view('viewName', $data);
}
// view
// use ternary operator to set active class to tab
// example:
// <?php echo $selectedPage== 'wall' ? 'active' : '' ?>
// code above means if $selectedPage equals 'wall', set class to active,
// if not do nothing
<button class="tablink <?php echo $selectedPage== 'wall' ? 'active' : '' ?>"
onclick="openPage('Wall', this, '#F06078')"id="defaultOpen">Wall</button>
<button class="tablink <?php echo $selectedPage== 'profile' ? 'active' : '' ?>"
onclick="openPage('Profile', this,
'#F06078')">Profile</button>
<button class="tablink <?php echo $selectedPage== 'gallery' ? 'active' : '' ?>"
onclick="openPage('Gallery', this,
'#F06078')">Gallery</button>

CodeIgniter - I can't update row in my table

I can't find the correct way update one row in my table.
My view:
...
<?php echo form_open('ImenikController/verify_editing_phonebook/'.$this->uri->segment(3)); ?>
Ime i prezime*:
<input type='text' name='ime_prezime' value=""> <br><br>
Ulica i broj: <input type='text' name='ulica' value=""> <br><br>
Mesto: <input type='text' name='mesto' value=""> <br><br>
Telefon*: <input type='text' name='telefon' value=""> <br><br>
<u>Napomena: Polja sa zvezdicom su obavezna.</u> <br /> <br />
<input background:url('images/login-btn.png') no-repeat; border: none;
width='103' height='42' style='margin-left:90px;' type='submit' value='Izmeni'>
<?php echo form_close(); ?>
...
My Controller:
function verify_editing_phonebook()
{
if ($this->session->userdata('logged_in'))
{
if ($this->session->userdata('admin') == 1)
{
$this->form_validation->set_rules('ime_prezime', 'Ime i prezime', 'trim|required|xss_clean');
$this->form_validation->set_rules('telefon', 'Telefon', 'trim|required|xss_clean');
if ($this->form_validation->run() == TRUE)
{
$id = $this->uri->segment(3);
if (isset($id) and $id > 0)
{
$this->load->model('LoginModel');
$this->LoginModel->edit_phonebook($id);
redirect(site_url().'ImenikController/', 'refresh');
}
}
else {
$temp = $this->session->userdata('logged_in');
$obj['id'] = $temp['id'];
$data['records'] = $this->LoginModel->get_Username($obj);
$this->load->view('ErrorEditing', $data);
}
}
else {
$this->load->view('restricted_admin');
}
}
else {
$this->load->view('restricted');
}
}
My Model:
function edit_phonebook($id)
{
$data = array ('ime_prezime' => $this->input->post('ime_prezime'),
'ulica' => $this->input->post('ulica'),
'mesto' => $this->input->post('mesto'),
'telefon' => $this->input->post('telefon'));
$this->db->where('id', $id);
$this->db->update('pregled', $data);
}
That solution doesn't work.
I get the url: localhost/imenik114/ImenikController/verify_editing_phonebook
It is a blank (white) page. And not editing row in table.
Basic Debugging Strategies
(1) Have you created all the view files?
(2) Have you tested edit_phonebook($id) independently?
(3) What does redirect(site_url().'ImenikController/', 'refresh'); display?
Did you define the index function for ImenikController?
(4) What URL did you use when you say 'That solution doesn't work.' ?
(5) If your URL is: "localhost/imenik114/ImenikController/verify_editing_phonebook"
you did not type in id in your 3rd segment
(6) If you are not logged in, do you see the correct restricted view?
(7) If you are logged in and NOT admin, do you see the correct restricted_admin view?
Potential Bug
Looking at this part of your code:
if ($this->form_validation->run() == TRUE)
{
$id = $this->uri->segment(3);
if (isset($id) and $id > 0)
{
$this->load->model('LoginModel');
$this->LoginModel->edit_phonebook($id);
redirect(site_url().'ImenikController/', 'refresh');
}
// You need to handle the case of $id not set
else
{
// load a view with error page saying $id is missing...
}
}
if your form validates, and you don't pass in segment(3), your controller will not load a view, therefore, you will get a blank page.
You need to check the case of $id not present, see code.
Code Fix
One more detail: the statement $id = $this->uri->segment(3); will set $id either to the id number or FALSE, therefore, you don't need isset($id) in your if statement. I would write $id = $this->uri->segment(3,0); to set the default to 0 instead of FALSE to keep the logic a bit clearer.
Thanks for answer but I solved my problem somehow.
I made a link in my view:
Edit
And in view for editing:
<?php echo form_open('Controller/verify_editing_phonebook/'.$this->uri->segment(3)); ?>
Function verify_editing_phonebook passed trough validation and loading view.
Thanks once again and sorry for my English...

Joomla Pagination URL is excluding the view=viewname

I have pagination setup on a custom Joomla component. To avoid a page long explanation, we're kind of doing some intricate iframe embedding and forward masking. This is pagination for the front end of the component.
In my iframe, I have a list of puppies (from the custom component). It is paginated. In order for the puppies to display correctly in the iframe it has to have the URL :
http://americasfavoritepuppybreeders.com/puppies/breed/labrador/page-2.html?tmpl=component&view=microsite
However when I actually click on the pagination link for page 2 it drops the view=microsite which causes problems. How can I adjust this so that it does not drop the view=microsite?
The original URL is http://americasfavoritepuppybreeders.com/puppies/breed/labrador/page-1.html?tmpl=component&view=microsite
The code for this pagination is long and between the model, view and view.html.php so it seems difficult for me to post all the related code. Here's some though where I have been looking.
Any ideas or hints on where / how to do this?
Thanks
Zach
// Get the pagination request variables
$limit = $app->input->get('limit', $params->get('display_num', 20), 'uint');
$limitstart = $app->input->get('limitstart', 0, 'uint');
$this->setState('puppies.limit', $limit);
$this->setState('puppies.limitstart', $limitstart);
// Load the parameters.
$this->setState('params', $params);
}
/** Method to get a store id based on the model configuration state. **/
protected function getStoreId($id = '')
{
// Compile the store id.
$id .= ':' . $this->getState('puppies.breed_alias');
$id .= ':' . $this->getState('puppies.limit');
$id .= ':' . $this->getState('puppies.limitstart');
$id .= ':' . serialize($this->getState('puppies.filter'));
$id .= ':' . $this->getState('puppies.featured');
return parent::getStoreId($id);
}
/** Method to get a JPagination object for the data set. **/
public function getPagination()
{
// Create the pagination object.
$limit = (int) $this->getState('puppies.limit');
$page = new JPagination($this->getTotal(), $this->getStart(), $limit);
return $page;
}
/** Method to get the total number of items for the data set. **/
public function getTotal()
{
return $this->items_total;
}
/** Method to get the starting number of items for the data set. **/
public function getStart()
{
$start = $this->getState('puppies.limitstart');
$limit = $this->getState('puppies.limit');
$total = $this->getTotal();
if ($start > $total - $limit)
{
$start = max(0, (int) (ceil($total / $limit) - 1) * $limit);
}
return $start;
}
Again, a portion of the code here but I have no idea what to begin posting for an answer to this so please I will post any code but point me in the right direction, thanks.
Somewhere at the bottom of your "adminform" in that view, there should be the all the hidden inputs that submit the view / controller / token.
Something like this:
<input type="hidden" name="option" value="com_puppies" />
<input type="hidden" name="view" value="microsite" />
<input type="hidden" name="task" value="" />
<input type="hidden" name="boxchecked" value="0" />
<inupt type="hidden" name="controller" value="microsite" />
<input type="hidden" name="filter_order" value="<?php echo $this->escape($this->state->get('list.ordering')); ?>" />
<input type="hidden" name="filter_order_Dir" value="<?php echo $this->escape($this->state->get('list.direction')) ?>" />
<?php echo JHtml::_('form.token'); ?>
Feel free to remove the inputs you won't use (i.e the filter_order ones if you handle that differently). The vital one is the view input. Also, leave the controller input out if you are not using a controller for that view (meaning you are using the default controller for that component)
Can you give this link http://americasfavoritepuppybreeders.com/puppies/breed/labrador/page-1.html?tmpl=component&view=microsite without SEF? you can try to create file /templates/{$your_template}/html/pagination.php with such code
<?php
function pagination_item_active(&$item){
$getData = new JInput($_GET);
$view = $getData->get('view','','string');
$link_part = ($view == 'microsite' ? '&view=microsite' : '');
$link = "<a title=\"" . $item->text . "\" href=\"" . $item->link.$link_part . "\" class=\"pagenav2\">" . $item->text . "</a>";
return $link;
}
function pagination_item_inactive(&$item){
return "<span class=\"pagenav\">" . $item->text . "</span>";
}
Also i think your problem in incorrect link. How you got this link http://americasfavoritepuppybreeders.com/puppies/breed/labrador/page-1.html?tmpl=component&view=microsite? If you use ready link with view=microsite, try create link on your view (microsite) in admin panel and use this link.

Issue with active record update in CodeIgniter

I have a form that submits data to a database in a CodeIgniter app (CI v. 2.0.2). We are allowing users to "edit" records by having them resubmit a record with new values and then doing an update. On submit, the form calls the vote controller's create method. Inside the create method, we check to see if there's already a record based on an entry code and the user's ID. If there is, we update; otherwise we create a new record. The creation works just fine; it's only the update I'm having an issue with. Here's the code.
view
<div id="vote_form">
<?php
$hidden = array('dot_judge_id' => $this->session->userdata('dot_judge_id'));
echo form_open('vote/create');
$entry_code_data = array(
'name' => 'entry_code',
'id' => 'entry_code',
'value' => set_value('entry_code')
);
echo form_hidden($hidden);
$score_options = array('1'=>'1 (lowest)', '2'=>'2','3'=>'3', '4'=>'4','5'=>'5 (highest)');
?>
<p><label for="entry_code">Entry code: </label><?php echo form_input($entry_code_data); ?></p>
<p><label for="q1">Question 1: </label><?php echo form_dropdown('q1', $score_options, ''); ?></p>
<p><label for="q2">Question 2: </label><?php echo form_dropdown('q2', $score_options, ''); ?></p>
<p><label for="q3">Question 3: </label><?php echo form_dropdown('q3', $score_options, ''); ?></p>
<p><label for="q4">Question 4: </label><?php echo form_dropdown('q4', $score_options, ''); ?></p>
<p><label for="q5">Question 5: </label><?php echo form_dropdown('q5', $score_options, ''); ?></p>
<p><?php echo form_submit('submit', 'Submit vote'); ?></p>
<?php echo form_close(); ?>
<?php echo validation_errors(); ?>
</div>
controller
function create() {
$id = $this->input->post('entry_code');
$judge_id = $this->input->post('dot_judge_id');
$data = array(
'entry_code' => $id,
'dot_judge_id' => $judge_id,
'q1' => $this->input->post('q1'),
'q2' => $this->input->post('q2'),
'q3' => $this->input->post('q3'),
'q4' => $this->input->post('q4'),
'q5' => $this->input->post('q5'),
);
//first check to see if there's already a record for this judge/entry
//if so, update. Otherwise, insert
$vote_id = $this->vote_model->getEntryById($id, $judge_id);
if($vote_id) {
log_message('debug', 'vote id exists: '.$vote_id);
$this->vote_model->updateRecord($data, $vote_id);
}
else {
log_message('debug', 'vote id does not exist; creating new');
$this->vote_model->createRecord($data);
}
/*
after submission, go to another page that gives choices - review entries, submit another entry, log out
*/
$data['msg'] = "Entry submitted";
$this->menu();
}
model
function getEntryByID($id, $judge_id) {
//determine if record already exists for entry/judge
$sql = 'SELECT vote_id from dot_vote WHERE entry_code = ? AND dot_judge_id = ?';
$query = $this->db->query($sql, array($id, $judge_id));
if($query->num_rows() == 1) {
$row = $query->row();
return $row->vote_id;
}
else {
return false;
}
}
function createRecord($data) {
$this->db->insert('dot_vote', $data);
return;
}
function updateRecord($data, $vote_id) {
log_message('debug', 'vote id is passed: '.$vote_id);
$this->db->where('vote_id', $vote_id);
$this->update('dot_vote', $data);
}
I know it's making it into the updateRecord method because the log_message output is in my log file displaying the correct vote_id (the auto-increment field of the returned record). But what I get in the browser is the following:
Can anyone point me in the right direction here? I have error display on in my config file, and have gotten SQL errors when they occur, so I don't think this is a SQL error. But I have no idea what could be happening in that tiny little function that's causing this. My next step is to skip the active record update and just use a standard query, but I'd like to know what's causing the problem here, even if I can get it to work the other way.
This line stood out:
$this->update('dot_vote', $data);
Do you mean this?
$this->db->update('dot_vote', $data);

how to echo the database contents in codeigniter while editing

How do i get the form field contents while editing (updating) entries in database ?
my controler is
//edit sidebar contents
function edit_lsidebar(){
if(isset($_POST['heading'])){
//adding text fields
$heading = $this->input->post('heading');
$content_text = $this->input->post('content_text');
$url = $this->input->post('url');
$link_text = $this->input->post('link_text');
$this->Lside_bar_model->edit_lsidebar($heading, $content_text, $url, $link_text);
redirect('welcome');
}
else $this->load->view('edit_lside_bar', $data);
}
my model is
function edit_lsidebar($heading, $content_text, $url, $link_text){
$data = array(
'heading'=>$heading,
'content_text'=>$content_text,
'url'=> $url,
'link_text' => $link_text
);
$this->db->where('id',$this->uri->segment(3));
$this->db->update('lsidebar', $data);
}
please help
When loading your edit_lside_bar view pass the existing $heading, $content_text, $url, $link_text variables with the data array you're passing to the view.
Then inside the view echo those values as a value attribute for input fields. For example:
Inside your controller:
else {
$data["lside_bar"] = $this->Lside_bar_model->get_lside_bar($id);
$this->load->view('edit_lside_bar', $data);
}
Inside your view:
<input type="text" name="heading" value="<?php echo $lside_bar->heading; ?>" />
<textarea name="content_text"><?php echo $lside_bar->content_text; ?></textarea>
....
That should give you a nice push in the right direction. Hope that helps!

Resources