/** * Checks if a given place already exists and creates it if it doesn't * * @param string $place_name The place's name * @param Model_Droplet $droplet_id Droplet id this link belongs to * @param Model_Account $account_id Account id this link belongs to * @return Model_Account_Droplet_place */ public static function get_place($place_name, $droplet_id, $account_id) { $orm_place = Model_Place::get_place_by_name($place_name, TRUE); $account_place = ORM::factory('account_droplet_place')->where('droplet_id', '=', $droplet_id)->where('place_id', '=', $orm_place->id)->where('account_id', '=', $account_id)->find(); if (!$account_place->loaded()) { $account_place->place_id = $orm_place->id; $account_place->droplet_id = $droplet_id; $account_place->account_id = $account_id; $account_place->save(); } return $account_place; }
/** * @covers Model_Place::get_place_by_lat_lon */ public function test_get_place_by_lat_lon() { // Valid place data; non-existent place $place_data = array('place_name' => 'Place1', 'latitude' => -89.28332996368408, 'longitude' => 179.81666946411133, 'source' => 'unittest'); // Find the place and save if not found $result = Model_Place::get_place_by_lat_lon($place_data, TRUE); $this->assertInstanceOf('Model_Place', $result); $result->delete(); // Invalid place data $result = Model_Place::get_place_by_lat_lon(array('place_name' => 'PlaceY')); $this->assertFalse($result); // Garbage collection unset($result, $place_data); }
/** * Creates a droplets from the given array * * @param array $droplet * @return array */ public static function create_from_array($droplets) { if (!count($droplets)) { return; } // Populate identities Model_Identity::get_identities($droplets); // Hash array with droplet_hash as key and index in droplets array that contain that hash $droplets_idx = array(); foreach ($droplets as $key => &$droplet) { if (!isset($droplet['id'])) { $hash = md5($droplet['identity_orig_id'] . $droplet['channel'] . $droplet['droplet_orig_id']); $droplet['droplet_hash'] = $hash; if (empty($droplets_idx[$hash])) { $droplets_idx[$hash] = array(); } $droplets_idx[$hash][] = $key; } } // Insert new drops $new_droplets = array(); if (!empty($droplets_idx)) { Swiftriver_Mutex::obtain(get_class(), 3600); // Find the drops that already exist by their droplet_hash $found_query = DB::select('droplet_hash', 'id')->from('droplets')->where('droplet_hash', 'IN', array_keys($droplets_idx)); $found = $found_query->execute()->as_array(); // Update the ids of existing drops found in the db and // remove them from droplets_idx to leave new drops $new_droplet_count = count($droplets_idx); foreach ($found as $hash) { foreach ($droplets_idx[$hash['droplet_hash']] as $key) { $droplets[$key]['id'] = $hash['id']; } $new_droplet_count--; unset($droplets_idx[$hash['droplet_hash']]); } if (!empty($droplets_idx)) { // Get a range of IDs to be used in inserting the new drops $base_id = Model_Droplet::get_ids($new_droplet_count); // Insert into the droplets table $query = DB::insert('droplets', array('id', 'channel', 'droplet_hash', 'droplet_orig_id', 'droplet_type', 'droplet_title', 'droplet_content', 'droplet_date_pub', 'droplet_date_add', 'identity_id', 'processing_status')); foreach ($droplets_idx as $hash => $keys) { foreach ($keys as $key) { $droplets[$key]['id'] = $base_id; } // PHP has reference issues with array so // we cannot copy the element we have but // refeference it in place as below $droplets[$keys[0]] // otherwise the element will be overwriten if we use a // copy. Sigh. $new_droplets[] = $droplets[$keys[0]]; $query->values(array('id' => $base_id++, 'channel' => $droplets[$keys[0]]['channel'], 'droplet_hash' => $droplets[$keys[0]]['droplet_hash'], 'droplet_orig_id' => $droplets[$keys[0]]['droplet_orig_id'], 'droplet_type' => $droplets[$keys[0]]['droplet_type'], 'droplet_title' => $droplets[$keys[0]]['droplet_title'], 'droplet_content' => $droplets[$keys[0]]['droplet_content'], 'droplet_date_pub' => $droplets[$keys[0]]['droplet_date_pub'], 'droplet_date_add' => gmdate("Y-m-d H:i:s", time()), 'identity_id' => $droplets[$keys[0]]['identity_id'], 'processing_status' => self::PROCESSING_STATUS_NEW)); } $query->execute(); } Swiftriver_Mutex::release(get_class()); } // Populate metadata IDs into the drops array Model_Tag::get_tags($droplets); Model_Link::get_links($droplets); Model_Place::get_places($droplets); Model_Media::get_media($droplets); // Populate the drop's metadata tables self::add_metadata($droplets); return array($droplets, $new_droplets); }