Source for file TagObject.inc
Documentation is available at TagObject.inc
require_once $config->get('sysRoot'). 'alpha/model/AlphaDAO.inc';
require_once $config->get('sysRoot'). 'alpha/util/helpers/AlphaValidator.inc';
* The tag class used in tag clouds and search
* @author John Collins <dev@alphaframework.org>
* @version $Id: TagObject.inc 1341 2011-03-17 15:02:02Z johnc $
* @license http://www.opensource.org/licenses/bsd-license.php The BSD License
* @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the
* following conditions are met:
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
* * Neither the name of the Alpha Framework nor the names
* of its contributors may be used to endorse or promote
* products derived from this software without specific
* prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* The name of the class of the object which is tagged
* The OID of the object which is tagged
* An array of data display labels for the class properties
protected $dataLabels = array("OID"=> "Tag ID#","taggedClass"=> "Class Name","taggedOID"=> "Tagged Object ID#","content"=> "Tag");
* The name of the database table for the class
private static $logger = null;
self::$logger = new Logger('TagObject');
// ensure to call the parent constructor
* Returns an array of TagObjects matching the class and OID provided
* @param $taggedClass The class name of the DAO that has been tagged.
* @param $taggedOID The Object ID of the DAO that has been tagged.
public function loadTags($taggedClass, $taggedOID) {
$sqlQuery = "SELECT OID FROM ". $this->getTableName(). " WHERE taggedOID='$taggedOID' AND taggedClass='$taggedClass' ORDER BY OID";
// now build an array of objects to be returned
while($row = $result->fetch_array(MYSQLI_ASSOC)) {
* Returns a hash array of the most popular tags based on their occurence in the database,
* ordered by alphabet and restricted to the a count matching the $limit supplied. The
* returned has array uses the tag content as a key and the database value as a value.
$sqlQuery = "SELECT content, count(*) as count FROM `tag` GROUP BY content ORDER BY count DESC LIMIT $limit";
// now build an array of tags to be returned
while($row = $result->fetch_array(MYSQLI_ASSOC)) {
$popTags[$row['content']] = $row['count'];
// sort the array by content key before returning
* Use this callback to create the tclass_toid__tcontent unique table index
foreach ($indexNames as $index) {
if ('tclass_toid_tcontent_unq_idx' == $index) {
$sqlQuery = 'CREATE UNIQUE INDEX tclass_toid_tcontent_unq_idx ON '. $this->getTableName(). ' (taggedClass,taggedOID,content);';
* Splits the passed content by spaces, filters (removes) stop words from stopwords.ini,
* and returns an array of TagObject instances.
* @param $taggedClass Optionally provide a BO class name
* @param $taggedOID Optionally provide a BO instance OID
* @param $applyStopwords Defaults true, set to false if you want to ignore the stopwords.
public static function tokenize($content, $taggedClass= '', $taggedOID= '', $applyStopwords= true) {
if(self::$logger == null)
self::$logger = new Logger('TagObject');
array_walk($lowerWords, 'TagObject::lowercaseArrayElement');
if(file_exists($config->get('sysRoot'). 'config/stopwords-'. $config->get('sysStopwordsSize'). '.ini')) {
$stopwords = file($config->get('sysRoot'). 'config/stopwords-'. $config->get('sysStopwordsSize'). '.ini', FILE_IGNORE_NEW_LINES);
}elseif(file_exists($config->get('sysRoot'). 'alpha/stopwords-'. $config->get('sysStopwordsSize'). '.ini')) {
$stopwords = file($config->get('sysRoot'). 'alpha/stopwords-'. $config->get('sysStopwordsSize'). '.ini', FILE_IGNORE_NEW_LINES);
throw new AlphaException('Unable to find a stopwords-'. $config->get('sysStopwordsSize'). '.ini file in the application!');
array_walk($stopwords, 'TagObject::lowercaseArrayElement');
foreach($filtered as $tagContent) {
// we only want to create word tags
// just making sure that we haven't added this one in already
if(!in_array($tagContent, $tagContents) && !empty($tagContent)) {
$tag->set('taggedClass', $taggedClass);
$tag->set('taggedOID', $taggedOID);
self::$logger->debug('Tags generated: ['. var_export($tagContents, true). ']');
* Applies trim() and strtolower to the array element passed by reference
* @param $key (not required)
private static function lowercaseArrayElement(&$element, $key) {
* Cleans tag content by remocing white spaces and converting to lowercase.
|