public function loadFromRow(\stdClass $row) { $this->setId($row->id)->setTypeId($row->type_id)->setVin($row->vin)->setMakeId($row->make_id)->setModelId($row->model_id)->setInventoryNumber($row->inventory_number)->setYear($row->year)->setOdometerReading($row->odometer_reading)->setDescription($row->description)->setPrice($row->price)->setPricePostfix($row->price_postfix)->setIsVisible($row->is_visible)->setIsFeatured($row->is_featured)->setExterior($row->exterior)->setInterior($row->interior)->setCreatedAt($row->created_at)->setImportedAt($row->imported_at)->setUpdatedAt($row->updated_at); if (isset($row->make)) { $this->make = new Make(); $this->make->setId($row->make_id)->setTitle($row->make); } if (isset($row->model)) { $this->model = new Model(); $this->model->setId($row->model_id)->setTitle($row->model); } if (isset($row->type)) { $this->type = new AutoType(); $this->type->setId($row->type_id)->setTitle($row->type); } $features = json_decode($row->features, TRUE); if (!empty($features)) { foreach ($features as $f) { $feature = new AutoFeature(); $feature->setId($f['id'])->setFeatureId($f['feature_id'])->setFeatureTitle($f['feature_title'])->setValue($f['value'])->setCreatedAt($f['created_at'])->setUpdatedAt($f['updated_at']); $this->addFeature($feature); } } }
public function activate() { add_option('squirrels_inventory_version', self::VERSION); require_once ABSPATH . '/wp-admin/includes/upgrade.php'; global $wpdb; /** Create tables */ $charset_collate = ''; if (!empty($wpdb->charset)) { $charset_collate .= "DEFAULT CHARACTER SET {$wpdb->charset}"; } if (!empty($wpdb->collate)) { $charset_collate .= " COLLATE {$wpdb->collate}"; } /** SQUIRRELS_FEATURES table */ $table = $wpdb->prefix . "squirrels_features"; if ($wpdb->get_var("SHOW TABLES LIKE '" . $table . "'") != $table) { $sql = "\n\t\t\t\tCREATE TABLE `" . $table . "`\n\t\t\t\t(\n\t\t\t\t\t`id` INT(11) NOT NULL AUTO_INCREMENT,\n\t\t\t\t\t`title` VARCHAR(50) DEFAULT NULL,\n\t\t\t\t\t`is_system` TINYINT(4) DEFAULT NULL,\n\t\t\t\t\t`is_true_false` TINYINT(4) DEFAULT NULL,\n\t\t\t\t\t`options` TEXT DEFAULT NULL,\n\t\t\t\t\t`created_at` DATETIME DEFAULT NULL,\n\t\t\t\t\t`updated_at` DATETIME DEFAULT NULL,\n\t\t\t\t\tPRIMARY KEY (`id`)\n\t\t\t\t)"; $sql .= $charset_collate . ";"; // new line to avoid PHP Storm syntax error dbDelta($sql); } /** SQUIRRELS_INVENTORY table */ $table = $wpdb->prefix . Auto::TABLE_NAME; if ($wpdb->get_var("SHOW TABLES LIKE '" . $table . "'") != $table) { $sql = "\n\t\t\t\tCREATE TABLE `" . $table . "`\n\t\t\t\t(\n\t\t\t\t\t`id` INT(11) NOT NULL AUTO_INCREMENT,\n\t\t\t\t\t`inventory_number` VARCHAR(50) DEFAULT NULL,\n\t\t\t\t\t`vin` VARCHAR(50) DEFAULT NULL,\n\t\t\t\t\t`type_id` INT(11) DEFAULT NULL,\n\t\t\t\t\t`make_id` INT(11) DEFAULT NULL,\n\t\t\t\t\t`model_id` INT(11) DEFAULT NULL,\n\t\t\t\t\t`year` INT(2) DEFAULT NULL,\n\t\t\t\t\t`odometer_reading` INT(11) DEFAULT NULL,\n\t\t\t\t\t`features` TEXT,\n\t\t\t\t\t`is_visible` TINYINT(4) DEFAULT 0,\n\t\t\t\t\t`is_featured` TINYINT(4) DEFAULT 0,\n\t\t\t\t\t`description` TEXT,\n\t\t\t\t\t`price` DECIMAL(11,4) DEFAULT NULL,\n\t\t\t\t\t`exterior` VARCHAR(50) DEFAULT NULL,\n\t\t\t\t\t`interior` VARCHAR(50) DEFAULT NULL,\n\t\t\t\t\t`created_at` DATETIME DEFAULT NULL,\n\t\t\t\t\t`imported_at` DATETIME DEFAULT NULL,\n\t\t\t\t\t`updated_at` DATETIME DEFAULT NULL,\n\t\t\t\t\tPRIMARY KEY (`id`),\n\t\t\t\t\tKEY `type_id` (`type_id`),\n\t\t\t\t\tKEY `make_id` (`make_id`),\n\t\t\t\t\tKEY `model_id` (`model_id`)\n\t\t\t\t)"; $sql .= $charset_collate . ";"; // new line to avoid PHP Storm syntax error dbDelta($sql); } /** SQUIRRELS_IMAGES table */ $table = $wpdb->prefix . "squirrels_images"; if ($wpdb->get_var("SHOW TABLES LIKE '" . $table . "'") != $table) { $sql = "\n\t\t\t\tCREATE TABLE `" . $table . "`\n\t\t\t\t(\n\t\t\t\t\t`id` INT(11) NOT NULL AUTO_INCREMENT,\n\t\t\t\t\t`inventory_id` INT(11) DEFAULT NULL,\n\t\t\t\t\t`media_id` INT(11) DEFAULT NULL,\n\t\t\t\t\t`url` VARCHAR(250) DEFAULT NULL,\n\t\t\t\t\t`is_default` TINYINT(4) DEFAULT 0,\n\t\t\t\t\t`created_at` DATETIME DEFAULT NULL,\n\t\t\t\t\t`updated_at` DATETIME DEFAULT NULL,\n\t\t\t\t\tPRIMARY KEY (`id`),\n\t\t\t\t\tKEY `inventory_id` (`inventory_id`)\n\t\t\t\t)"; $sql .= $charset_collate . ";"; // new line to avoid PHP Storm syntax error dbDelta($sql); } $make_id = 0; $model_id = 0; $type_id = 0; /** Pre-load makes and models */ $makes = $this->getMakesModels(); foreach ($makes as $make_data) { $make = new Make(); $make->setTitle($make_data['title'])->create(); foreach ($make_data['models'] as $model_data) { $model = new Model(); $model->setTitle($model_data['title'])->setMakeId($make->getId())->create(); if ($make->getTitle() == 'Ford' && $model->getTitle() == 'Mustang') { $make_id = $make->getId(); $model_id = $model->getId(); } } } /** Pre-load Auto Types */ $auto_types = array('Car', 'Truck', 'SUV', 'Motorcycle', 'RV', 'Boat'); foreach ($auto_types as $title) { $auto_type = new AutoType(); $auto_type->setTitle($title)->create(); if ($title == 'Car') { $type_id = $auto_type->getId(); } } /** Add a couple sample Features */ if (Feature::getFeatureByTitle('Transmission') === FALSE) { $feature = new Feature(); $feature->setTitle('Transmission')->setIsTrueFalse(FALSE)->addOption(new FeatureOption('Automatic', 1, TRUE))->addOption(new FeatureOption('Manual', 2, FALSE))->create(); $feature = new Feature(); $feature->setTitle('AWD')->setIsTrueFalse(TRUE)->create(); } /** Add a sample Car */ if ($make_id > 0 && $model_id > 0 && $type_id > 0) { $auto = new Auto(); $auto->setTypeId($type_id)->setInventoryNumber('123456')->setVin('QWERTY')->setMakeId($make_id)->setModelId($model_id)->setYear('1965')->setOdometerReading(50000)->setPrice(100000)->setDescription('This is a sample car.')->setExterior('Red')->setInterior('Black')->setIsVisible(TRUE)->create(); } }