/** * @return int * @param Category $o_category * @desc Save the supplied Category to the database, and return the id */ function Save($o_category) { # check parameters if (!$o_category instanceof Category) { die('Unable to save category'); } # build query # if no id, it's a new category; otherwise update the category if ($o_category->GetId()) { $s_sql = 'UPDATE ' . $this->GetSettings()->GetTable('Category') . ' SET ' . 'parent = ' . Sql::ProtectNumeric($o_category->GetParentId(), true) . ', ' . "name = " . Sql::ProtectString($this->GetDataConnection(), $o_category->GetName()) . ", " . "code = " . Sql::ProtectString($this->GetDataConnection(), $o_category->GetUrl()) . ", " . 'sort_override = ' . Sql::ProtectNumeric($o_category->GetSortOverride()) . ', ' . 'date_changed = ' . gmdate('U') . ' ' . 'WHERE id = ' . Sql::ProtectNumeric($o_category->GetId()); # run query $this->GetDataConnection()->query($s_sql); } else { $s_sql = 'INSERT INTO ' . $this->GetSettings()->GetTable('Category') . ' SET ' . 'parent = ' . Sql::ProtectNumeric($o_category->GetParentId(), true) . ', ' . "name = " . Sql::ProtectString($this->GetDataConnection(), $o_category->GetName()) . ", " . "code = " . Sql::ProtectString($this->GetDataConnection(), $o_category->GetUrl()) . ", " . 'sort_override = ' . Sql::ProtectNumeric($o_category->GetSortOverride()) . ', ' . 'date_added = ' . gmdate('U') . ', ' . 'date_changed = ' . gmdate('U'); # run query $o_result = $this->GetDataConnection()->query($s_sql); # get autonumber $o_category->SetId($this->GetDataConnection()->insertID()); } return $o_category->GetId(); }
/** * @return Category * @param Category $o_child * @desc Find the parent Category of the supplied child Category */ function GetParent($o_child) { $o_found_category = null; # check input if ($o_child instanceof $this->s_item_class) { # find the relevant category in the category list if (is_array($this->a_items)) { foreach ($this->a_items as $o_category) { if ($o_category instanceof $this->s_item_class and $o_category->GetId() == $o_child->GetParentId()) { $o_found_category = $o_category; break; } } } } # return either the requested category or false return $o_found_category instanceof $this->s_item_class ? $o_found_category : false; }