5 use RedBeanPHP\ToolBox as ToolBox;
6 use RedBeanPHP\AssociationManager as AssociationManager;
7 use RedBeanPHP\OODBBean as OODBBean;
10 * RedBeanPHP Tag Manager.
12 * The tag manager offers an easy way to quickly implement basic tagging
15 * Provides methods to tag beans and perform tag-based searches in the
18 * @file RedBeanPHP/TagManager.php
19 * @author Gabor de Mooij and the RedBeanPHP community
23 * copyright (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community
24 * This source file is subject to the BSD/GPLv2 License that is bundled
25 * with this source code in the file license.txt.
35 * @var AssociationManager
37 protected $associationManager;
45 * Checks if the argument is a comma separated string, in this case
46 * it will split the string into words and return an array instead.
47 * In case of an array the argument will be returned 'as is'.
49 * @param array|string $tagList list of tags
53 private function extractTagsIfNeeded( $tagList )
55 if ( $tagList !== FALSE && !is_array( $tagList ) ) {
56 $tags = explode( ',', (string) $tagList );
65 * Finds a tag bean by it's title.
68 * @param string $title title to search for
72 protected function findTagByTitle( $title )
74 $beans = $this->redbean->find( 'tag', array( 'title' => array( $title ) ) );
77 $bean = reset( $beans );
87 * The tag manager offers an easy way to quickly implement basic tagging
90 * @param ToolBox $toolbox toolbox object
92 public function __construct( ToolBox $toolbox )
94 $this->toolbox = $toolbox;
95 $this->redbean = $toolbox->getRedBean();
97 $this->associationManager = $this->redbean->getAssociationManager();
101 * Tests whether a bean has been associated with one ore more
102 * of the listed tags. If the third parameter is TRUE this method
103 * will return TRUE only if all tags that have been specified are indeed
104 * associated with the given bean, otherwise FALSE.
105 * If the third parameter is FALSE this
106 * method will return TRUE if one of the tags matches, FALSE if none
109 * Tag list can be either an array with tag names or a comma separated list
112 * @param OODBBean $bean bean to check for tags
113 * @param array|string $tags list of tags
114 * @param boolean $all whether they must all match or just some
118 public function hasTag( $bean, $tags, $all = FALSE )
120 $foundtags = $this->tag( $bean );
122 $tags = $this->extractTagsIfNeeded( $tags );
123 $same = array_intersect( $tags, $foundtags );
126 return ( implode( ',', $same ) === implode( ',', $tags ) );
129 return (bool) ( count( $same ) > 0 );
133 * Removes all sepcified tags from the bean. The tags specified in
134 * the second parameter will no longer be associated with the bean.
136 * Tag list can be either an array with tag names or a comma separated list
139 * @param OODBBean $bean tagged bean
140 * @param array|string $tagList list of tags (names)
144 public function untag( $bean, $tagList )
146 $tags = $this->extractTagsIfNeeded( $tagList );
148 foreach ( $tags as $tag ) {
149 if ( $t = $this->findTagByTitle( $tag ) ) {
150 $this->associationManager->unassociate( $bean, $t );
156 * Tags a bean or returns tags associated with a bean.
157 * If $tagList is NULL or omitted this method will return a
158 * comma separated list of tags associated with the bean provided.
159 * If $tagList is a comma separated list (string) of tags all tags will
160 * be associated with the bean.
161 * You may also pass an array instead of a string.
163 * Tag list can be either an array with tag names or a comma separated list
166 * @param OODBBean $bean bean to be tagged
167 * @param array|string $tagList a list of tags
171 public function tag( OODBBean $bean, $tagList = NULL )
173 if ( is_null( $tagList ) ) {
175 $tags = $bean->sharedTag;
176 $foundTags = array();
178 foreach ( $tags as $tag ) {
179 $foundTags[] = $tag->title;
185 $this->associationManager->clearRelations( $bean, 'tag' );
186 $this->addTags( $bean, $tagList );
192 * Adds tags to a bean.
193 * If $tagList is a comma separated list of tags all tags will
194 * be associated with the bean.
195 * You may also pass an array instead of a string.
197 * Tag list can be either an array with tag names or a comma separated list
200 * @param OODBBean $bean bean to add tags to
201 * @param array|string $tagList list of tags to add to bean
205 public function addTags( OODBBean $bean, $tagList )
207 $tags = $this->extractTagsIfNeeded( $tagList );
209 if ( $tagList === FALSE ) {
213 foreach ( $tags as $tag ) {
214 if ( !$t = $this->findTagByTitle( $tag ) ) {
215 $t = $this->redbean->dispense( 'tag' );
218 $this->redbean->store( $t );
221 $this->associationManager->associate( $bean, $t );
226 * Returns all beans that have been tagged with one or more
227 * of the specified tags.
229 * Tag list can be either an array with tag names or a comma separated list
232 * @param string $beanType type of bean you are looking for
233 * @param array|string $tagList list of tags to match
234 * @param string $sql additional SQL (use only for pagination)
235 * @param array $bindings bindings
239 public function tagged( $beanType, $tagList, $sql = '', $bindings = array() )
241 $tags = $this->extractTagsIfNeeded( $tagList );
242 $records = $this->toolbox->getWriter()->queryTagged( $beanType, $tags, FALSE, $sql, $bindings );
244 return $this->redbean->convertToBeans( $beanType, $records );
248 * Returns all beans that have been tagged with ALL of the tags given.
250 * Tag list can be either an array with tag names or a comma separated list
253 * @param string $beanType type of bean you are looking for
254 * @param array|string $tagList list of tags to match
258 public function taggedAll( $beanType, $tagList, $sql = '', $bindings = array() )
260 $tags = $this->extractTagsIfNeeded( $tagList );
261 $records = $this->toolbox->getWriter()->queryTagged( $beanType, $tags, TRUE, $sql, $bindings );
263 return $this->redbean->convertToBeans( $beanType, $records );