[eluser]RMinor[/eluser]
Hello, I am refactoring my models to use Jamie Rumbelow's MY_Model. I've run into a snag with some custom methods that need to be called when inserting a record. I am modifying my model that adds information about a photo album. Each time an album is created I want to create a unique code (for the mobile application to upload to it), save the title as a slug, insert dynamically created local and GMT timestamps based on the user's input of date and time, and insert create and update timestamps. I can see from his book how to do the create and update timestamps, but how would I do the rest? How do I pass the user's title to the create_slug method. How would I use the create_timestamp method on insert? My model is below:
Code: <?php
class Album_model extends MY_Model {
public $belongs_to = array('profile');
public $has_many = array('photo');
public $validation = array(
array('field' => 'title', 'label' => 'Title', 'rules' => 'trim|required'),
array('field' => 'description', 'label' => 'Description', 'rules' => 'trim|required'),
array('field' => 'date', 'label' => 'Date', 'rules' => 'trim|required'),
array('field' => 'time', 'label' => 'Time', 'rules' => 'trim|required'),
array('field' => 'city', 'label' => 'City', 'rules' => 'trim|required'),
array('field' => 'state', 'label' => 'State', 'rules' => 'trim|required'),
array('field' => 'zip_code', 'label' => 'Zip Code', 'rules' => 'trim|required'),
array('field' => 'private', 'label' => 'Private', 'rules' => 'trim|required'),
array('field' => 'mobile_uploads', 'label' => 'Mobile Uploads', 'rules' => 'trim|required'),
);
public $before_create = array('create_timestamps', 'create_slug');
public $before_update = array('update_timestamps');
public function __construct() {
parent::__construct();
}
/**
* Method to insert timestamps
* @param type $row
* @return type
*/
protected function create_timestamps($row) {
$row['created_at'] = $row['updated_at'] = date('Y-m-d H:i:s');
return $row;
}
/**
* Method to update timestamp
* @param type $row
* @return type
*/
protected function update_timestamps($row) {
$row['updated_at'] = date('Y-m-d H:i:s');
return $row;
}
/**
* Method to create an album slug
* @param string $title - This is the user input title, how would I use this properly here
* @return string
*/
protected function create_slug($row, $title) {
$row['slug'] = url_title($title, '-', true);
return $row;
}
/**
* Method to create a mobile application album code
* @param integer $length
* @return string
*/
protected function create_mobile_code($length = 6) {
$album_code = '';
$possible = "2346789bcdfghjkmnpqrtvwxyzBCDFGHJKLMNPQRTVWXYZ";
$max_length = strlen($possible);
if ($length > $max_length) {
$length = $max_length;
}
$i = 0;
while ($i < $length) {
$character = substr($possible, mt_rand(0, $max_length-1), 1);
if (!strstr($album_code, $character)) {
$album_code .= $character;
$i++;
}
}
$this->db->select('album_code');
$this->db->where('album_code', $album_code);
$query = $this->db->get('album');
if ($query->num_rows == 0) {
return $album_code;
} else {
$this->create_mobile_code();
}
}
/**
* Method to create a MySQL timestamp (local and GMT) to be used by the mobile application
* @param string $date
* @param string $time
* @ return string
*/
protected function create_album_timestamp($date, $time) {
if (!$date || !$time) {
return '';
}
$pm = strrpos($time, 'PM');
if ($pm === FALSE) {
$parts = explode(':', $time);
$hour = $parts[0];
switch ($hour) {
case '0':
$hour = '00';
break;
case '1':
$hour = '01';
break;
case '2':
$hour = '02';
break;
case '3':
$hour = '03';
break;
case '4':
$hour = '04';
break;
case '5':
$hour = '05';
break;
case '6':
$hour = '06';
break;
case '7':
$hour = '07';
break;
case '8':
$hour = '08';
break;
case '9':
$hour = '09';
break;
}
$more_parts = explode(' ', $parts[1]);
$new_time = $hour . ':' . $more_parts[0] . ':00';
$timestamp = $date . ' ' . $new_time;
} else {
$parts = explode(':', $time);
$hour = $parts[0] + 12;
$more_parts = explode(' ', $parts[1]);
$new_time = $hour . ':' . $more_parts[0] . ':00';
$timestamp = $date . ' ' . $new_time;
}
return strtotime($timestamp);
}
/**
* Method to create an album
*
* In the future we may want to store both local timestamps and GMT
* timestamps
*
* @param array $create_array
* @param int $user_id
* @return int or boolean
*/
public function create($create_array, $user_id) {
if (!is_numeric($user_id)) {
throw new Exception('The user id must be an integer.');
}
$album_code = $this->create_mobile_code();
$slug = url_title($create_array['album_title'], '-', TRUE);
$defaults = array(
'title' => '',
'slug' => $slug,
'description' => '',
'date' => '',
'time' => '',
'city' => '',
'state' => '',
'zip_code' => '',
'local_timestamp' => '',
'gmt_timestamp' => '',
'mobile_code' => $album_code,
'profile_id' => $user_id,
'private' => '',
'mobile_uploads' => '',
);
$data = array_merge($defaults, $create_array);
$data['local_timestamp'] = $this->_convertTime($create_array['album_date'], $create_array['album_time']);
$data['gmt_timestamp'] = gmdate($this->_convertTime($create_array['album_date'], $create_array['album_time']));
$this->db->insert('album', $data);
if ($this->db->affected_rows() == 1) {
return $slug;
}
return FALSE;
}
}
|