Реализован AJAX метод добавления точки. Немного доработана модель.

This commit is contained in:
Krivchikov Dmitry 2014-04-08 11:02:04 +04:00
parent 8d1ef522dd
commit 85b7b234cc
4 changed files with 50 additions and 8 deletions

View File

@ -60,7 +60,14 @@ class Model
{
$request = App::$DB->prepare('INSERT INTO ' . $this->_tableName_ . ' (' . implode(',', array_keys($values)) . ') values (:' . implode(', :', array_keys($values)) . ')');
return $request->execute((array) $values);
if ( $request->execute((array) $values) )
{
return $this->getByPK(App::$DB->lastInsertId());
}
else
{
return false;
}
}
function save()

View File

@ -20,10 +20,4 @@ class IndexController extends Controller
));
}
static function actionAddpoint()
{
$point = new Point();
$point->add($_POST);
App::redirect('/');
}
}

View File

@ -60,4 +60,45 @@ class JsonController extends Controller
'data' => $categories,
));
}
/**
* Добавление точки
*/
static function actionAddPoint()
{
$errors = array();
if(!( isset($_POST['name']) && $_POST['name'] && strlen($_POST['name']) > 1 )) $errors[]='Неверное название точки';
if(!( isset($_POST['img']) && $_POST['img'] && strlen($_POST['img']) > 13 )) $errors[]='Плохая ссылка на изображение';
if(!( isset($_POST['description']) && $_POST['description'] && strlen($_POST['description']) > 25 )) $errors[]='Слишком короткое описание';
if(!( isset($_POST['categoryId']) && $_POST['categoryId'] )) $errors[]='Не задана категория точки';
if(!( isset($_POST['source']) )) $errors[]='Не передано поле источкика информации. Допускается пустое значение.';
if(!( isset($_POST['lat']) && $_POST['lat'] )) $errors[]='Ошибочное значение широты';
if(!( isset($_POST['lng']) && $_POST['lng'] )) $errors[]='Ошибочное значение долготы';
if (empty($errors))
{
$point = new Point();
$point->add($_POST);
if ( $point->id )
{
self::renderPartial('json.php', array(
'data' => $point->id
));
}
else
{
header("HTTP/1.0 500 Internal Server Error");
die('Error 500 - Internal Server Error');
}
}
else
{
header("HTTP/1.0 400 Bad Request");
header('Content-Type: application/json');
print json_encode($errors);
}
}
}

View File

@ -27,7 +27,7 @@ function init() {
myMap.balloon.open(coords, {
contentHeader:'Добавление точки',
contentBody:
'<form method="POST" action="/index/addpoint/" width="400px" height="300px">' +
'<form method="POST" action="/json/addpoint/" width="400px" height="300px">' +
'<p>Название:<br/>' +
'<input type="text" id="name" name="name" /></p>' +
'<p>URL изображения:<br/>' +