Page MenuHomeCode

Body.php
No OneTemporary

Body.php

<?php
/**
* Geo body class.
*
* Zed. The immensity of stars. The HyperShip. The people.
*
* (c) 2010, Dereckson, some rights reserved.
* Released under BSD license.
*
* A 3D grid of objects
*
* 0.1 2010-01-27 21:51 Autogenerated by Pluton Scaffolding
*
* @package Zed
* @subpackage Geo
* @author Sébastien Santoro aka Dereckson <dereckson@espace-win.org>
* @copyright 2010 Sébastien Santoro aka Dereckson
* @license http://www.opensource.org/licenses/bsd-license.php BSD
* @version 0.1
* @link http://scherzo.dereckson.be/doc/zed
* @link http://zed.dereckson.be/
* @filesource
*/
namespace Zed\Models\Geo;
use Keruald\Database\DatabaseEngine;
use Keruald\OmniTools\Collections\Vector;
use Zed\Models\Base\Entity;
/**
* Geo body class
*/
class Body extends Entity {
public string $code;
public string $name;
public bool $hypership = false;
public bool $asteroid = false;
public bool $moon = false;
public bool $planet = false;
public bool $star = false;
public bool $orbital = false;
public bool $hidden = false;
public $location;
public string $description;
public string $lastError = "";
/**
* Initializes a new instance
*/
function __construct (DatabaseEngine $db, string|int $code = null) {
$this->setDatabase($db);
if ($code) {
$this->code = (string)$code;
$this->load_from_database();
}
}
/**
* Loads the object body (ie fill the properties) from the $_POST array
*
* @param bool $readBoolean if false, don't read any form item matching a boolean field to avoid to set it to false if there are absent from a form.
*/
function load_from_form ($readBoolean = true) {
if (array_key_exists('name', $_POST)) {
$this->name = $_POST['name'];
}
if ($readBoolean) {
if (array_key_exists('hypership', $_POST)) {
$this->hypership = $_POST['hypership'];
}
if (array_key_exists('star', $_POST)) {
$this->star = $_POST['star'];
}
if (array_key_exists('asteroid', $_POST)) {
$this->asteroid = $_POST['asteroid'];
}
if (array_key_exists('moon', $_POST)) {
$this->moon = $_POST['moon'];
}
if (array_key_exists('planet', $_POST)) {
$this->planet = $_POST['planet'];
}
if (array_key_exists('orbital', $_POST)) {
$this->orbital = $_POST['orbital'];
}
if (array_key_exists('hidden', $_POST)) {
$this->hidden = $_POST['hidden'];
}
}
if (array_key_exists('location', $_POST)) {
$this->location = $_POST['location'];
}
if (array_key_exists('description', $_POST)) {
$this->description = $_POST['description'];
}
}
/**
* Loads the object body (ie fill the properties) from the database
*/
function load_from_database () : bool {
$db = $this->getDatabase();
$sql = "SELECT * FROM " . TABLE_BODIES . " WHERE body_code = '" . $this->code . "'";
if ( !($result = $db->query($sql)) ) {
message_die(SQL_ERROR, "Unable to query geo_bodies", '', __LINE__, __FILE__, $sql);
}
if (!$row = $db->fetchRow($result)) {
$this->lastError = "body unknown: " . $this->code;
return false;
}
$this->name = $row['body_name'];
$this->location = $row['body_location'];
$this->description = $row['body_description'] ?? "";
if ($row['body_status']) {
$flags = explode(',', $row['body_status']);
foreach ($flags as $flag) {
$this->$flag = true;
}
}
return true;
}
/**
* Gets the status field
*
* @return string the status field value (e.g. "asteroid,hidden")
*/
function getStatus () : string {
$flags = ['hypership','asteroid','moon','planet','star','orbital','hidden'];
return Vector::from($flags)
->filter(function ($flag) {
return $this->$flag;
})
->implode(",")
->getValue();
}
/**
* Gets the kind of place the body is (e.g. asteroid)
*
* @return string the kind of place
*/
function kind () : string {
//If a location can be described by 2 flags, order the relevant flags list
//by priority, as it'll return the first triggered.
//e.g. a moon converted in hypership will be "hypership" and not "moon".
$relevantFlags = ['hypership','asteroid','moon','planet','star','orbital'];
return Vector::from($relevantFlags)
->filter(function ($flag) {
return $this->$flag;
})
->firstOr("");
}
/**
* Gets the name of the body, as a string representation of the object
*
* @return string the name of the body
*/
function __toString () {
return $this->name;
}
public function getFullCode () : string {
return sprintf("B%05d", $this->code);
}
/**
* Saves to database
*/
function save_to_database () : void {
$db = $this->getDatabase();
$code = $this->code ? "'" . $db->escape($this->code) . "'" : 'NULL';
$name = $db->escape($this->name);
$status = $this->getStatus();
$location = $db->escape($this->location);
$description = $db->escape($this->description);
//Updates or inserts
$sql = "REPLACE INTO " . TABLE_BODIES . " (`body_code`, `body_name`, `body_status`, `body_location`, `body_description`) VALUES ($code, '$name', '$status', '$location', '$description')";
if (!$db->query($sql)) {
message_die(SQL_ERROR, "Unable to save", '', __LINE__, __FILE__, $sql);
}
if (!$code) {
//Gets new record code value
$this->code = $db->nextId();
}
}
}

File Metadata

Mime Type
text/x-php
Expires
Wed, Jul 16, 06:43 (1 w, 3 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
21391
Default Alt Text
Body.php (6 KB)

Event Timeline