Файл: adultscript-2.0.3-pro/files/admin/modules/photo/components/category_add.php
Строк: 64
<?php
defined('_VALID') or die('Restricted Access!');
define('IMG_WIDTH', 175);
define('IMG_HEIGHT', 215);
class VComponent_Admin_photo_category_add
{
public function __construct()
{
}
public function render()
{
$errors = array();
$messages = array();
$db = VF::factory('database');
$cat = array('name' => '', 'slug' => '', 'description' => '', 'status' => '1');
if (isset($_POST['submit_add_category'])) {
$filter = &VF::factory('filter');
$name = $filter->get('name');
$slug = $filter->get('slug');
$description = trim($_POST['description']);
$status = $filter->get('status', 'INT');
if ($name == '') {
$errors[] = 'Category name field cannot be left blank!';
} elseif (!VValid::length($name, 1, 100)) {
$errors[] = 'Category name cat contain maximum 100 characters!';
} else {
$db->query("SELECT cat_id FROM #__photo_categories WHERE name = '".$db->escape($name)."' LIMIT 1");
if ($db->affected_rows()) {
$errors[] = 'Category name already used for another category!';
} else {
$cat['name'] = $name;
}
}
if ($slug == '') {
$errors[] = 'Category slug field cannot be left blank!';
} elseif (!VValid::length($slug, 1, 100)) {
$errors[] = 'Category slug can contain maximum 100 characters!';
} elseif (!VValid::slug($slug)) {
$errors[] = 'Category slug can contain only lower case letters, numbers and dashes!';
} else {
$db->query("SELECT cat_id FROM #__photo_categories WHERE slug = '".$db->escape($slug)."' LIMIT 1");
if ($db->affected_rows()) {
$errors[] = 'Category slug is already used for another category!';
} else {
$cat['slug'] = $slug;
}
}
if ($description != '') {
$cat['description'] = $description;
}
if (isset($_FILES['image']['tmp_name'])) {
if (!$file = VUpload::process('image', 2, array('jpg', 'jpeg', 'png', 'gif'))) {
$errors = array_merge($errors, VUpload::error());
}
} else {
$errors[] = 'Please upload a category image!';
}
$cat['status'] = $status;
if (!$errors) {
$db->query("INSERT INTO #__photo_categories
SET name = '".$db->escape($name)."',
description = '".$db->escape($description)."',
slug = '".$db->escape($slug)."',
status = '".$status."'");
if ($db->affected_rows()) {
$cat_id = $db->get_last_insert_id('#__photo_categories');
$image = VF::factory('image');
$dst = MEDIA_DIR.'/photos/cat/'.$cat_id.'.jpg';
if ($image->load($file['path']) &&
$image->resize(IMG_WIDTH, IMG_HEIGHT, 'EXACT', $dst)) {
$messages[] = 'Category added!';
} else {
$errors[] = 'Failed to add category image! Aborting...';
}
} else {
$errors[] = 'Failed to add category! Application Error!?';
}
}
}
$tpl = VF::factory('template');
$tpl->menu = 'photo';
$tpl->submenu = 'photo_category_add';
$tpl->meta_title = 'Admin::Photo::Category::Add';
$tpl->errors = $errors;
$tpl->messages = $messages;
$tpl->cat = $cat;
$tpl->load(array('header', 'photo_category_add', 'footer'));
$tpl->display();
}
}
?>