public function test_insert_and_update_and_delete()
 {
     $term1 = EE_Term::new_instance(array('name' => 'monkey1', 'slug' => 'monkey1'));
     $term2 = EE_Term::new_instance(array('name' => 'monkey2', 'slug' => 'monkey2'));
     $term1->save();
     $term2->save();
     $tt_1 = EE_Term_Taxonomy::new_instance(array('taxonomy' => 'whatever', 'term_id' => $term1->ID()));
     $tt_1->save();
     $tt_2 = EE_Term_Taxonomy::new_instance(array('taxonomy' => 'whatever', 'term_id' => $term2->ID()));
     $tt_2->save();
     $e = EE_Event::new_instance(array('EVT_name' => 'for_term_1'));
     $e->save();
     //ok done setup
     //test INSERT
     $this->assertEquals(0, $tt_1->count());
     $new_tr_id = EEM_Term_Relationship::instance()->insert(array('term_taxonomy_id' => $tt_1->ID(), 'object_id' => $e->ID()));
     $this->assertNotNull($new_tr_id);
     //refresh out term_taxonomy objects, as the database has changed
     $tt_1 = EEM_Term_Taxonomy::reset()->get_one_by_ID($tt_1->ID());
     $tt_2 = EEM_Term_Taxonomy::instance()->get_one_by_ID($tt_2->ID());
     $this->assertEquals(1, $tt_1->count());
     $this->assertEquals(0, $tt_2->count());
     //test UPDATE... except we can't update term_relationship because there's no Primary Key
     //on it. This should be fixed at some point
     //@todo: fix this test
     //		$updated = EEM_Term_Relationship::instance()->update_by_ID(array('term_taxonomy_id'=>$tt_2->ID() ), $new_tr_id );
     //		//refresh out term_taxonomy objects, as the database has changed
     //		$tt_1 = EEM_Term_Taxonomy::reset()->get_one_by_ID( $tt_1->ID() );
     //		$tt_2 = EEM_Term_Taxonomy::instance()->get_one_by_ID($tt_2->ID() );
     //		$this->assertEquals( 0, $tt_1->count() );
     //		$this->assertEquals(1,$tt_2->count() );
     //test DELETE
     //@todo: fix this test too. see above
     //		$count_deleted = EEM_Term_Relationship::instance()->delete_by_ID($new_tr_id);
     //		$this->assertNotEmpty( $count_deleted );
     //		//refresh out term_taxonomy objects, as the database has changed
     //		$tt_1 = EEM_Term_Taxonomy::reset()->get_one_by_ID( $tt_1->ID() );
     //		$tt_2 = EEM_Term_Taxonomy::instance()->get_one_by_ID($tt_2->ID() );
     //		$this->assertEquals( 0, $tt_1->count() );
     //		$this->assertEquals(0,$tt_2->count() );
 }
 public function get_terms($taxonomy = 'espresso_people_categories', $per_page = 10, $current_page = 1, $count = FALSE)
 {
     global $wpdb;
     //testing term stuff
     $orderby = isset($this->_req_data['orderby']) ? $this->_req_data['orderby'] : 'Term.term_id';
     $order = isset($this->_req_data['order']) ? $this->_req_data['order'] : 'DESC';
     $limit = ($current_page - 1) * $per_page;
     $where = array('taxonomy' => $taxonomy);
     if (isset($this->_req_data['s'])) {
         $sstr = '%' . $this->_req_data['s'] . '%';
         $where['OR'] = array('Term.name' => array('LIKE', $sstr), 'description' => array('LIKE', $sstr));
     }
     $query_params = array($where, 'order_by' => array($orderby => $order), 'limit' => $limit . ',' . $per_page, 'force_join' => array('Term'));
     $terms = $count ? EEM_Term_Taxonomy::instance()->count($query_params, 'term_id') : EEM_Term_Taxonomy::instance()->get_all($query_params);
     return $terms;
 }
 public function column_PER_event_types($item)
 {
     EE_Registry::instance()->load_helper('URL');
     //first do a query to get all the types for this user for where they are assigned to an event.
     $event_type_IDs = EEM_Person_Post::instance()->get_col(array(array('PER_ID' => $item->ID(), 'OBJ_type' => 'Event')), 'PT_ID');
     $event_types = EEM_Term_Taxonomy::instance()->get_all(array(array('term_taxonomy_id' => array('IN', $event_type_IDs))));
     //loop through the types and setup the pills and the links
     $content = '<ul class="person-to-cpt-people-type-list">';
     foreach ($event_types as $type) {
         $name = $type->get_first_related('Term')->get('name');
         $count = EEM_Person_Post::instance()->count(array(array('PER_ID' => $item->ID(), 'OBJ_type' => 'Event', 'PT_ID' => $type->ID())));
         $event_filter_link = EEH_URL::add_query_args_and_nonce(array('page' => 'espresso_events', 'action' => 'default', 'PER_ID' => $item->ID(), 'PT_ID' => $type->ID()), admin_url('admin.php'));
         $content .= '<li><a href="' . $event_filter_link . '">' . $name . '<span class="person-type-count">' . $count . '</span></a></li>';
     }
     $content .= '</ul>';
     echo $content;
 }
 protected function _setup_data()
 {
     $this->_data = $this->_admin_page->get_categories($this->_per_page, $this->_current_page);
     $this->_all_data_count = EEM_Term_Taxonomy::instance()->count(array(array('taxonomy' => 'espresso_event_categories')));
 }
 /**
  * Adds an event category with the specified name and description to the specified
  * $cpt_model_object. Intelligently adds a term if necessary, and adds a term_taxonomy if necessary,
  * and adds an entry in the term_relationship if necessary.
  * @param EE_CPT_Base $cpt_model_object
  * @param string $category_name (used to derive the term slug too)
  * @param string $category_description
  * @param int $parent_term_taxonomy_id
  * @return EE_Term_Taxonomy
  */
 function add_event_category(EE_CPT_Base $cpt_model_object, $category_name, $category_description = '', $parent_term_taxonomy_id = null)
 {
     //create term
     require_once EE_MODELS . 'EEM_Term.model.php';
     //first, check for a term by the same name or slug
     $category_slug = sanitize_title($category_name);
     $term = EEM_Term::instance()->get_one(array(array('OR' => array('name' => $category_name, 'slug' => $category_slug))));
     if (!$term) {
         $term = EE_Term::new_instance(array('name' => $category_name, 'slug' => $category_slug));
         $term->save();
     }
     //make sure there's a term-taxonomy entry too
     require_once EE_MODELS . 'EEM_Term_Taxonomy.model.php';
     $term_taxonomy = EEM_Term_Taxonomy::instance()->get_one(array(array('term_id' => $term->ID(), 'taxonomy' => EE_Event_Category_Taxonomy)));
     /** @var $term_taxonomy EE_Term_Taxonomy */
     if (!$term_taxonomy) {
         $term_taxonomy = EE_Term_Taxonomy::new_instance(array('term_id' => $term->ID(), 'taxonomy' => EE_Event_Category_Taxonomy, 'description' => $category_description, 'count' => 1, 'parent' => $parent_term_taxonomy_id));
         $term_taxonomy->save();
     } else {
         $term_taxonomy->set_count($term_taxonomy->count() + 1);
         $term_taxonomy->save();
     }
     return $this->add_relationship_to($cpt_model_object, $term_taxonomy, 'Term_Taxonomy');
 }
 /**
  * Utility function used to get various items from the Person_Post model depending on the given params.
  * When no obj_id is provided, we use what is set as the $post id if present.
  *
  * @param int         $obj_id              What the obj_id is for comparing against.
  * @param string    $primary_obj_type What it is being retrieved.
  *
  * @return EE_Base_Class[]  (what is returned depends on what the primary_obj_type is)
  */
 protected static function _get_rel_objects($obj_id = 0, $primary_obj_type = 'Person')
 {
     $objects = array();
     if (empty($obj_id)) {
         global $post;
         $obj_id = $post instanceof WP_Post ? $post->ID : $obj_id;
     }
     //still empty? return empty array
     if (empty($obj_id)) {
         return array();
     }
     if ($primary_obj_type != 'Person') {
         $where = array('PER_ID' => $obj_id, 'OBJ_type' => $primary_obj_type);
         $query = array($where);
     } else {
         $where = array('OBJ_ID' => $obj_id);
         $query = array($where, 'order_by' => array('PER_OBJ_order' => 'ASC'));
     }
     $object_items = EEM_Person_Post::instance()->get_all($query);
     $term_name_cache = array();
     if (method_exists(EEM_Event::instance(), 'public_event_stati')) {
         $public_event_stati = EEM_Event::instance()->public_event_stati();
     } else {
         $public_event_stati = get_post_stati(array('public' => TRUE));
         foreach (EEM_Event::instance()->get_custom_post_statuses() as $custom_post_status) {
             $public_event_stati[] = strtolower(str_replace(' ', '_', $custom_post_status));
         }
     }
     foreach ($object_items as $object_item) {
         if (!isset($term_name_cache[$object_item->get('PT_ID')]) || !isset($objects[$term_name][$object_item->ID()])) {
             $term_name = EEM_Term_Taxonomy::instance()->get_one_by_ID($object_item->get('PT_ID'))->get_first_related('Term')->get('name');
             $related_object = $object_item->get_first_related($primary_obj_type, array(array('status' => array('IN', apply_filters('FHEE__EEH_People_View__get_rel_objects__public_event_stati', $public_event_stati)))));
             if ($related_object instanceof EE_Base_Class) {
                 $objects[$term_name][$object_item->ID()] = $related_object;
                 $term_name_cache[$object_item->get('PT_ID')] = $term_name;
             }
         }
     }
     return $objects;
 }
 /**
  * @todo: account for wp 4.2 term splitting (https://developer.wordpress.org/plugins/taxonomy/working-with-split-terms-in-wp-4-2/)
  */
 function test_save_data_array_to_db__from_this_site__term_split()
 {
     //create term and term taxonomy
     $term = $this->new_model_obj_with_dependencies('Term', array('name' => 'Jaguar', 'slug' => 'jag'));
     $ttcar = $this->new_model_obj_with_dependencies('Term_Taxonomy', array('term_id' => $term->ID(), 'taxonomy' => 'cars', 'description' => 'A fast car'));
     $ttcat = $this->new_model_obj_with_dependencies('Term_Taxonomy', array('term_id' => $term->ID(), 'taxonomy' => 'cats', 'description' => 'A large black cat that likes to swim'));
     //create "csv" data for it (pretend exported)
     $csv_data = array('Term' => array($term->model_field_array()), 'Term_Taxonomy' => array($ttcar->model_field_array(), $ttcat->model_field_array()));
     $this->assertEquals($ttcat->get('term_id'), $ttcar->get('term_id'));
     //split the term in the "wp" way. Our model objet $ttcar will NOT get updated on its own
     $new_term_id_for_car = _split_shared_term($term->ID(), $ttcar->ID());
     $ttcar = EEM_Term_Taxonomy::instance()->refresh_entity_map_from_db($ttcar->ID());
     //		echo "updated term taxonomy:";var_dump($ttcar->model_field_array());
     $this->assertNotEquals($ttcat->get('term_id'), $ttcar->get('term_id'));
     //import it
     $new_mapping = EE_Import::instance()->save_data_rows_to_db($csv_data, false, array());
     $ttcar = EEM_Term_Taxonomy::instance()->refresh_entity_map_from_db($ttcar->ID());
     //when it's done importing, we should have saved a term-taxonomy for the new term, not re-inserted a term-taxonomy to the old term
     //and because it used the models, the model objects we have in scope should already be up-to-date
     $this->assertEquals($new_term_id_for_car, $ttcar->get('term_id'));
 }
 protected function _setup_data()
 {
     $this->_data = $this->_admin_page->get_terms('espresso_people_type', $this->_per_page, $this->_current_page);
     $this->_all_data_count = EEM_Term_Taxonomy::instance()->count(array(array('taxonomy' => 'espresso_people_type')));
 }
 /**
  * Callback for FHEE__EE_Admin_Page___display_admin_list_table_page__before_list_table__template_arg used to add
  * context title for when the event list table is filtered by person and person type.
  * @param $original_content
  * @param $page_slug
  * @param $request_data
  * @param $request_action
  */
 public function filtered_events_list_table_title($original_content, $page_slug, $request_data, $request_action)
 {
     if ($page_slug === 'espresso_events' && $request_action === 'default') {
         $person = $person_type = null;
         if (isset($request_data['PER_ID'])) {
             $person = EEM_Person::instance()->get_one_by_ID($request_data['PER_ID']);
         }
         if (isset($request_data['PT_ID'])) {
             $person_type = EEM_Term_Taxonomy::instance()->get_one_by_ID($request_data['PT_ID']);
             $person_type = $person_type instanceof EE_Term_Taxonomy ? $person_type->get_first_related('Term') : null;
         }
         if ($person instanceof EE_Person && $person_type instanceof EE_Term) {
             $title = sprintf(__('Viewing the events that %s is assigned to as %s', 'event_espresso'), $person->full_name(), $person_type->name());
         } elseif ($person instanceof EE_Person && !$person_type instanceof EE_Term) {
             $title = sprintf(__('Viewing the events that %s is assigned to', 'event_espresso'), $person->full_name());
         } elseif (!$person instanceof EE_Person && $person_type instanceof EE_Term) {
             $title = sprintf(__('Viewing the events that has at least one person assigned as %s.', 'event_espresso'), $person_type->name());
         } else {
             $title = '';
         }
         if ($title) {
             return '<h2>' . $title . '</h2>';
         }
     }
     return $original_content;
 }