function __construct(cs_phpDB $db, $location) { parent::__construct($db); $loc = new csb_location($db); $location = $loc->fix_location($location); $criteria = array('isActive' => "t", 'location' => $location); try { $this->validBlogs = $this->get_blogs($criteria, 'blog_display_name DESC'); } catch (exception $e) { throw new exception(__METHOD__ . ": failed to retrieve any valid blogs for location=(" . $location . ")... " . "MORE INFO::: " . $e->getMessage()); } }
/** * Creates new blog entry. * * @param $blogName (str) Display name of blog. * @param $owner (str/int) UID of owner (username converted to uid) * @param $location (str) location of blog * * @return exception throws exception on error * @return (int) Newly created blog_id */ function create_blog($blogName, $owner, $location) { if (!strlen($blogName)) { throw new exception(__METHOD__ . ": invalid blogName (" . $blogName . ")"); } elseif (!strlen($owner)) { throw new exception(__METHOD__ . ": invalid owner (" . $owner . ")"); } if (!is_numeric($owner)) { $username = $owner; //for later $owner = $this->get_uid($owner); if (!is_numeric($owner) || $owner < 1) { throw new exception(__METHOD__ . ": unable to find UID for user (" . $username . ")"); } } //attempt to get/create the location... $loc = new csb_location($this->db); $locationData = $loc->get_location_id($location); $locationId = null; if (is_array($locationData) && isset($locationData[0]) && isset($locationData[0]['location_id'])) { $locationId = $locationData[0]['location_id']; } if (!is_numeric($locationId) || $locationId < 1) { //TODO: should we really be creating this automatically? $locationId = $loc->add_location($location); } $formattedBlogName = $this->create_permalink_from_title($blogName); $sql = 'INSERT INTO csblog_blog_table (blog_display_name, blog_name, ' . 'uid, location_id) VALUES (:display, :name, :uid, :location)'; $params = array('display' => $blogName, 'name' => $formattedBlogName, 'uid' => $owner, 'location' => $locationId); try { $retval = $this->db->run_insert($sql, $params, 'csblog_blog_table_blog_id_seq'); $this->initialize_locals($formattedBlogName); } catch (Exception $e) { throw new exception(__METHOD__ . ": failed to create blog::: " . $e->getMessage()); } return $retval; }
function test_locations() { $locObj = new csb_location($this->dbObj); //FORMAT: <name>=>array(test,expected) $testThis = array('multipleSlashes' => array('////x//y/z///a//b/////////////c/', '/x/y/z/a/b/c'), 'noBeginningSlash' => array('x/y/test/you', '/x/y/test/you'), 'endingSlash' => array('/x/y/', '/x/y'), 'invalidCharacters' => array('/x__354-x@2/3/@/#!%^/&*/()+=@/', '/x__354-x2/3')); $existingLocations = array(); try { $existingLocations = $locObj->get_locations(); throw new exception(__METHOD__ . ": locations already exist... " . $e->getMessage()); } catch (exception $e) { // $this->gfObj->debug_print(__METHOD__ .": no pre-existing locations"); } $this->assertEquals(count($existingLocations), 0); $addedLocations = array(); foreach ($testThis as $testName => $data) { //Test Cleaning... $this->assertEquals($locObj->fix_location($data[1]), $data[1], "Unclean test pattern '" . $testName . "' (" . $locObj->fix_location($data[1]) . ")"); $this->assertEquals($locObj->fix_location($data[0]), $data[1], "Cleaning failed for pattern '" . $testName . "' (" . $locObj->fix_location($data[0]) . ")"); //Test adding the locations. $newLocId = $locObj->add_location($data[0]); if (isset($addedLocations[$data[1]])) { $addedLocations[$data[1]]++; } else { $addedLocations[$data[1]] = 1; } $this->assertTrue(is_numeric($newLocId), "Didn't get valid location_id (" . $newLocId . ")"); $locArr = $locObj->get_locations(); $this->assertEquals($locArr[$newLocId], $data[1], "New location (" . $locArr[$newLocId] . ") does not match expected (" . $data[1] . ")"); //make sure retrieving the location_id using clean & unclean data works properly. $getUncleanLoc = $locObj->get_location_id($data[0]); $getCleanLoc = $locObj->get_location_id($data[1]); // $this->assertEquals($newLocId, $getUncleanLoc); $this->assertEquals($getUncleanLoc, $getCleanLoc); } }