/** * Edits an existing object type * * @version 1.0 * @since 1.0 * * @param array $data | * => VAL @param int $type_id | Unique id for this object type * => VAL @param string $type_slug | Slug name for this type. Must be unique to all other slugs. * => VAL @param string $name_admin | Admin name for this object type. Max 255 characters. * => VAL @param string $txt_admin | Admin description for this object type. * => VAL @param string $action_txt | Text shown to user in create object link. Max 255 characters. * => VAL @param string $name_user | Name shown to users for this object type. Max 255 characters. * => VAL @param string $txt_user | Description shown to users for this object type. * * @return bool | Exception on failure. True on success. False on no change. */ public function editType($data) { $db = new FOX_db(); if (!$data["type_id"]) { throw new FOX_exception(array('numeric' => 1, 'text' => "Missing type_id parameter in data array", 'data' => $data, 'file' => __FILE__, 'class' => __CLASS__, 'function' => __FUNCTION__, 'line' => __LINE__, 'child' => null)); } // Nulling this column prevents a user accidentally changing the module_id by passing in array // key value for it for even when they're not supposed to. unset($data["module_id"]); // We have to load the original row from the database before we modify it. This is so we know // the original $type_slug value so we can delete it from the cache if the user changes it, and // so we build an efficient query that only updates fields that have been changed. $ctrl = array("format" => "row_array"); try { $row_data = $db->runSelectQueryCol($this->_struct(), "type_id", "=", $data["type_id"], $columns = null, $ctrl); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 2, 'text' => "Error reading from database", 'file' => __FILE__, 'class' => __CLASS__, 'function' => __FUNCTION__, 'line' => __LINE__, 'child' => $child)); } // Compare the existing column values to the new column values in the $data array, and add // any columns with updated values to the $update_data array. $update_data = array(); foreach ($data as $col => $val) { if ($row_data[$col] != $val) { $update_data[$col] = $val; } } unset($col, $val); // If there are no values that need to be updated, return true to indicate no db rows were changed. // Otherwise, run the query. Rules set when the database table was created will prevent duplicate // $type_slugs, and other data integrity problems if (count($update_data) == 0) { return true; } else { // Lock the cache // ========================== try { $cache_image = self::lockCache(); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 3, 'text' => "Error locking cache", 'file' => __FILE__, 'class' => __CLASS__, 'function' => __FUNCTION__, 'line' => __LINE__, 'child' => $child)); } // Run the update // ========================== try { $rows_changed = $db->runUpdateQueryCol($this->_struct(), $update_data, "type_id", "=", $data["type_id"], $columns = null); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 4, 'text' => "Error writing to database", 'file' => __FILE__, 'class' => __CLASS__, 'function' => __FUNCTION__, 'line' => __LINE__, 'child' => $child)); } } // Rebuild the cache image // ========================== if ($rows_changed) { // If the type's slug value was changed, delete the old entry and create a // new one in the slug_to_id cache table if ($data["type_slug"] && $data["type_slug"] != $row_data["type_slug"]) { unset($cache_image["slug_to_type_id"][$row_data["module_id"]][$row_data["type_slug"]]); $cache_image["slug_to_type_id"][$row_data["module_id"]][$data["type_slug"]] = $data["type_id"]; } // Update any changed keys in the main cache table foreach ($update_data as $col => $val) { $cache_image["keys"][$data["type_id"]][$col] = $val; } unset($col, $val); } // Update the persistent cache, releasing our lock // =========================================================== try { self::writeCache($cache_image); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 5, 'text' => "Cache write error", 'file' => __FILE__, 'class' => __CLASS__, 'function' => __FUNCTION__, 'line' => __LINE__, 'child' => $child)); } // Overwrite the class cache $this->cache = $cache_image; return (bool) $rows_changed; }
/** * Edits an existing level * * @version 1.0 * @since 1.0 * * @param array $data | * => VAL @param int $level_id | Unique id for this access level. * => VAL @param string $level_slug | Slug name for this level. Must be unique to all other slugs. * => VAL @param string $level_name | Name of the level. Max 64 characters. * => VAL @param string $level_desc | Description of the level. Max 255 characters. * => VAL @param int $rank | Rank of this level within the object type. 1-255 where 1 is the highest. * => VAL @param int $key_id | Key id required to access to this level * * @return int | Exception on failure. False on no change. True on success. */ public function editLevel($data) { $db = new FOX_db(); if (!$data["level_id"]) { throw new FOX_exception(array('numeric' => 1, 'text' => "Missing level_id in data array", 'data' => $data, 'file' => __FILE__, 'class' => __CLASS__, 'function' => __FUNCTION__, 'line' => __LINE__, 'child' => null)); } // Nulling these columns prevents a user accidentally changing the module_id and // type_id values by passing in array keys for them when they're not supposed to. unset($data["module_id"]); unset($data["type_id"]); // We have to load the original row from the database before we modify it. This is so we know // the original $level_slug value so we can delete it from the cache if the user changes it, and // so we can build an efficient query that only updates fields that have been changed. $ctrl = array("format" => "row_array"); try { $row_data = $db->runSelectQueryCol($this->_struct(), "level_id", "=", $data["level_id"], $columns = null, $ctrl); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 2, 'text' => "Error reading from database", 'data' => array('level_id' => $data["level_id"], 'ctrl' => $ctrl), 'file' => __FILE__, 'class' => __CLASS__, 'function' => __FUNCTION__, 'line' => __LINE__, 'child' => $child)); } // Compare the existing column values to the new column values in the $data array, and add // any columns with updated values to the $update_data array. $update_data = array(); foreach ($data as $col => $val) { if ($row_data[$col] != $val) { $update_data[$col] = $val; } } unset($col, $val); // If rank is being updated, check that the new rank doesn't already exist // ========================================================================= if ($update_data["rank"]) { $args = array(array("col" => "module_id", "op" => "=", "val" => $row_data["module_id"]), array("col" => "type_id", "op" => "=", "val" => $row_data["type_id"]), array("col" => "rank", "op" => "=", "val" => $data["rank"])); $ctrl = array("format" => "var", "count" => true); try { $already_exists = $db->runSelectQuery($this->_struct(), $args, $columns = null, $ctrl); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 3, 'text' => "Error reading from database", 'data' => array('args' => $args, 'ctrl' => $ctrl), 'file' => __FILE__, 'class' => __CLASS__, 'function' => __FUNCTION__, 'line' => __LINE__, 'child' => $child)); } if ($already_exists) { throw new FOX_exception(array('numeric' => 4, 'text' => "Rank you are trying to change target item to already exists", 'data' => array('data' => $data, 'faulting_rank' => $update_data["rank"]), 'file' => __FILE__, 'class' => __CLASS__, 'function' => __FUNCTION__, 'line' => __LINE__, 'child' => null)); } } // If there are no values that need to be updated, return false to indicate no db rows were changed. // Otherwise, run the query. Rules set when the database table was created will prevent duplicate // $level_slugs and duplicate $rank values within a $level_id, and other data integrity problems. if (count($update_data) == 0) { return false; } // Lock the cache // ========================== try { $cache_image = self::lockCache(); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 5, 'text' => "Error locking cache", 'file' => __FILE__, 'class' => __CLASS__, 'function' => __FUNCTION__, 'line' => __LINE__, 'child' => $child)); } // Run the update // ========================== try { $rows_changed = $db->runUpdateQueryCol($this->_struct(), $update_data, "level_id", "=", $data["level_id"], $columns = null); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 6, 'text' => "Error writing to database", 'file' => __FILE__, 'class' => __CLASS__, 'function' => __FUNCTION__, 'line' => __LINE__, 'child' => $child)); } // Rebuild the cache image // ========================== // If the level's $slug value was changed, delete the old entry and create a // new one in the slug_to_id cache table if ($update_data["level_slug"] && $update_data["level_slug"] != $row_data["level_slug"]) { unset($cache_image["slug_to_level_id"][$row_data["module_id"]][$row_data["type_id"]][$row_data["level_slug"]]); $cache_image["slug_to_level_id"][$row_data["module_id"]][$row_data["type_id"]][$data["level_slug"]] = $row_data["level_id"]; } // If the level's $rank value was changed, delete the old entry and create a // new entry in the type_levels cache table if ($update_data["rank"] && $update_data["rank"] != $row_data["rank"]) { unset($cache_image["type_levels"][$row_data["type_id"]][$row_data["rank"]]); $cache_image["type_levels"][$row_data["type_id"]][$data["rank"]] = true; } // Update any changed keys in the main cache table foreach ($update_data as $col => $val) { $cache_image["keys"][$data["level_id"]][$col] = $val; } unset($col, $val); // Update the persistent cache, releasing our lock // =========================================================== try { self::writeCache($cache_image); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 7, 'text' => "Cache write error", 'file' => __FILE__, 'class' => __CLASS__, 'function' => __FUNCTION__, 'line' => __LINE__, 'child' => $child)); } // Overwrite the class cache $this->cache = $cache_image; return (bool) $rows_changed; }
/** * Edits fields for an existing group, given the group's id * * @version 1.0 * @since 1.0 * * @param int $group_id | id of the group * @param string $name | name of group (max 32 chars) * @param string $title | title of group (max 128 chars) * @param string $caption | caption of group * @param bool $is_default | if set true, this group is the default group * @param int $icon | media_id of image to use as icon for the group * @return bool | False on failure. True on success. */ public function editGroup($group_id, $name, $title, $caption, $is_default = false, $icon = null) { // IMPORTANT: Default group flag rules // ========================================== // 1) Only one group can be the default group // 2) There must always be a default group global $fox; $data = array("name" => $name, "title" => $title, "caption" => $caption); if ($is_default == true) { $data["is_default"] = true; } if ($icon) { $data["icon"] = $icon; } // Get the column values for the current group // ========================================================== $db = new FOX_db(); $columns = array("mode" => "exclude", "col" => array("group_id")); $ctrl = array("format" => "col"); try { $old = $db->runSelectQueryCol(self::$struct, "group_id", "=", $group_id, $columns, $ctrl); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 4, 'text' => "DB select exception", 'data' => array("col" => "group_id", "op" => "=", "val" => $group_id, "columns" => $columns, "ctrl" => $ctrl), 'file' => __FILE__, 'line' => __LINE__, 'method' => __METHOD__, 'child' => $child)); } // CASE 1: User has set $is_default FALSE and this is currrently the DEFAULT group // ============================================================================= if ($data["is_default"] != true && $old["is_default"] == true) { // There always has to be a default group return false; } // CASE 2: User is not setting this group to be the default group // ============================================================================= if ($data["is_default"] != true) { // Update the group's data try { $group_update_ok = $db->runUpdateQueryCol(self::$struct, $data, "group_id", "=", $group_id); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 5, 'text' => "DB update exception", 'data' => array("data" => $data, "col" => "group_id", "op" => "=", "val" => $group_id), 'file' => __FILE__, 'line' => __LINE__, 'method' => __METHOD__, 'child' => $child)); } if ($group_update_ok) { $result = true; } else { $result = false; } } else { // Find $group_id of current default group $args = array(array("col" => "is_default", "op" => "=", "val" => true)); $columns = array("mode" => "include", "col" => "group_id"); $ctrl = array("format" => "var"); try { $old_default = $db->runSelectQuery(self::$struct, $args, $columns, $ctrl); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 6, 'text' => "DB select exception", 'data' => array("args" => $args, "columns" => $columns, "ctrl" => $ctrl), 'file' => __FILE__, 'line' => __LINE__, 'method' => __METHOD__, 'child' => $child)); } // @@@@@@ BEGIN TRANSACTION @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ try { $started_transaction = $db->beginTransaction(); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 7, 'text' => "beginTransaction exception", 'file' => __FILE__, 'line' => __LINE__, 'method' => __METHOD__, 'child' => $child)); } if ($started_transaction) { // Update the group's fields, setting its default flag try { $group_update_ok = $db->runUpdateQueryCol(self::$struct, $data, "group_id", "=", $group_id); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 8, 'text' => "DB update exception", 'data' => array("data" => $data, "col" => "group_id", "op" => "=", "val" => $group_id), 'file' => __FILE__, 'line' => __LINE__, 'method' => __METHOD__, 'child' => $child)); } // Remove remove default flag from old group $unset_default = array("is_default" => false); try { $default_update_ok = $db->runUpdateQueryCol(self::$struct, $unset_default, "group_id", "=", $old_default); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 9, 'text' => "DB update exception", 'data' => array("data" => $unset_default, "col" => "group_id", "op" => "=", "val" => $old_default), 'file' => __FILE__, 'line' => __LINE__, 'method' => __METHOD__, 'child' => $child)); } // If all queries were successful, commit the transaction if ($group_update_ok && $default_update_ok) { try { $result = $db->commitTransaction(); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 10, 'text' => "commitTransaction exception", 'file' => __FILE__, 'line' => __LINE__, 'method' => __METHOD__, 'child' => $child)); } } else { try { $db->rollbackTransaction(); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 11, 'text' => "rollbackTransaction exception", 'file' => __FILE__, 'line' => __LINE__, 'method' => __METHOD__, 'child' => $child)); } $result = false; } } else { $result = false; } // @@@@@@ END TRANSACTION @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ } if ($result) { // If any values stored in the cache have been changed, update the cache if ($old["is_default"] != $data["is_default"] || $old["name"] != $data["name"]) { try { self::loadCache(); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 12, 'text' => "loadCache exception", 'file' => __FILE__, 'line' => __LINE__, 'method' => __METHOD__, 'child' => $child)); } if ($old["name"] != $data["name"]) { unset($this->cache["ids"][$old["name"]]); $this->cache["ids"][$name] = $group_id; } if ($old["is_default"] != $data["is_default"]) { $this->cache["default_id"] = $group_id; } try { $cache_ok = self::saveCache(); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 13, 'text' => "saveCache exception", 'file' => __FILE__, 'line' => __LINE__, 'method' => __METHOD__, 'child' => $child)); } return $cache_ok; } else { return true; } } else { return false; } }
/** * Deletes an existing user class. Users in this class will be moved to the class's "transfer_to" * class. If the "transfer_to" class was not set, users will be moved to the site's "default" class. * * @version 1.0 * @since 1.0 * * @param int $id | id of this class to delete * @return bool/int | False on failure. Numer of affected users on success. */ public function deleteClass($id) { // Fetch the db entry for this user class // ============================================================================================ $db = new FOX_db(); $columns = array("format" => "include", "col" => array("id", "is_default", "transfer_to")); $ctrl = array("format" => "row_array"); try { $result = $db->runSelectQueryCol(self::$struct, "id", "=", $id, $columns, $ctrl); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 1, 'text' => "DB select exception", 'data' => array("col" => "id", "op" => "=", "val" => $id, "columns" => $columns, "ctrl" => $ctrl), 'file' => __FILE__, 'line' => __LINE__, 'method' => __METHOD__, 'child' => $child)); } // Trap user trying to delete the default class // ============================================================================================ if ($result["is_default"] == true) { echo "\nERROR: You cannot delete a class that is set as the default user class. To delete this "; echo "class, set another class as the default class, then come back and delete this one.\n"; die; } // If the old class has a "transfer_to" class, use it. If the old class doesn't list a // "transfer_to" class, find the class that has the "default" flag, and use that class instead. // ============================================================================================ if (!empty($result["transfer_to"])) { $dest_class = $result["transfer_to"]; } else { $columns = array("format" => "include", "col" => "id"); $ctrl = array("format" => "var"); try { $dest_class = $db->runSelectQueryCol(self::$struct, "is_default", "=", true, $columns, $ctrl); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 2, 'text' => "DB select exception", 'data' => array("col" => "is_default", "op" => "=", "val" => true, "columns" => $columns, "ctrl" => $ctrl), 'file' => __FILE__, 'line' => __LINE__, 'method' => __METHOD__, 'child' => $child)); } } // Run the update query // ============================================================================================ $update = array("user_class_id" => $dest_class, "when_class_set" => gmdate("Y-m-d H:i:s"), "when_class_expires" => null); try { $result = $db->runUpdateQueryCol(FOX_user::_struct(), $update, "user_class_id", "=", $id); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 3, 'text' => "DB update exception", 'data' => array("data" => $update, "col" => "usewr_class_id", "op" => "=", "val" => $id), 'file' => __FILE__, 'line' => __LINE__, 'method' => __METHOD__, 'child' => $child)); } // TODO: Although this code will move the users to the new class and delete the old class, it will cause // huge problems if the classes have different limitations. For example, if the old class allowed video // uploads in albums, and the new class does not. Or if the old class allowed 10 instances of an album // type and the new one does not, etc. We need to come up with ways to resolve these problems. }