/**
  * Load the locomotive object
  * @since Version 3.2
  * @version 3.2
  * @return boolean
  */
 public function fetch()
 {
     if (!$this->id) {
         throw new Exception("Cannot load loco object - loco ID not provided");
         return false;
     }
     $this->mckey = "railpage:locos.loco_id=" . $this->id;
     #deleteMemcacheObject($this->mckey);
     if ($row = $this->getCache($this->mckey)) {
         // Do nothing
     } elseif ($this->db instanceof \sql_db) {
         $query = "SELECT l.*, s.name AS loco_status, ow.operator_name AS owner_name, op.operator_name AS operator_name\r\n\t\t\t\t\t\t\tFROM loco_unit AS l\r\n\t\t\t\t\t\t\tLEFT JOIN loco_status AS s ON l.loco_status_id = s.id\r\n\t\t\t\t\t\t\tLEFT JOIN operators AS ow ON ow.operator_id = l.owner_id\r\n\t\t\t\t\t\t\tLEFT JOIN operators AS op ON op.operator_id = l.operator_id\r\n\t\t\t\t\t\t\tWHERE l.loco_id = " . $this->id;
         if ($rs = $this->db->query($query)) {
             $row = $rs->fetch_assoc();
             $this->setCache($this->mckey, $row, strtotime("+24 hours"));
         }
     } else {
         if (RP_DEBUG) {
             global $site_debug;
             $debug_timer_start = microtime(true);
         }
         $query = "SELECT l.*, s.name AS loco_status, ow.operator_name AS owner_name, op.operator_name AS operator_name\r\n\t\t\t\t\t\t\tFROM loco_unit AS l\r\n\t\t\t\t\t\t\tLEFT JOIN loco_status AS s ON l.loco_status_id = s.id\r\n\t\t\t\t\t\t\tLEFT JOIN operators AS ow ON ow.operator_id = l.owner_id\r\n\t\t\t\t\t\t\tLEFT JOIN operators AS op ON op.operator_id = l.operator_id\r\n\t\t\t\t\t\t\tWHERE l.loco_id = ?";
         $row = $this->db->fetchRow($query, $this->id);
         if (RP_DEBUG) {
             if ($row === false) {
                 $site_debug[] = "Zend_DB: FAILED select loco ID " . $this->id . " in " . round(microtime(true) - $debug_timer_start, 5) . "s";
             } else {
                 $site_debug[] = "Zend_DB: SUCCESS select loco ID " . $this->id . " in " . round(microtime(true) - $debug_timer_start, 5) . "s";
             }
         }
         $this->setCache($this->mckey, $row, strtotime("+24 hours"));
     }
     if (isset($row) && is_array($row)) {
         $this->number = stripslashes($row['loco_num']);
         $this->name = stripslashes($row['loco_name']);
         $this->gauge = stripslashes($row['loco_gauge']);
         $this->gauge_id = $row['loco_gauge_id'];
         $this->status_id = $row['loco_status_id'];
         $this->status = $row['loco_status'];
         $this->class_id = $row['class_id'];
         $this->owner_id = $row['owner_id'];
         $this->owner = $row['owner_name'];
         $this->operator_id = $row['operator_id'];
         $this->operator = $row['operator_name'];
         $this->entered_service = $row['entered_service'];
         $this->withdrawal_date = $row['withdrawn'];
         $this->date_added = $row['date_added'];
         $this->date_modified = $row['date_modified'];
         $this->builders_num = $row['builders_number'];
         $this->photo_id = intval($row['photo_id']);
         $this->manufacturer_id = $row['manufacturer_id'];
         $this->Class = new LocoClass($this->class_id);
         $this->class =& $this->Class;
         $this->flickr_tag = trim(str_replace(" ", "", $this->Class->flickr_tag . "-" . $this->number));
         $this->gauge_formatted = format_gauge($this->gauge);
         $this->url = new Url(strtolower($this->makeLocoURL($this->Class->slug, $this->number)));
         $this->url->edit = sprintf("%s?mode=loco.edit&id=%d", $this->Module->url, $this->id);
         $this->url->sightings = sprintf("%s/sightings", $this->url->url);
         /**
          * Set the meta data
          */
         if (isset($row['meta'])) {
             $this->meta = json_decode($row['meta'], true);
         } else {
             $this->meta = array();
         }
         // Fetch the gauge data
         if ($this->gauge = getMemcacheObject(sprintf("railpage:locos.gauge_id=%d", $row['loco_gauge_id']))) {
             // Do nothing
         } elseif ($this->db instanceof \sql_db) {
             $query = "SELECT * FROM loco_gauge WHERE gauge_id = '" . $this->db->real_escape_string($row['loco_gauge_id']) . "'";
             if ($rs = $this->db->query($query)) {
                 $this->gauge = $rs->fetch_assoc();
                 $this->setCache("rp-locos-gauge-" . $row['loco_gauge_id'], $this->gauge);
             }
         } else {
             $query = "SELECT * FROM loco_gauge WHERE gauge_id = ?";
             $this->gauge = $this->db->fetchRow($query, $row['loco_gauge_id']);
             $this->setCache("railpage:locos.gauge_id=" . $row['loco_gauge_id'], $this->gauge, strtotime("+2 months"));
         }
         /**
          * If an asset ID exists and is greater than 0, create the asset object
          */
         if (isset($row['asset_id']) && $row['asset_id'] > 0) {
             try {
                 $this->Asset = new \Railpage\Assets\Asset($row['asset_id']);
             } catch (Exception $e) {
                 global $Error;
                 $Error->save($e);
             }
         }
         /**
          * Try to load the Image object
          */
         if (filter_var($row['photo_id'], FILTER_VALIDATE_INT)) {
             $Images = new \Railpage\Images\Images();
             $this->Image = $Images->findImage("flickr", $row['photo_id']);
             $this->Image->addLink($this->namespace, $this->id);
         }
         /**
          * Get all owners of this locomotive
          */
         try {
             $this->owners = $this->getOrganisations(1);
             if (!empty($this->owner_id) && empty($this->owners)) {
                 $this->addOrganisation($this->owner_id, 1);
                 // Re-fetch the owners
                 $this->owners = $this->getOrganisations(1);
             }
             reset($this->owners);
             if (isset($this->owners[0]['organisation_id']) && isset($this->owners[0]['organisation_name'])) {
                 $this->owner_id = $this->owners[0]['organisation_id'];
                 $this->owner = $this->owners[0]['organisation_name'];
             } else {
                 $this->owner_id = 0;
                 $this->owner = "Unknown";
             }
         } catch (Exception $e) {
             global $Error;
             $Error->save($e);
         }
         /**
          * Get all operators of this locomotive
          */
         try {
             $this->operators = $this->getOrganisations(2);
             if (!empty($this->operator_id) && empty($this->operators)) {
                 $this->addOrganisation($this->operator_id, 2);
                 // Re-fetch the operators
                 $this->operators = $this->getOrganisations(2);
             }
             reset($this->operators);
             if (isset($this->operators[0]['organisation_id']) && isset($this->operators[0]['organisation_name'])) {
                 $this->operator_id = $this->operators[0]['organisation_id'];
                 $this->operator = $this->operators[0]['organisation_name'];
             } else {
                 $this->operator_id = 0;
                 $this->operator = "Unknown";
             }
         } catch (Exception $e) {
             global $Error;
             $Error->save($e);
         }
         /**
          * Get the average rating of the data of this loco
          */
         try {
             $this->rating = $this->getRating();
         } catch (Exception $e) {
             // Do nothing for now
         }
         /**
          * Get the manufacturer
          */
         if (empty($this->manufacturer_id)) {
             $this->manufacturer_id = $this->Class->manufacturer_id;
             $this->manufacturer = $this->Class->manufacturer;
         } else {
             try {
                 $builders = $this->listManufacturers();
                 if (count($builders['manufacturers'])) {
                     $this->manufacturer = $builders['manufacturers'][$this->manufacturer_id]['manufacturer_name'];
                 }
             } catch (Exception $e) {
                 // I hate globals, but I don't want to throw an exception here...
                 global $Error;
                 $Error->save($e);
             }
         }
         /**
          * Update the latest owner/operator stored in this row
          */
         $owners = $this->getOrganisations(1, 1);
         $operators = $this->getOrganisations(2, 1);
         if (count($owners) && intval(trim($this->owner_id)) != intval(trim($owners[0]['operator_id']))) {
             if (RP_DEBUG) {
                 global $site_debug;
                 $site_debug[] = __CLASS__ . "::" . __FUNCTION__ . "() : committing changes to owner for loco ID " . $this->id;
                 $site_debug[] = __CLASS__ . "::" . __FUNCTION__ . "() : Current owner_id: " . $this->owner_id . ", Proposed owner_id: " . $owners[0]['operator_id'];
             }
             $this->owner = $owners[0]['organisation_name'];
             $this->owner_id = $owners[0]['operator_id'];
             $this->commit();
         }
         if (count($operators) && intval(trim($this->operator_id)) != intval(trim($operators[0]['operator_id']))) {
             if (RP_DEBUG) {
                 global $site_debug;
                 $site_debug[] = __CLASS__ . "::" . __FUNCTION__ . "() : committing changes to operator for loco ID " . $this->id;
                 $site_debug[] = __CLASS__ . "::" . __FUNCTION__ . "() : Current operator_id: " . $this->operator_id . ", Proposed operator_id: " . $owners[0]['operator_id'];
             }
             $this->operator = $operators[0]['organisation_name'];
             $this->operator_id = $operators[0]['operator_id'];
             $this->commit();
         }
         /**
          * Build the shortcut URL for this object
          */
         try {
             $this->fwlink = new \Railpage\fwlink($this->url);
             if (empty($this->fwlink->url)) {
                 $this->fwlink->url = $this->url;
                 $this->fwlink->title = $this->number . " - " . $this->Class->name;
                 $this->fwlink->commit();
             }
         } catch (Exception $e) {
             global $Error;
             $Error->save($e);
         }
         /**
          * Populate the list of liveries
          */
         foreach ($this->db->fetchAll("SELECT lu.livery_id FROM loco_unit_livery AS lu LEFT JOIN loco_livery AS li ON lu.livery_id = li.livery_id WHERE lu.loco_id = ? ORDER BY li.livery", $this->id) as $row) {
             $Livery = new Livery($row['livery_id']);
             $livery = array("id" => $Livery->id, "name" => $Livery->name, "tag" => $Livery->tag, "country" => array("code" => $Livery->country), "region" => array("code" => $Livery->region));
             if ($Livery->Image instanceof \Railpage\Images\Image) {
                 $livery['image'] = array("id" => $Livery->Image->id, "title" => $Livery->Image->title, "description" => $Livery->Image->description, "provider" => $Livery->Image->provider, "photo_id" => $Livery->Image->photo_id, "sizes" => $Livery->Image->sizes);
             }
             $this->liveries[] = $livery;
         }
         /**
          * Set the StatsD namespaces
          */
         $this->StatsD->target->view = sprintf("%s.%d.view", $this->namespace, $this->id);
         $this->StatsD->target->edit = sprintf("%s.%d.view", $this->namespace, $this->id);
     } else {
         throw new Exception("No data found for Loco ID " . $this->id);
         return false;
     }
 }
Beispiel #2
0
 /**
  * Populate this object with data returned from Memcached/Redis/DB
  * @since Version 3.9.1
  * @return void
  */
 private function populate()
 {
     $timer = Debug::getTimer();
     $row = Utility\LocomotiveUtility::fetchLocomotive($this);
     if (!is_array($row) || count($row) === 0) {
         throw new Exception("Data for this locomotive could not be retrieved");
     }
     $lookup = Utility\DataUtility::getLocoColumnMapping();
     foreach ($row as $key => $val) {
         if (isset($lookup[$key])) {
             $var = $lookup[$key];
             $this->{$var} = $val;
         }
     }
     $ints = ["gauge_id", "status_id", "class_id", "owner_id", "operator_id", "photo_id", "manufacturer_id"];
     foreach ($ints as $int) {
         $this->{$int} = filter_var($this->{$int}, FILTER_VALIDATE_INT);
     }
     $this->Class = Factory::CreateLocoClass($this->class_id);
     $this->class =& $this->Class;
     $this->flickr_tag = trim(str_replace(" ", "", $this->Class->flickr_tag . "-" . $this->number));
     $this->gauge_formatted = format_gauge($this->gauge);
     $this->makeLinks();
     Debug::logEvent(__METHOD__, $timer);
     return $row;
 }