/** * saves the location in a specific order and change the parent * @return void */ public function action_saveorder() { $this->auto_render = FALSE; $this->template = View::factory('js'); $loc = new Model_Location(core::get('id_location')); if ($loc->loaded()) { //saves the current location $loc->id_location_parent = core::get('id_location_parent'); $loc->parent_deep = core::get('deep'); //saves the categories in the same parent the new orders $order = 0; foreach (core::get('brothers') as $id_loc) { $id_loc = substr($id_loc, 3); //removing the li_ to get the integer //not the main location so loading and saving if ($id_loc != core::get('id_location')) { $c = new Model_Location($id_loc); $c->order = $order; $c->save(); } else { //saves the main location $loc->order = $order; $loc->save(); } $order++; } $this->template->content = __('Saved'); } else { $this->template->content = __('Error'); } }
/** * Add New Location * * @param array $locationParts Array of Location Parts * @return bool | int */ public function addLocation($locationParts) { $newLocation = new Model_Location(); $newLocation->fromArray($locationParts); try { $newLocation->save(); } catch (Doctrine_Exception $e) { return $e->getMessage(); } return $newLocation->id; }
private function update_location($sub, $ip) { $rack = $ip->network->device->racks; $room = $rack->rooms; $floor = $room->floors; $building = $floor->buildings; $loc = Model_Location::find()->where('rack', $rack['id'])->where('node', $sub->id)->get_one(); if (!$loc) { $in = array('node' => $sub->id, 'type' => 0, 'building' => $building['id'], 'floor' => $floor['id'], 'room' => $room['id'], 'rack' => $rack['id'], 'pos_from' => 1, 'pos_to' => $rack['size']); $location = new Model_Location($in); $location->save(); } }
public function action_index() { if ($this->id) { $val2 = \Validation::forge('data'); $val2->add_field('name', 'subnet name', 'required|min_length[1]|max_length[50]'); if ($val2->run()) { $this->name = $val2->validated('name'); $data = \Input::post('data'); $m = explode(',', $data); $stat = $this->checkForInhert($m); if ($stat['stat']) { $in = array('node' => $this->name, 'type' => 0, 'building' => $m[0], 'floor' => $m[1], 'room' => $m[2], 'rack' => $m[3], 'pos_from' => $m[4], 'pos_to' => $m[5]); $location = new Model_Location($in); $location->save(); $o = $this->structure(); echo json_encode(array('stat' => $stat, 'data' => $o)); } else { echo json_encode(array('stat' => $stat)); } } } }
/** * This function will upgrade DB that didn't existed in versions prior to 2.3.0 */ public function action_230() { //Cron update try { DB::query(Database::UPDATE, "UPDATE `" . self::$db_prefix . "crontab` SET period='00 3 * * *' WHERE callback='Sitemap::generate' LIMIT 1")->execute(); DB::query(Database::UPDATE, "UPDATE `" . self::$db_prefix . "crontab` SET period='00 5 * * *' WHERE callback='Core::delete_cache' LIMIT 1")->execute(); DB::query(Database::UPDATE, "UPDATE `" . self::$db_prefix . "crontab` SET period='00 4 1 * *' WHERE callback='Core::optimize_db' LIMIT 1")->execute(); DB::query(Database::UPDATE, "UPDATE `" . self::$db_prefix . "crontab` SET period='00 7 * * *' WHERE callback='Cron_Ad::unpaid' LIMIT 1")->execute(); DB::query(Database::UPDATE, "UPDATE `" . self::$db_prefix . "crontab` SET period='00 8 * * *' WHERE callback='Cron_Ad::expired_featured' LIMIT 1")->execute(); DB::query(Database::UPDATE, "UPDATE `" . self::$db_prefix . "crontab` SET period='00 9 * * *' WHERE callback='Cron_Ad::expired' LIMIT 1")->execute(); } catch (exception $e) { } //control login attempts try { DB::query(Database::UPDATE, "ALTER TABLE `" . self::$db_prefix . "users` ADD `last_failed` DATETIME NULL DEFAULT NULL ;")->execute(); } catch (exception $e) { } try { DB::query(Database::UPDATE, "ALTER TABLE `" . self::$db_prefix . "users` ADD `failed_attempts` int(10) unsigned DEFAULT 0")->execute(); } catch (exception $e) { } //categories/locations/users/ads has_image/last_modified try { DB::query(Database::UPDATE, "ALTER TABLE `" . self::$db_prefix . "categories` ADD `last_modified` DATETIME NULL DEFAULT NULL ;")->execute(); } catch (exception $e) { } try { DB::query(Database::UPDATE, "ALTER TABLE `" . self::$db_prefix . "categories` ADD `has_image` TINYINT( 1 ) NOT NULL DEFAULT '0' ;")->execute(); } catch (exception $e) { } try { DB::query(Database::UPDATE, "ALTER TABLE `" . self::$db_prefix . "locations` ADD `last_modified` DATETIME NULL DEFAULT NULL ;")->execute(); } catch (exception $e) { } try { DB::query(Database::UPDATE, "ALTER TABLE `" . self::$db_prefix . "locations` ADD `has_image` TINYINT( 1 ) NOT NULL DEFAULT '0' ;")->execute(); } catch (exception $e) { } try { DB::query(Database::UPDATE, "ALTER TABLE `" . self::$db_prefix . "users` ADD `has_image` TINYINT( 1 ) NOT NULL DEFAULT '0' ;")->execute(); } catch (exception $e) { } try { DB::query(Database::UPDATE, "ALTER TABLE `" . self::$db_prefix . "ads` ADD `last_modified` DATETIME NULL DEFAULT NULL ;")->execute(); } catch (exception $e) { } //new configs $configs = array(array('config_key' => 'aws_s3_active', 'group_name' => 'image', 'config_value' => 0), array('config_key' => 'aws_access_key', 'group_name' => 'image', 'config_value' => ''), array('config_key' => 'aws_secret_key', 'group_name' => 'image', 'config_value' => ''), array('config_key' => 'aws_s3_bucket', 'group_name' => 'image', 'config_value' => ''), array('config_key' => 'aws_s3_domain', 'group_name' => 'image', 'config_value' => 0), array('config_key' => 'disallow_nudes', 'group_name' => 'image', 'config_value' => 0), array('config_key' => 'html_head', 'group_name' => 'general', 'config_value' => ''), array('config_key' => 'html_footer', 'group_name' => 'general', 'config_value' => ''), array('config_key' => 'login_to_contact', 'group_name' => 'advertisement', 'config_value' => 0), array('config_key' => 'custom_css', 'group_name' => 'appearance', 'config_value' => 0), array('config_key' => 'custom_css_version', 'group_name' => 'appearance', 'config_value' => 0), array('config_key' => 'only_admin_post', 'group_name' => 'advertisement', 'config_value' => 0), array('config_key' => 'map_active', 'group_name' => 'appearance', 'config_value' => 1), array('config_key' => 'map_jscode', 'group_name' => 'appearance', 'config_value' => ''), array('config_key' => 'map_settings', 'group_name' => 'appearance', 'config_value' => ''), array('config_key' => 'recaptcha_active', 'group_name' => 'general', 'config_value' => ''), array('config_key' => 'recaptcha_secretkey', 'group_name' => 'general', 'config_value' => ''), array('config_key' => 'recaptcha_sitekey', 'group_name' => 'general', 'config_value' => '')); Model_Config::config_array($configs); //upgrade has_image field to use it as images count $ads = new Model_Ad(); $ads = $ads->where('has_images', '>', 0)->find_all(); if (count($ads)) { foreach ($ads as $ad) { $ad->has_images = 0; //begin with 0 images $route = $ad->image_path(); $folder = DOCROOT . $route; $image_keys = array(); if (is_dir($folder)) { //retrive ad pictures foreach (new DirectoryIterator($folder) as $file) { if (!$file->isDot()) { $key = explode('_', $file->getFilename()); $key = end($key); $key = explode('.', $key); $key = isset($key[0]) ? $key[0] : NULL; if (is_numeric($key)) { if (strpos($file->getFilename(), 'thumb_') === 0) { $image_keys[] = $key; } } } } //count images and reordering file names if (count($image_keys)) { asort($image_keys); foreach ($image_keys as $image_key) { $ad->has_images++; @rename($folder . $ad->seotitle . '_' . $image_key . '.jpg', $folder . $ad->seotitle . '_' . $ad->has_images . '.jpg'); @rename($folder . 'thumb_' . $ad->seotitle . '_' . $image_key . '.jpg', $folder . 'thumb_' . $ad->seotitle . '_' . $ad->has_images . '.jpg'); } } } //update has_images count try { $ad->save(); } catch (Exception $e) { throw HTTP_Exception::factory(500, $e->getMessage()); } } } //upgrade categories has_image $images_path = DOCROOT . 'images/categories'; if (is_dir($images_path)) { //retrive cat pictures foreach (new DirectoryIterator($images_path) as $file) { if ($file->isFile()) { $cat_name = str_replace('.png', '', $file->getFilename()); $cat = new Model_Category(); $cat->where('seoname', '=', $cat_name)->find(); if ($cat->loaded()) { $cat->has_image = 1; $cat->save(); } } } } //upgrade locations has_image $images_path = DOCROOT . 'images/locations'; if (is_dir($images_path)) { //retrive loc pictures foreach (new DirectoryIterator($images_path) as $file) { if ($file->isFile()) { $loc_name = str_replace('.png', '', $file->getFilename()); $loc = new Model_Location(); $loc->where('seoname', '=', $loc_name)->find(); if ($loc->loaded()) { $loc->has_image = 1; $loc->save(); } } } } //upgrade users has_image $images_path = DOCROOT . 'images/users'; if (is_dir($images_path)) { //retrive user pictures foreach (new DirectoryIterator($images_path) as $file) { if ($file->isFile() and is_numeric($id_user = str_replace('.png', '', $file->getFilename()))) { $user = new Model_User($id_user); if ($user->loaded()) { $user->has_image = 1; $user->save(); } } } } }
public function action_icon() { //get icon if (isset($_FILES['location_icon'])) { $icon = $_FILES['location_icon']; } else { $this->redirect(Route::get($this->_route_name)->uri(array('controller' => Request::current()->controller(), 'action' => 'index'))); } $location = new Model_Location($this->request->param('id')); if (core::config('image.aws_s3_active')) { require_once Kohana::find_file('vendor', 'amazon-s3-php-class/S3', 'php'); $s3 = new S3(core::config('image.aws_access_key'), core::config('image.aws_secret_key')); } if (core::post('icon_delete') and $location->delete_icon() == TRUE) { Alert::set(Alert::SUCCESS, __('Icon deleted.')); $this->redirect(Route::get($this->_route_name)->uri(array('controller' => Request::current()->controller(), 'action' => 'update', 'id' => $location->id_location))); } // end of icon delete if (!Upload::valid($icon) or !Upload::not_empty($icon) or !Upload::type($icon, explode(',', core::config('image.allowed_formats'))) or !Upload::size($icon, core::config('image.max_image_size') . 'M')) { if (Upload::not_empty($icon) && !Upload::type($icon, explode(',', core::config('image.allowed_formats')))) { Alert::set(Alert::ALERT, $icon['name'] . ' ' . sprintf(__('Is not valid format, please use one of this formats "%s"'), core::config('image.allowed_formats'))); $this->redirect(Route::get($this->_route_name)->uri(array('controller' => Request::current()->controller(), 'action' => 'update', 'id' => $location->id_location))); } if (!Upload::size($icon, core::config('image.max_image_size') . 'M')) { Alert::set(Alert::ALERT, $icon['name'] . ' ' . sprintf(__('Is not of valid size. Size is limited to %s MB per image'), core::config('image.max_image_size'))); $this->redirect(Route::get($this->_route_name)->uri(array('controller' => Request::current()->controller(), 'action' => 'update', 'id' => $location->id_location))); } Alert::set(Alert::ALERT, $icon['name'] . ' ' . __('Image is not valid. Please try again.')); $this->redirect(Route::get($this->_route_name)->uri(array('controller' => Request::current()->controller(), 'action' => 'update', 'id' => $location->id_location))); } else { if ($icon != NULL) { // saving/uploading img file to dir. $path = 'images/locations/'; $root = DOCROOT . $path; //root folder $icon_name = $location->seoname . '.png'; // if folder does not exist, try to make it if (!file_exists($root) and !@mkdir($root, 0775, true)) { // mkdir not successful ? Alert::set(Alert::ERROR, __('Image folder is missing and cannot be created with mkdir. Please correct to be able to upload images.')); return; // exit function } // save file to root folder, file, name, dir if ($file = Upload::save($icon, $icon_name, $root)) { // put icon to Amazon S3 if (core::config('image.aws_s3_active')) { $s3->putObject($s3->inputFile($file), core::config('image.aws_s3_bucket'), $path . $icon_name, S3::ACL_PUBLIC_READ); } // update location info $location->has_image = 1; $location->last_modified = Date::unix2mysql(); $location->save(); Alert::set(Alert::SUCCESS, $icon['name'] . ' ' . __('Icon is uploaded.')); } else { Alert::set(Alert::ERROR, $icon['name'] . ' ' . __('Icon file could not been saved.')); } $this->redirect(Route::get($this->_route_name)->uri(array('controller' => Request::current()->controller(), 'action' => 'update', 'id' => $location->id_location))); } } }
/** * does the DB migration * @param pointer $db * @param string $pf db_prefix */ private function migrate($db, $pf) { set_time_limit(0); $db_config = core::config('database.default'); $prefix = $db_config['table_prefix']; //connect DB original/to where we migrate $dbo = Database::instance('default'); //oc_accounts --> oc_users $users_map = array(); $accounts = $db->query(Database::SELECT, 'SELECT * FROM `' . $pf . 'accounts`'); foreach ($accounts as $account) { $user = new Model_User(); $user->where('email', '=', $account['email'])->limit(1)->find(); if (!$user->loaded()) { $user->name = $account['name']; $user->email = $account['email']; $user->password = $account['password']; $user->created = $account['createdDate']; $user->last_modified = $account['lastModifiedDate']; $user->last_login = $account['lastSigninDate']; $user->status = $account['active']; $user->id_role = 1; $user->seoname = $user->gen_seo_title($user->name); $user->save(); } $users_map[$account['email']] = $user->id_user; } //categories --> categories $categories_map = array(0 => 1); $categories = $db->query(Database::SELECT, 'SELECT * FROM `' . $pf . 'categories` ORDER BY `idCategoryParent` ASC'); foreach ($categories as $category) { $cat = new Model_Category(); $cat->name = $category['name']; $cat->order = $category['order']; $cat->created = $category['created']; $cat->seoname = $category['friendlyName']; $cat->price = $category['price']; $cat->description = substr($category['description'], 0, 250); $cat->parent_deep = $category['idCategoryParent'] > 0 ? 1 : 0; //there's only 1 deep $cat->id_category_parent = isset($categories_map[$category['idCategoryParent']]) ? $categories_map[$category['idCategoryParent']] : 1; $cat->save(); //we save old_id stores the new ID, so later we know the category parent, and to changes the ADS category id $categories_map[$category['idCategory']] = $cat->id_category; } //locations --> locations $locations_map = array(0 => 1); $locations = $db->query(Database::SELECT, 'SELECT * FROM `' . $pf . 'locations` ORDER BY `idLocationParent` ASC'); foreach ($locations as $location) { $loc = new Model_Location(); $loc->name = $location['name']; $loc->seoname = $location['friendlyName']; $loc->parent_deep = $location['idLocationParent'] > 0 ? 1 : 0; //there's only 1 deep $loc->id_location_parent = isset($locations_map[$location['idLocationParent']]) ? $locations_map[$location['idLocationParent']] : 1; $loc->save(); //we save old_id stores the new ID, so later we know the location parent, and to changes the ADS location id $locations_map[$location['idLocation']] = $loc->id_location; } //posts --> ads $ads_map = array(); $ads = $db->query(Database::SELECT, 'SELECT * FROM `' . $pf . 'posts`'); foreach ($ads as $a) { if (Valid::email($a['email'])) { //gettin the id_user if (isset($users_map[$a['email']])) { $id_user = $users_map[$a['email']]; } else { $user = Model_User::create_email($a['email'], $a['name']); $id_user = $user->id_user; } $ad = new Model_Ad(); $ad->id_ad = $a['idPost']; //so images still work $ad->id_user = $id_user; $ad->id_category = isset($categories_map[$a['idCategory']]) ? $categories_map[$a['idCategory']] : 1; $ad->id_location = isset($locations_map[$a['idLocation']]) ? $locations_map[$a['idLocation']] : 1; $ad->title = $a['title']; $ad->seotitle = $ad->gen_seo_title($a['title']); $ad->description = !empty($a['description']) ? Text::html2bb($a['description']) : $a['title']; $ad->address = $a['place']; $ad->price = $a['price']; $ad->phone = $a['phone']; $ad->has_images = $a['hasImages']; $ad->ip_address = ip2long($a['ip']); $ad->created = $a['insertDate']; $ad->published = $ad->created; //Status migration...big mess! if ($a['isAvailable'] == 0 and $a['isConfirmed'] == 0) { $ad->status = Model_Ad::STATUS_NOPUBLISHED; } elseif ($a['isAvailable'] == 1 and $a['isConfirmed'] == 0) { $ad->status = Model_Ad::STATUS_NOPUBLISHED; } elseif ($a['isAvailable'] == 1 and $a['isConfirmed'] == 1) { $ad->status = Model_Ad::STATUS_PUBLISHED; } elseif ($a['isAvailable'] == 0 and $a['isConfirmed'] == 1) { $ad->status = Model_Ad::STATUS_UNAVAILABLE; } elseif ($a['isAvailable'] == 2) { $ad->status = Model_Ad::STATUS_SPAM; } else { $ad->status = Model_Ad::STATUS_UNAVAILABLE; } try { $ad->save(); } catch (ORM_Validation_Exception $e) { // d($e->errors('')); } $ads_map[$a['idPost']] = $ad->id_ad; } } //posthits --> visits, mass migration $insert = 'INSERT INTO `' . $prefix . 'visits` ( `id_ad`, `created`, `ip_address`) VALUES'; $step = 5000; $total = $db->query(Database::SELECT, 'SELECT count(*) cont FROM `' . $pf . 'postshits`')->as_array(); $total = $total[0]['cont']; for ($i = 0; $i < $total; $i += $step) { $hits = $db->query(Database::SELECT, 'SELECT * FROM `' . $pf . 'postshits` LIMIT ' . $i . ', ' . $step); $values = ''; foreach ($hits as $hit) { //build insert query $values .= '(' . $hit['idPost'] . ', \'' . $hit['hitTime'] . '\', \'' . ip2long($hit['ip']) . '\'),'; } $dbo->query(Database::INSERT, $insert . substr($values, 0, -1)); } //old way of migrating // $hits = $db->query(Database::SELECT, 'SELECT * FROM `'.$pf.'postshits` '); // foreach ($hits as $hit) // { // //build insert query // $visit = new Model_Visit(); // $visit->id_ad = (isset($ads_map[$hit['idPost']]))?$ads_map[$hit['idPost']]:NULL; // $visit->created = $hit['hitTime']; // $visit->ip_address = ip2long($hit['ip']); // $visit->save(); // } }