Exemplo n.º 1
0
 public function save()
 {
     Cache::set("entity_" . $this->guid, $this);
     Dbase::createDefaultTables();
     if (!$this->owner_guid && loggedIn()) {
         if ($this->type != "systemvariable") {
             $this->owner_guid = getLoggedInUserGuid();
         }
     }
     $fields = array();
     $time = time();
     $ignore = array("type", "guid", "default_icon");
     if (!$this->access_id) {
         $default_access = Setting::get("default_access");
         if (!$default_access) {
             $default_access = "public";
         }
         $this->access_id = getInput("access_id") ? getInput("access_id") : $default_access;
     }
     $this->last_updated = $time;
     Dbase::addColumn('last_updated', strtolower($this->type));
     if (!$this->guid) {
         $this->time_created = $time;
         $query = "INSERT INTO `entities` (`type`) VALUES ('" . strtolower($this->type) . "')";
         $guid = Dbase::query($query);
         if ($guid == 0) {
             return false;
         }
         $this->guid = $guid;
     }
     Dbase::createTable(strtolower($this->type));
     $vars = get_object_vars($this);
     $query = "SELECT * FROM `" . strtolower($this->type) . "` WHERE `guid` = '{$this->guid}' LIMIT 1";
     $results = Dbase::getResults($query);
     if (!$results || $results->num_rows == 0) {
         $query = "INSERT INTO `" . strtolower($this->type) . "` (`guid`) VALUES ('{$this->guid}')";
         Dbase::query($query);
     }
     $query = "UPDATE `" . strtolower($this->type) . "` SET ";
     $columns = Dbase::getResultsArray("SHOW columns FROM `" . strtolower($this->type) . "`");
     foreach ($columns as $column) {
         $fields[] = $column['Field'];
     }
     foreach ($vars as $key => $value) {
         if (!in_array($key, $ignore)) {
             if (is_array($value) || is_object($value) || is_bool($value)) {
                 $value = Dbase::con()->real_escape_string(serialize($value));
             } else {
                 $value = Dbase::con()->real_escape_string($value);
             }
             if (!in_array($key, $fields)) {
                 Dbase::addColumn($key, strtolower($this->type));
             }
             $query .= "`{$key}`='{$value}',";
         }
     }
     $query = rtrim($query, ",");
     $query .= " WHERE `guid` = '{$this->guid}'";
     Dbase::query($query);
     return $this->guid;
 }