private function testCategories()
 {
     $UshApiLib_Site_Info = new UshApiLib_Site_Info(url::base() . "api");
     echo "<h1>Categories</h1><strong>URL:</strong> " . $UshApiLib_Site_Info->getUrl() . "<br/><br/>";
     $params = new UshApiLib_Categories_Task_Parameter();
     $task = new UshApiLib_Categories_Task($params, $UshApiLib_Site_Info);
     $response = $task->execute();
     echo "<strong>Query String:</strong> " . Kohana::debug($params->get_query_string()) . "<br/><br/>";
     echo "<strong>JSON:</strong> " . $task->getJson() . "<br/><br/>";
     echo "<strong>Code:</strong> " . $response->getError_code() . " <strong>Message:</strong> " . $response->getError_message() . "<br/><br/>";
     foreach ($response->getCategories() as $cat) {
         echo "Category Name: " . $cat['category']->category_title . "<br/>";
         foreach ($cat['translations'] as $translation) {
             echo "Translation: Lang: " . $translation->locale . " Name: " . $translation->category_title . "<br/>";
         }
     }
 }
    /**
     * Use remote Ushahidi deployments API to get Category Data
     */
    private function _process_site_categories($site)
    {
        $modified_ids = array();
        // this is an array of our primary keys
        $UshApiLib_Site_Info = new UshApiLib_Site_Info(sharing_helper::clean_url($site->site_url) . "/api", $site->site_username, $site->site_password);
        $params = new UshApiLib_Categories_Task_Parameter();
        if (isset($_GET['debug']) and $_GET['debug'] == 1) {
            echo "<strong>Query String:</strong> " . Kohana::debug($params->get_query_string()) . "<br/><br/>";
        }
        $task = new UshApiLib_Categories_Task($params, $UshApiLib_Site_Info);
        $response = $task->execute();
        if ($response->getError_code()) {
            if (isset($_GET['debug']) and $_GET['debug'] == 1) {
                echo "Error Code: " . $response->getError_code() . " Message: " . $response->getError_message() . "<BR /><BR />";
            }
            return;
        }
        // Grab existing items
        $existing_items = ORM::factory('sharing_category')->with('category')->where('sharing_site_id', $site->id)->find_all();
        // Build array of existing items, key'd by remote id
        $array = array();
        foreach ($existing_items as $item) {
            $array[$item->remote_category_id] = $item;
        }
        $existing_items = $array;
        // First pass - parent categories
        foreach ($response->getCategories() as $remote_category_id => $category_info) {
            $remote_category = $category_info['category'];
            // skip child categories
            if ($remote_category->parent_id != 0) {
                continue;
            }
            $sharing_category = $this->_save_category($remote_category_id, $category_info, $site->id, $existing_items);
            if (!$sharing_category) {
                continue;
            }
            $existing_items[$remote_category_id] = $sharing_category;
            // Save the primary key of the row we touched. We will be deleting ones that weren't touched.
            $modified_ids[] = $sharing_category->id;
        }
        // 2nd pass: child categories
        foreach ($response->getCategories() as $remote_category_id => $category_info) {
            $remote_category = $category_info['category'];
            // skip top level categories
            if ($remote_category->parent_id == 0) {
                continue;
            }
            // Find the sharing_category that matches the parent
            if (!isset($existing_items[$remote_category->parent_id])) {
                continue;
            }
            // Skip if parent missing
            $parent = $existing_items[$remote_category->parent_id];
            $sharing_category = $this->_save_category($remote_category_id, $category_info, $site->id, $existing_items, $parent);
            if (!$sharing_category) {
                continue;
            }
            $existing_items[$remote_category_id] = $sharing_category;
            // Save the primary key of the row we touched. We will be deleting ones that weren't touched.
            $modified_ids[] = $sharing_category->id;
        }
        // Delete the reports that are no longer being displayed on the shared site
        if (count($modified_ids) > 0) {
            $modified_ids = array_map('intval', $modified_ids);
            // Hide categories when deleted/hidden on main site
            // We can't tell the difference between deleted/hidden from the API
            $result = Database::instance()->query('UPDATE category SET category_visible = 0
				WHERE id IN (SELECT category_id FROM sharing_category WHERE id NOT IN (' . implode(',', $modified_ids) . ') AND sharing_site_id = ?)', $site->id);
            // @todo save hidden state so we only hide a category once, and it can be made visible by admin
        }
    }