示例#1
0
function savecat($option)
{
    global $mtconf, $mainframe;
    $database =& JFactory::getDBO();
    $my =& JFactory::getUser();
    $jdate = JFactory::getDate();
    $now = $jdate->toMySQL();
    $template_all_subcats = JRequest::getInt('template_all_subcats', 0, 'post');
    $related_cats = explode(',', JRequest::getVar('other_cats', '', 'post'));
    $remove_image = JRequest::getInt('remove_image', 0);
    $cat_image = JRequest::getVar('cat_image', null, 'files');
    if ($related_cats[0] == '') {
        $related_cats = array();
    }
    $post = JRequest::get('post');
    $post['cat_desc'] = JRequest::getVar('cat_desc', '', 'POST', 'string', JREQUEST_ALLOWHTML);
    $row = new mtCats($database);
    if (!$row->bind($post)) {
        echo "<script> alert('" . $row->getError() . "'); window.history.go(-1); </script>\n";
        exit;
    }
    if (empty($row->alias)) {
        $row->alias = JFilterOutput::stringURLSafe($row->cat_name);
    }
    # Get the name of the old photo
    if ($row->cat_id > 0) {
        $sql = 'SELECT cat_image FROM #__mt_cats WHERE cat_id = ' . $database->quote($row->cat_id);
        $database->setQuery($sql);
        $old_image = $database->loadResult();
    } else {
        $old_image = '';
    }
    # Remove previous old image
    $msg = '';
    if ($remove_image || $old_image != '' && array_key_exists('tmp_name', $cat_image) && !empty($cat_image['tmp_name'])) {
        $row->cat_image = '';
        if (file_exists($mtconf->getjconf('absolute_path') . $mtconf->get('relative_path_to_cat_original_image') . $old_image) && file_exists($mtconf->getjconf('absolute_path') . $mtconf->get('relative_path_to_cat_small_image') . $old_image) && is_writable($mtconf->getjconf('absolute_path') . $mtconf->get('relative_path_to_cat_small_image')) && is_writable($mtconf->getjconf('absolute_path') . $mtconf->get('relative_path_to_cat_original_image'))) {
            if (!unlink($mtconf->getjconf('absolute_path') . $mtconf->get('relative_path_to_cat_original_image') . $old_image) || !unlink($mtconf->getjconf('absolute_path') . $mtconf->get('relative_path_to_cat_small_image') . $old_image)) {
                $msg .= JText::_('Error deleting old image');
            }
        }
    }
    # Create Thumbnail
    if ($cat_image['name'] != '') {
        if (!is_writable($mtconf->getjconf('absolute_path') . $mtconf->get('relative_path_to_cat_small_image')) || !is_writable($mtconf->getjconf('absolute_path') . $mtconf->get('relative_path_to_cat_original_image'))) {
            $msg .= JText::_('Image directories not writable');
        } else {
            $mtImage = new mtImage();
            $mtImage->setDirectory($mtconf->getjconf('absolute_path') . $mtconf->get('relative_path_to_cat_small_image'));
            $mtImage->setMethod($mtconf->get('resize_method'));
            $mtImage->setQuality($mtconf->get('resize_quality'));
            $mtImage->setSize($mtconf->get('resize_cat_size'));
            $mtImage->setTmpFile($cat_image['tmp_name']);
            $mtImage->setType($cat_image['type']);
            if ($row->cat_id > 0) {
                $mtImage->setName($row->cat_id . '_' . $cat_image['name']);
                $row->cat_image = $row->cat_id . '_' . $cat_image['name'];
            } else {
                $mtImage->setName($cat_image['name']);
                $row->cat_image = $cat_image['name'];
            }
            $mtImage->setSquare(false);
            $mtImage->resize();
            $mtImage->saveToDirectory();
            move_uploaded_file($cat_image['tmp_name'], $mtconf->getjconf('absolute_path') . $mtconf->get('relative_path_to_cat_original_image') . $row->cat_image);
        }
    }
    # Is this a new category?
    // Category created by conventional "Add Category" link
    if ($row->cat_id == 0) {
        $new_cat = true;
        $row->cat_created = $now;
    } else {
        $database->setQuery('SELECT cat_approved FROM #__mt_cats WHERE cat_id = ' . $database->quote($row->cat_id));
        $cat_approved = $database->loadResult();
        // Approved new category submitted by users
        if ($row->cat_approved == 1 && $cat_approved == 0 && $row->lft == 0 && $row->rgt == 0) {
            $new_cat = true;
            $row->cat_created = $now;
        } else {
            $new_cat = false;
        }
    }
    if (!$row->store()) {
        echo "<script> alert('" . $row->getError() . "'); window.history.go(-1); </script>\n";
        exit;
    } else {
        # If this is a newlink, rename the photo to listingID_photoName.jpg
        if ($new_cat && $cat_image['name'] != '') {
            // Get last inserted listing ID
            $mysql_last_insert_id = $database->insertid();
            if ($mysql_last_insert_id > 0) {
                if (rename($mtconf->getjconf('absolute_path') . $mtconf->get('cat_image_dir') . $cat_image['name'], $mtconf->getjconf('absolute_path') . $mtconf->get('cat_image_dir') . $mysql_last_insert_id . "_" . $cat_image['name'])) {
                    $database->setQuery('UPDATE #__mt_cats SET cat_image = ' . $database->quote($mysql_last_insert_id . "_" . $cat_image['name']) . ' WHERE cat_id = ' . $database->quote($mysql_last_insert_id) . ' LIMIT 1');
                    $database->query();
                }
            }
        }
    }
    # Change all subcats to use this template
    if ($template_all_subcats == 1) {
        $row->updateSubCatsTemplate();
    }
    # Update the Category Counts for all cat_parent(s)
    if ($new_cat) {
        $row->updateLftRgt();
        $row->updateCatCount(1);
    }
    $row->reorder("cat_parent='{$row->cat_parent}'");
    # Update the related categories
    $mtRelCats = new mtRelCats($database);
    $mtRelCats->setcatid($row->cat_id);
    $mtRelCats->update($related_cats);
    $returntask = JRequest::getCmd('returntask', '', 'post');
    // /*
    if ($returntask != '') {
        $mainframe->redirect("index2.php?option={$option}&task={$returntask}", $msg);
    } else {
        $task = JRequest::getCmd('task', '', 'post');
        if ($task == "applycat") {
            $mainframe->redirect("index2.php?option={$option}&task=editcat&cat_id={$row->cat_id}", $msg);
        } else {
            $mainframe->redirect("index2.php?option={$option}&task=listcats&cat_id={$row->cat_parent}", $msg);
        }
    }
    // */
}