/** * Add menus to the publish form **/ public function action_form_publish($form, $post) { $menus = $this->get_menus(); $menulist = array(); foreach ($menus as $menu) { $menulist[$menu->id] = $menu->name; } $settings = $form->publish_controls->append('fieldset', 'menu_set', _t('Menus', 'termmenus')); $settings->append('checkboxes', 'menus', 'null:null', _t('Menus', 'termmenus'), $menulist); // If this is an existing post, see if it has categories already if (0 != $post->id) { // Get the terms associated to this post $object_terms = Vocabulary::get_all_object_terms('post', $post->id); $menu_ids = array_keys($menulist); $value = array(); // if the term is in a menu vocab, enable that checkbox foreach ($object_terms as $term) { if (in_array($term->vocabulary_id, $menu_ids)) { $value[] = $term->vocabulary_id; } } $form->menus->value = $value; } }
public function test_object_terms() { $post = Post::create( array( 'title' => 'Unit Test Post', 'content' => 'This is a unit test post to test setting and getting terms.', 'user_id' => 1, 'status' => Post::status( 'draft' ), 'content_type' => Post::type( 'entry' ), 'pubdate' => HabariDateTime::date_create(), ) ); $v = Vocabulary::get( 'tags' ); // Test setting terms with strings $new_terms = array( 'habari', 'unit test' ); $v->set_object_terms( 'post', $post->id, $new_terms ); $terms = $v->get_object_terms( 'post', $post->id ); $t = array(); foreach( $terms as $term ) { $t[] = (string)$term; } $this->assert_equal( 2, count( $terms ) ); $this->assert_equal( 0, count( array_diff( $new_terms, $t ) ) ); // Test get_all_object_terms $nv = Vocabulary::create( array( 'name' => 'animals', 'description' => 'Types of animals.', 'features' => array( 'hierarchical' ) ) ); $root = $nv->add_term( 'Animal Kingdom' ); $nv->set_object_terms( 'post', $post->id, array( $root ) ); $terms = Vocabulary::get_all_object_terms( 'post', $post->id ); $new_terms[] = 'Animal Kingdom'; $t = array(); foreach( $terms as $term ) { $t[] = (string)$term; } $this->assert_equal( 3, count( $terms ) ); Utils::debug( 3, count( $terms ) ); $this->assert_equal( 0, count( array_diff( $new_terms, $t ) ) ); Utils::debug( 0, count( array_diff( $new_terms, $t ) ) ); $v->delete_term( 'unit test' ); $post->delete(); $nv->delete(); }