/**
  * This returns all models sorted by Make Title, then Model Title
  *
  * @return \SquirrelsInventory\Model[]
  */
 public static function getAllModels()
 {
     $models = array();
     $makes = Make::getAllMakes();
     $parents = array(0 => array());
     foreach ($makes as $make_id => $make) {
         $parents[$make_id] = array();
     }
     $query = new \WP_Query(array('post_type' => self::CUSTOM_POST_TYPE, 'post_status' => 'publish', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC'));
     if ($query->have_posts()) {
         while ($query->have_posts()) {
             $query->the_post();
             $custom = get_post_custom(get_the_ID());
             $model = new Model(get_the_ID(), get_the_title());
             if (array_key_exists('make_id', $custom)) {
                 $make_id = $custom['make_id'][0];
                 $model->setMakeId($make_id);
                 if (array_key_exists($model->getMakeId(), $makes)) {
                     $model->setMake($makes[$model->getMakeId()]);
                 }
             } else {
                 $make_id = 0;
             }
             $parents[$make_id][] = $model;
         }
     }
     foreach ($parents as $parent_id => $_models) {
         /** @var Model[] $_models */
         foreach ($_models as $model) {
             $models[$model->getId()] = $model;
         }
     }
     return $models;
 }
 public function editInventory()
 {
     if (strlen($_REQUEST['new_make']) > 0 && strlen($_REQUEST['new_model']) > 0) {
         $make = new Make();
         $make->setTitle($_REQUEST['new_make'])->create();
         $model = new Model();
         $model->setTitle($_REQUEST['new_model'])->setMake($make)->create();
     } else {
         $model = new Model($_REQUEST['model_id']);
         $model->loadMake();
     }
     $auto = new Auto($_REQUEST['id']);
     $auto->setPrice($_REQUEST['price'])->setPricePostfix($_REQUEST['price_postfix'])->setTypeId($_REQUEST['type_id'])->setInventoryNumber($_REQUEST['inventory_number'])->setVin($_REQUEST['vin'])->setMakeId($model->getMakeId())->setModelId($model->getId())->setYear($_REQUEST['year'])->setDescription($_REQUEST['description'])->setIsVisible($_REQUEST['is_visible'])->setIsFeatured($_REQUEST['is_featured'])->setInterior($_REQUEST['interior'])->setExterior($_REQUEST['exterior'])->setOdometerReading($_REQUEST['odometer_reading']);
     /* Features */
     $auto->setFeatures(NULL);
     $features = json_decode(stripslashes($_REQUEST['features']), TRUE);
     foreach ($features as $f) {
         if ($f['remove'] == 0) {
             $feature = new AutoFeature();
             $feature->setId(uniqid())->setFeatureId($f['feature_id'])->setFeatureTitle($f['title'])->setValue($f['value'])->setCreatedAt(time())->setUpdatedAt(time());
             $auto->addFeature($feature);
         }
     }
     $auto->update();
     $images = json_decode(stripslashes($_REQUEST['images']), TRUE);
     /** Remove deleted images */
     if ($auto->getImageCount() > 0) {
         foreach ($auto->getImages() as $image) {
             $delete_me = TRUE;
             foreach ($images as $i) {
                 if ($image->getId() == $i['id']) {
                     $delete_me = FALSE;
                     break;
                 }
             }
             if ($delete_me) {
                 $auto->deleteImage($image->getId());
             }
         }
     }
     foreach ($images as $i) {
         /** Add new images */
         if ($i['id'] == 0) {
             $image = new Image();
             $image->setInventoryId($auto->getId())->setMediaId($i['media_id'])->setUrl($i['url'])->setIsDefault($i['def'])->create();
             $auto->addImage($image);
         } else {
             $image = new Image($i['id']);
             $image->setInventoryId($auto->getId())->setMediaId($i['media_id'])->setUrl($i['url'])->setIsDefault($i['def'])->update();
             $auto->setImage($i['id'], $image);
         }
     }
     return $auto->getId();
 }