153 lines
4.9 KiB
PHP
153 lines
4.9 KiB
PHP
<?php
|
|
|
|
/**
|
|
* This is the model class for table "{{reservation_images}}".
|
|
*
|
|
* @property integer $id
|
|
* @property string $filename
|
|
* @property string $fullpath
|
|
* @property string $image_type
|
|
* @property integer $user_id
|
|
* @property integer $reservation_id
|
|
* @property string $created_at
|
|
* @property string $updated_at
|
|
* @property bool $newRecord
|
|
*/
|
|
class ReservationImage implements ImportRecord
|
|
{
|
|
public $id;
|
|
public $filename;
|
|
public $image_type;
|
|
public $fullpath;
|
|
public $user_id;
|
|
public $reservation_id;
|
|
public $created_at;
|
|
public $updated_at;
|
|
public $newRecord;
|
|
|
|
/**
|
|
* ReservationImage constructor.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->newRecord = true;
|
|
}
|
|
|
|
|
|
public static function createImage(Reservation $reservation, $type, $path)
|
|
{
|
|
assert('!empty($path)');
|
|
|
|
$image = new self;
|
|
$image->user_id = $reservation->user_id;
|
|
$image->image_type = $type;
|
|
$image->reservation_id = $reservation->id;
|
|
$image->fullpath = $path;
|
|
$image->filename = (string)substr(pathinfo($path, PATHINFO_BASENAME), 0, 10);
|
|
$image->save();
|
|
|
|
return $image;
|
|
}
|
|
|
|
public function save()
|
|
{
|
|
try {
|
|
if ($this->newRecord) {
|
|
if (!isset($this->id)) {
|
|
$sql = "INSERT INTO reservation_images (user_id, image_type, filename, fullpath, reservation_id, created_at) VALUES ('" . $this->user_id .
|
|
"', '" . $this->image_type . "', '" . $this->filename . "', '" . $this->fullpath . "', '" . $this->reservation_id . "', NOW())";
|
|
} else {
|
|
$sql = "INSERT INTO reservation_images (id, user_id, image_type, filename, fullpath, reservation_id, created_at) VALUES ('" . $this->id . "', '" . $this->user_id .
|
|
"', '" . $this->image_type . "', '" . $this->filename . "', '" . $this->fullpath . "', '" . $this->reservation_id . "', NOW())";
|
|
}
|
|
mysql_query($sql);
|
|
$err = mysql_errno();
|
|
if ($err > 0) {
|
|
echo mysql_error();
|
|
return false;
|
|
}
|
|
if (!isset($this->id)) {
|
|
$this->id = mysql_insert_id();
|
|
}
|
|
} else {
|
|
throw new Exception("update reservation image not implemented");
|
|
}
|
|
|
|
return true;
|
|
} catch (Exception $e) {
|
|
echo $e->getMessage();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public function getIsNewRecord()
|
|
{
|
|
return $this->newRecord;
|
|
}
|
|
|
|
public static function deleteAll($condition = '')
|
|
{
|
|
throw new Exception("deleteALL not implemented");
|
|
}
|
|
|
|
public static function className()
|
|
{
|
|
return get_called_class();
|
|
}
|
|
|
|
public static function findOne($id)
|
|
{
|
|
$sql = "SELECT * FROM reservation_images WHERE id = '$id'";
|
|
$rez = mysql_query($sql);
|
|
if (mysql_num_rows($rez)) {
|
|
$reservationImage = new ReservationImage();
|
|
$reservationImage->newRecord = false;
|
|
$dbObject = mysql_fetch_array($rez);
|
|
$reservationImage->id = $dbObject['id'];
|
|
$reservationImage->filename = $dbObject['filename'];
|
|
$reservationImage->image_type = $dbObject['image_type'];
|
|
$reservationImage->fullpath = $dbObject['fullpath'];
|
|
$reservationImage->user_id = $dbObject['user_id'];
|
|
$reservationImage->reservation_id = $dbObject['reservation_id'];
|
|
$reservationImage->created_at = $dbObject['created_at'];
|
|
$reservationImage->updated_at = $dbObject['updated_at'];
|
|
|
|
return $reservationImage;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* @param $reservationId - ИД
|
|
* @return ReservationImage[]
|
|
*/
|
|
public static function findForReservation($reservationId)
|
|
{
|
|
$sql = "SELECT * FROM reservation_images WHERE reservation_id = '$reservationId'";
|
|
$rez = mysql_query($sql);
|
|
|
|
$reservationImages = array();
|
|
|
|
if (mysql_num_rows($rez)) {
|
|
while ($dbObject = mysql_fetch_assoc($rez)) {
|
|
$reservationImage = new ReservationImage();
|
|
$reservationImage->newRecord = false;
|
|
$reservationImage->id = $dbObject['id'];
|
|
$reservationImage->filename = $dbObject['filename'];
|
|
$reservationImage->image_type = $dbObject['image_type'];
|
|
$reservationImage->fullpath = $dbObject['fullpath'];
|
|
$reservationImage->user_id = $dbObject['user_id'];
|
|
$reservationImage->reservation_id = $dbObject['reservation_id'];
|
|
$reservationImage->created_at = $dbObject['created_at'];
|
|
$reservationImage->updated_at = $dbObject['updated_at'];
|
|
|
|
$reservationImages[] = $reservationImage;
|
|
}
|
|
}
|
|
|
|
return $reservationImages;
|
|
}
|
|
|
|
}
|