Exemple #1
0
    public function editItem($id, $data)
    {
        $db = JO_Db::getDefaultAdapter();
        $info = self::getItem($id);
        if (!$info) {
            return;
        }
        if (isset($data['free_file'])) {
            $db->update('items', array('free_file' => 'false'));
            self::addUserStatus($id, 'freefile');
        }
        $db->update('items', array('name' => $data['name'], 'description' => $data['description'], 'price' => $data['price'], 'free_file' => isset($data['free_file']) ? 'true' : 'false', 'weekly_from' => $data['weekly_from'] ? JO_Date::getInstance($data['weekly_from'], 'yy-mm-dd', true) : '0000-00-00', 'weekly_to' => $data['weekly_to'] ? JO_Date::getInstance($data['weekly_to'], 'yy-mm-dd', true) : '0000-00-00'), array('id = ?' => (int) $id));
        if (isset($data['set_status']) && $data['set_status'] == 'active') {
            $db->update('items', array('status' => $data['set_status']), array('id = ?' => (int) $id));
            $db->update(Model_Users::getPrefixDB() . 'users', array('items' => new JO_Db_Expr('items + 1')), array('user_id = ?' => $info['user_id']));
            /////////// send email
            $request = JO_Request::getInstance();
            $translate = JO_Translate::getInstance();
            $not_template = Model_Notificationtemplates::get('approval_item');
            $mail = new JO_Mail();
            if (JO_Registry::get('mail_smtp')) {
                $mail->setSMTPParams(JO_Registry::forceGet('mail_smtp_host'), JO_Registry::forceGet('mail_smtp_port'), JO_Registry::forceGet('mail_smtp_user'), JO_Registry::forceGet('mail_smtp_password'));
            }
            $mail->setFrom('no-reply@' . $request->getDomain());
            $href = '<a href="' . WM_Router::create($request->getBaseUrl() . '?module=' . $info['module'] . '&controller=items&item_id=' . $id) . '">' . $info['name'] . '</a>';
            if ($not_template) {
                $title = $not_template['title'];
                $html = html_entity_decode($not_template['template'], ENT_QUOTES, 'utf-8');
                $html = str_replace('{USERNAME}', $info['username'], $html);
                $html = str_replace('{ITEM}', $info['name'], $html);
                $html = str_replace('{URL}', $href, $html);
            } else {
                $title = "[" . $request->getDomain() . "] " . $data['name'];
                $html = nl2br($translate->translate('Item') . ' 
				' . $href . ' ' . $translate->translate('approval'));
            }
            $mail->setSubject($title);
            $mail->setHTML($html);
            $result = $mail->send(array($info['email']), JO_Registry::get('mail_smtp') ? 'smtp' : 'mail');
            unset($mail);
            //////////////////////
        }
        if (isset($data['weekly_to']) && trim($data['weekly_to']) != '') {
            self::addUserStatus($id, 'featured');
        }
        $db->delete('items_to_category', array('item_id = ?' => (int) $id));
        if (isset($data['category_id'])) {
            foreach ($data['category_id'] as $category_id) {
                $categories = Model_Categories::getCategoryParents(Model_Categories::getCategories(array('filter_id_key' => true)), $category_id);
                $categories = explode(',', $categories);
                array_pop($categories);
                $categories = array_reverse($categories);
                $categories = ',' . implode(',', $categories) . ',';
                $db->insert('items_to_category', array('item_id' => (int) $id, 'categories' => $categories));
            }
        }
        $db->delete('items_attributes', array('item_id = ?' => (int) $id));
        if (isset($data['attributes']) && is_array($data['attributes'])) {
            foreach ($data['attributes'] as $cid => $value) {
                if (is_array($value)) {
                    foreach ($value as $val) {
                        $db->insert('items_attributes', array('item_id' => $id, 'attribute_id' => $val, 'category_id' => (int) $cid));
                    }
                } elseif ($value) {
                    $db->insert('items_attributes', array('item_id' => $id, 'attribute_id' => $value, 'category_id' => (int) $cid));
                }
            }
        }
        $db->delete('items_tags', array('item_id = ?' => (int) $id));
        if (isset($data['tags']) && $data['tags']) {
            $tags = explode(',', $data['tags']);
            foreach ($tags as $tag) {
                $tag = trim($tag);
                if ($tag) {
                    $tag_id = Model_Tags::getTagByTitleAndInsert($tag);
                    if ($tag_id) {
                        $db->insert('items_tags', array('item_id' => $id, 'tag_id' => (int) $tag_id, 'type' => ''));
                    }
                }
            }
        }
    }
Exemple #2
0
    public function editItem($id, $data)
    {
        set_time_limit(0);
        $db = JO_Db::getDefaultAdapter();
        $info = self::getItem($id);
        if (!$info) {
            return;
        }
        if (isset($data['free_file'])) {
            $db->update('items', array('free_file' => 'false'));
            self::addUserStatus($id, 'freefile');
        }
        $db->update('items', array('name' => $data['name'], 'description' => $data['description'], 'price' => $data['price'][$data['default_price']], 'free_file' => isset($data['free_file']) ? 'true' : 'false', 'item_tags_string' => isset($data['tags']) ? $data['tags'] : '', 'weekly_from' => $data['weekly_from'] ? JO_Date::getInstance($data['weekly_from'], 'yy-mm-dd', true) : '0000-00-00', 'weekly_to' => $data['weekly_to'] ? JO_Date::getInstance($data['weekly_to'], 'yy-mm-dd', true) : '0000-00-00'), array('id = ?' => (int) $id));
        if (isset($data['set_status']) && $data['set_status'] == 'active') {
            $db->update('items', array('status' => $data['set_status']), array('id = ?' => (int) $id));
            /////////// send email
            $request = JO_Request::getInstance();
            $translate = JO_Translate::getInstance();
            $not_template = Model_Notificationtemplates::get('approval_item');
            $mail = new JO_Mail();
            if (JO_Registry::get('mail_smtp')) {
                $mail->setSMTPParams(JO_Registry::forceGet('mail_smtp_host'), JO_Registry::forceGet('mail_smtp_port'), JO_Registry::forceGet('mail_smtp_user'), JO_Registry::forceGet('mail_smtp_password'));
            }
            $mail->setFrom('no-reply@' . $request->getDomain());
            $href = '<a href="' . WM_Router::create($request->getBaseUrl() . '?controller=items&item_id=' . $id) . '">' . $info['name'] . '</a>';
            if ($not_template) {
                $title = $not_template['title'];
                $html = html_entity_decode($not_template['template'], ENT_QUOTES, 'utf-8');
                $html = str_replace('{USERNAME}', $info['username'], $html);
                $html = str_replace('{ITEM}', $info['name'], $html);
                $html = str_replace('{URL}', $href, $html);
            } else {
                $title = "[" . $request->getDomain() . "] " . $data['name'];
                $html = nl2br($translate->translate('Item') . ' 
				' . $href . ' ' . $translate->translate('approval'));
            }
            $mail->setSubject($title);
            $mail->setHTML($html);
            $result = $mail->send(array($info['email']), JO_Registry::get('mail_smtp') ? 'smtp' : 'mail');
            unset($mail);
            //////////////////////
        }
        if (isset($data['weekly_to']) && trim($data['weekly_to']) != '') {
            self::addUserStatus($id, 'featured');
        }
        $db->delete('items_to_category', array('item_id = ?' => (int) $id));
        if (isset($data['category_id'])) {
            foreach ($data['category_id'] as $category_id) {
                $categories = Model_Categories::getCategoryParents(Model_Categories::getCategories(array('filter_id_key' => true)), $category_id);
                $categories = explode(',', $categories);
                array_pop($categories);
                $categories = array_reverse($categories);
                $categories = ',' . implode(',', $categories) . ',';
                $db->insert('items_to_category', array('item_id' => (int) $id, 'categories' => $categories));
            }
        }
        $db->delete('items_attributes', array('item_id = ?' => (int) $id));
        if (isset($data['attributes']) && is_array($data['attributes'])) {
            foreach ($data['attributes'] as $cid => $value) {
                if (is_array($value)) {
                    foreach ($value as $val) {
                        $db->insert('items_attributes', array('item_id' => $id, 'attribute_id' => $val, 'category_id' => (int) $cid));
                    }
                } elseif ($value) {
                    $db->insert('items_attributes', array('item_id' => $id, 'attribute_id' => $value, 'category_id' => (int) $cid));
                }
            }
        }
        $db->delete('items_tags', array('item_id = ?' => (int) $id));
        if (isset($data['tags']) && $data['tags']) {
            $tags = explode(',', $data['tags']);
            foreach ($tags as $tag) {
                $tag = trim($tag);
                if ($tag) {
                    $tag_id = Model_Tags::getTagByTitleAndInsert($tag);
                    if ($tag_id) {
                        $db->insert('items_tags', array('item_id' => $id, 'tag_id' => (int) $tag_id, 'type' => ''));
                    }
                }
            }
        }
        $sizes = Model_Sizes::getAll();
        $tmp_sizes = array();
        foreach ($sizes as $size1) {
            $tmp_sizes[$size1['id']] = $size1;
        }
        $info_file = getimagesize(BASE_PATH . '/uploads/' . $info['main_file']);
        $steps = 0;
        if ($info_file[0] < $info_file[1]) {
            $type = 'p';
            $steps = $info_file[0];
            $source_aspect_ratio = round($info_file[1] / $info_file[0], 5);
        } elseif ($info_file[0] > $info_file[1]) {
            $type = 'l';
            $steps = $info_file[1];
            $source_aspect_ratio = round($info_file[0] / $info_file[1], 5);
        } else {
            $type = 'k';
            $steps = $info_file[0];
            $source_aspect_ratio = 1;
        }
        $temp_sizes = array();
        $temp_sizes2 = array();
        $deleted = array();
        foreach ($data['price'] as $size_id => $price) {
            if (trim($price) && (double) $price && isset($tmp_sizes[$size_id])) {
                $sizeMP = $tmp_sizes[$size_id]['size'] * 1000000;
                $sizeMPFrom = $sizeMP - $sizeMP / 100;
                for ($i = $steps; $i >= 1; $i--) {
                    if ($type == 'p') {
                        $width = $info_file[0] - $i;
                        $height = round($width * $source_aspect_ratio);
                    } elseif ($type == 'l') {
                        $height = $info_file[1] - $i;
                        $width = round($height * $source_aspect_ratio);
                    } else {
                        $width = $info_file[0] - $i;
                        $height = $info_file[1] - $i;
                    }
                    if ($width < 1 || $height < 1) {
                        continue;
                    }
                    if ($width * $height >= $sizeMPFrom && $width * $height <= $sizeMP) {
                        $temp_sizes[$size_id] = array('width' => $width, 'height' => $height, 'price' => $price, 'size_id' => $size_id, 'size' => $tmp_sizes[$size_id]['size']);
                        $temp_sizes2[$size_id] = true;
                        continue;
                    }
                }
            } else {
                $deleted[] = $size_id;
            }
        }
        $query_files = $db->select()->from('items_prices')->where('item_id = ?', (int) $id);
        $list_files = $db->fetchAll($query_files);
        if ($list_files) {
            foreach ($list_files as $fils) {
                if (in_array($fils['size_id'], $deleted)) {
                    if (!self::fileIsOrdered($id, $fils['size_id'])) {
                        unlink(BASE_PATH . '/uploads/' . $fils['main_file']);
                        $db->delete('items_prices', array('id = ?' => (int) $fils['id']));
                    } else {
                        $db->delete('items_prices', array('id = ?' => (int) $fils['id']));
                    }
                } elseif (!in_array($fils['size_id'], $temp_sizes2)) {
                    if (!self::fileIsOrdered($id, $fils['size_id'])) {
                        unlink(BASE_PATH . '/uploads/' . $fils['main_file']);
                        $db->delete('items_prices', array('id = ?' => (int) $fils['id']));
                    } else {
                        $db->delete('items_prices', array('id = ?' => (int) $fils['id']));
                    }
                } else {
                    if (file_exists(BASE_PATH . '/uploads/' . $fils['main_file'])) {
                        $db->update('items_prices', array('price' => (double) $temp_sizes[$fils['size_id']]['price']), array('id = ?' => (int) $fils['id']));
                        unset($temp_sizes[$fils['size_id']]);
                    } else {
                        $db->delete('items_prices', array('id = ?' => (int) $fils['id']));
                    }
                }
            }
        }
        $main_file = BASE_PATH . '/uploads/' . $info['main_file'];
        $main_path = dirname($info['main_file']);
        foreach ($temp_sizes as $key => $value) {
            $name_new = md5(time() . '_' . mt_rand()) . '.' . round($key, 2) . strtolower(strrchr(basename($info['main_file']), '.'));
            $ext = strtolower(strrchr($info['main_file'], '.'));
            $image_p = imagecreatetruecolor($value['width'], $value['height']);
            $image = null;
            if ($ext == '.jpg' || $ext == '.jpeg') {
                $image = imagecreatefromjpeg(BASE_PATH . '/uploads/' . $info['main_file']);
            } else {
                continue;
            }
            imageantialias($image_p, true);
            if ($image) {
                imagecopyresampled($image_p, $image, 0, 0, 0, 0, $value['width'], $value['height'], $info_file[0], $info_file[1]);
            }
            if (!file_exists(dirname($main_file) . '/downloads/')) {
                mkdir(dirname($main_file) . '/downloads/', 0777, true);
            }
            imagejpeg($image_p, dirname($main_file) . '/downloads/' . $name_new, 100);
            if (file_exists(dirname($main_file) . '/downloads/' . $name_new)) {
                $db->insert('items_prices', array('item_id' => (int) $id, 'size_id' => (int) $value['size_id'], 'price' => (double) $value['price'], 'main_file' => $main_path . '/downloads/' . $name_new, 'size' => $key, 'width' => $value['width'], 'height' => $value['height']));
            }
        }
    }