Пример #1
0
 /**
  * {@inheritdoc}
  */
 public function getRelated($sourceObject, $returnType = 'all', $start = 0, $limit = 10, $distinct = '')
 {
     $config = ConfigProvider::getInstance();
     // the result objects
     $results = array();
     // matching tags with weights
     $matches = array();
     // only used in conjunction with distinct param
     $distinctValues = array();
     if ($config->get('cache.provider.name') != '') {
         $key = get_class($sourceObject) . '-' . $sourceObject->getOID() . '-related' . ($distinct == '' ? '' : '-distinct');
         $matches = $this->loadFromCache($key);
     }
     if (count($matches) == 0) {
         // all the tags on the source object for comparison
         $tags = $sourceObject->getPropObject('tags')->getRelatedObjects();
         foreach ($tags as $tag) {
             $Tag = new Tag();
             if ($distinct == '') {
                 $matchingTags = $Tag->query('SELECT * FROM ' . $Tag->getTableName() . " WHERE \n                        content='" . $tag->get('content') . "' AND NOT \n                        (taggedOID = '" . $sourceObject->getOID() . "' AND taggedClass = '" . get_class($sourceObject) . "');");
             } else {
                 // filter out results where the source object field is identical to distinct param
                 $matchingTags = $Tag->query('SELECT * FROM ' . $Tag->getTableName() . " WHERE \n                        content='" . $tag->get('content') . "' AND NOT \n                        (taggedOID = '" . $sourceObject->getOID() . "' AND taggedClass = '" . get_class($sourceObject) . "')\n                        AND taggedOID IN (SELECT OID FROM " . $sourceObject->getTableName() . ' WHERE ' . $distinct . " != '" . addslashes($sourceObject->get($distinct)) . "');");
             }
             foreach ($matchingTags as $matchingTag) {
                 if ($returnType == 'all' || $tag->get('taggedClass') == $returnType) {
                     $key = $matchingTag['taggedClass'] . '-' . $matchingTag['taggedOID'];
                     // matches on the distinct if defined need to be skipped
                     if ($distinct != '') {
                         try {
                             $BO = new $matchingTag['taggedClass']();
                             $BO->load($matchingTag['taggedOID']);
                             // skip where the source object field is identical
                             if ($sourceObject->get($distinct) == $BO->get($distinct)) {
                                 continue;
                             }
                             if (!in_array($BO->get($distinct), $distinctValues)) {
                                 $distinctValues[] = $BO->get($distinct);
                             } else {
                                 continue;
                             }
                         } catch (RecordNotFoundException $e) {
                             self::$logger->warn('Error loading object [' . $matchingTag['taggedOID'] . '] of type [' . $matchingTag['taggedClass'] . '], probable orphan');
                         }
                     }
                     if (isset($matches[$key])) {
                         // increment the weight if the same BO is tagged more than once
                         $weight = intval($matches[$key]) + 1;
                         $matches[$key] = $weight;
                     } else {
                         $matches[$key] = 1;
                     }
                 }
             }
             if ($config->get('cache.provider.name') != '') {
                 $key = get_class($sourceObject) . '-' . $sourceObject->getOID() . '-related' . ($distinct == '' ? '' : '-distinct');
                 $this->addToCache($key, $matches);
             }
         }
     }
     // sort the matches based on tag frequency weight
     arsort($matches);
     $this->numberFound = count($matches);
     // now paginate
     $matches = array_slice($matches, $start, $limit);
     // now load each object
     foreach ($matches as $key => $weight) {
         $parts = explode('-', $key);
         $BO = new $parts[0]();
         $BO->load($parts[1]);
         $results[] = $BO;
     }
     return $results;
 }