Просмотр исходного кода

#87 keywords metatags partially done

Alex Trofimov 12 лет назад
Родитель
Сommit
274310ecda

+ 80 - 22
inc/classes/BxDolMetatags.php

@@ -9,6 +9,8 @@
 
 bx_import('BxDolMetatagsQuery');
 
+define('BX_METATAGS_KEYWORDS_MAX', 9);
+
 /**
  * @page objects
  * @section metatags Meta-tags
@@ -19,6 +21,7 @@ bx_import('BxDolMetatagsQuery');
 // TODO: implementation with some modules
 // TODO: integrate to comments, try to integrate to one metatags object - maybe specify metatags object in comments object and make appropriate changes in comments, comments content should be treated as main conetent and main content should be shown in search results)
 // TODO: pages for listing content by @keyword, @mention, country location, city location, or BETTER try integrate it to the search engine, to not make separate pages in each module
+// TODO: integration with notifications, when smbd is @mentioned
 
 /**
  * Meta-tags for different content.
@@ -27,20 +30,19 @@ class BxDolMetatags extends BxDol implements iBxDolFactoryObject
 {
     protected $_sObject;
     protected $_aObject;
-    protected $_mixedContentId;
+    protected $_oQuery;
     protected $_aMetas = array ();
 
     /**
      * Constructor
      * @param $aObject array of metags object options
      */
-    public function __construct($aObject, $mixedContentId)
+    public function __construct($aObject)
     {
         parent::__construct();
 
         $this->_sObject = $aObject['object'];
         $this->_aObject = $aObject;
-        $this->_mixedContentId = $mixedContentId;
         
         $a = array ('keywords', 'locations', 'mentions', 'pictures');
         foreach ($a as $sMeta) {
@@ -48,6 +50,8 @@ class BxDolMetatags extends BxDol implements iBxDolFactoryObject
                 continue;
             $this->_aMetas[] = $sMeta;
         }
+
+        $this->_oQuery = new BxDolMetatagsQuery($this->_aObject);
     }
 
     /**
@@ -55,9 +59,9 @@ class BxDolMetatags extends BxDol implements iBxDolFactoryObject
      * @param $sObject object name
      * @return object instance or false on error
      */
-    static public function getObjectInstance($sObject, $mixedContentId)
+    static public function getObjectInstance($sObject)
     {
-        if (isset($GLOBALS['bxDolClasses']['BxDolMetatags!'.$sObject]))
+        if (isset($GLOBALS['bxDolClasses']['BxDolMetatags!'.$sObject])) 
             return $GLOBALS['bxDolClasses']['BxDolMetatags!'.$sObject];
 
         $aObject = BxDolMetatagsQuery::getMetatagsObject($sObject);
@@ -70,7 +74,7 @@ class BxDolMetatags extends BxDol implements iBxDolFactoryObject
         else
             bx_import($sClass);
 
-        $o = new $sClass($aObject, $mixedId);
+        $o = new $sClass($aObject);
 
         return ($GLOBALS['bxDolClasses']['BxDolMetatags!'.$sObject] = $o);
     }
@@ -79,11 +83,12 @@ class BxDolMetatags extends BxDol implements iBxDolFactoryObject
      * Add all available meta tags to the head section 
      * @return number of successfully added metas
      */
-    public function metaAdd() {
+    public function metaAdd($iId) 
+    {
         $i = 0;
         foreach ($this->_aMetas as $sMeta) {
             $sFunc = $sMeta . 'AddMeta';
-            $i += $this->$sFunc();
+            $i += $this->$sFunc($iId);
         }
         return $i;
     }
@@ -92,33 +97,62 @@ class BxDolMetatags extends BxDol implements iBxDolFactoryObject
 
     /**
      * Add #keywords from the string
+     * @param $iId content id
      * @param $s string with #keywords
      * @return number of found keywords
      */
-    public function keywordsAdd($s) {
-        // TODO:        
+    public function keywordsAdd($iId, $s) 
+    {
+        $a = array();
+        if (!preg_match_all('/(\#[\pL\pN]+)/u', $s, $a))
+            return 0;
+
+        $a[0] = array_unique($a[0]);
+
+        return $this->_oQuery->keywordsAdd($iId, array_slice($a[0], 0, BX_METATAGS_KEYWORDS_MAX));
     }
 
     /**
      * Add links to the #keywords in the string
+     * @param $iId content id
      * @param $s string with #keywords
      * @return modified string where all #keywords are transformed to links with rel="tag" attribute
      */
-    public function keywordsParse($s) {
-        // TODO:
+    public function keywordsParse($iId, $s) 
+    {   
+        $a = $this->keywordsGet($iId);
+        if (empty($a))
+            return $s;
+    
+        foreach ($a as $sKeyword)
+            $s = str_ireplace('#' . $sKeyword, '<a href="#"><s>#</s><b>' . $sKeyword . '</b></a>', $s);
+
+        return $s;
     }
 
     /**
      * Add keywords meta info to the head section
+     * @param $iId content id
      */
-    protected function keywordsAddMeta() {
+    protected function keywordsAddMeta($iId) 
+    {
         // TODO:
     }
 
+    /**
+     * Get list of keywords associated with the content
+     * @return array of keywords
+     */
+    public function keywordsGet($iId)
+    {
+        return $this->_oQuery->keywordsGet($iId);
+    }
+
 
 
     /**
      * Add location for the content
+     * @param $iId content id
      * @param $sLatitude latitude
      * @param $sLongitude longitude
      * @param $sCountryCode optional 2 letters country code (ISO 3166-1)
@@ -127,22 +161,27 @@ class BxDolMetatags extends BxDol implements iBxDolFactoryObject
      * @param $sZip optional ZIP/postcode
      * @return true if location was added, or false otherwise
      */
-    public function locationsAdd($sLatitude, $sLongitude, $sCountryCode = '', $sState = '', $sCity = '', $sZip = '') {
+    public function locationsAdd($iId, $sLatitude, $sLongitude, $sCountryCode = '', $sState = '', $sCity = '', $sZip = '') 
+    {
         // TODO:        
     }
 
     /**
      * Get locations string with links
+     * @param $iId content id
      * @return string with links to country and city
      */
-    public function locationsString() {
+    public function locationsString($iId) 
+    {
         // TODO:
     }
 
     /**
      * Add keywords meta info to the head section
+     * @param $iId content id
      */
-    protected function locationsAddMeta() {
+    protected function locationsAddMeta($iId) 
+    {
         // TODO:
     }
 
@@ -150,49 +189,68 @@ class BxDolMetatags extends BxDol implements iBxDolFactoryObject
 
     /**
      * Add @mentions from the string (most probably @mentions will be some sort of links already, so parsing may have to look for smth like <a data-mention="bx_persons|123">mention name</a> instead of @mention, since there is no usernames for profiles modules and name could contain spaces and othr characters)
+     * @param $iId content id
      * @param $s string with @mentions
      * @return number of found mentions 
      */
-    public function mentionsAdd($s) {
+    public function mentionsAdd($iId, $s) 
+    {
         // TODO:        
     }
 
     /**
      * Add links to the @mentions in the string (actual tranformation may have to be performed with ready links like <a data-mention="bx_persons|123">mention name</a>)
+     * @param $iId content id
      * @param $s string with @mentions
      * @return modified string where all @mentions are transformed to links
      */
-    public function mentionsParse($s) {
+    public function mentionsParse($iId, $s) 
+    {
         // TODO:
     }
 
     /**
      * No mentions meta info in the head section
      */
-    protected function mentionsAddMeta() {
+    protected function mentionsAddMeta($iId) 
+    {
         return 0;
     }
 
 
 
-
     /**
      * Add thumb for the content
+     * @param $iId content id
      * @param $sStorageObject storage object name where file is stored
      * @param $mixedFileId file id in the storage
      * @return true if file was added, or false otherwise
      */
-    public function picturesAdd($sStorageObject, $mixedFileId) {
+    public function picturesAdd($iId, $sStorageObject, $mixedFileId) 
+    {
         // TODO:        
     }
 
     /**
      * Add thumb meta info to the head section
+     * @param $iId content id
      */
-    protected function picturesAddMeta() {
+    protected function picturesAddMeta($iId) 
+    {
         // TODO:
     }
 
+
+
+    /**
+     * Delete all data associated with the content
+     * @param $iId content id
+     */
+    public function onDeleteContent($iId) 
+    {
+        // TODO: 
+    }
+
 }
 
 /** @} */

+ 31 - 0
inc/classes/BxDolMetatagsQuery.php

@@ -34,6 +34,37 @@ class BxDolMetatagsQuery extends BxDolDb
         return $aObject;
     }
 
+
+
+    public function keywordsAdd($mixedContentId, $aKeywords)
+    {
+        $this->keywordsDelete($mixedContentId);
+        $i = 0;
+        foreach ($aKeywords as $sKeyword) {
+            $sQuery = $this->prepare("INSERT INTO `{$this->_aObject['table_keywords']}` SET `object_id` = ?, `keyword` = ?", $mixedContentId, trim($sKeyword, '#'));
+            $i += ($this->query($sQuery) ? 1 : 0);
+        }
+        return $i;
+    }
+
+    public function keywordsDelete($mixedContentId)
+    {
+        return $this->metaDelete($this->_aObject['table_keywords'], $mixedContentId);
+    }
+
+    public function keywordsGet($mixedContentId)
+    {
+        $sQuery = $this->prepare("SELECT `keyword` FROM `{$this->_aObject['table_keywords']}` WHERE `object_id` = ?", $mixedContentId);
+        return $this->getColumn($sQuery);
+    }
+
+
+
+    protected function metaDelete($sTable, $mixedContentId)
+    {
+        $sQuery = $this->prepare("DELETE FROM `{$sTable}` WHERE `object_id` = ?", $mixedContentId);
+        return $this->query($sQuery);
+    }
 }
 
 /** @} */

+ 11 - 0
modules/base/text/classes/BxBaseModTextFormsEntryHelper.php

@@ -100,6 +100,11 @@ class BxBaseModTextFormsEntryHelper extends BxBaseModGeneralFormsEntryHelper
         // change profile to 'pending' only if profile is 'active'
         if ($oProfile->isActive() && !empty($aTrackTextFieldsChanges['changed_fields']))
             $oProfile->disapprove(BX_PROFILE_ACTION_AUTO);
+        if (!empty($CNF['OBJECT_METATAGS'])) { // && isset($aTrackTextFieldsChanges['changed_fields'][$CNF['FIELD_TEXT']])) { // TODO: check if aTrackTextFieldsChanges works 
+            bx_import('BxDolMetatags');
+            $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
+            $oMetatags->keywordsAdd($aContentInfo[$CNF['FIELD_ID']], $aContentInfo[$CNF['FIELD_TEXT']]);
+        }
 
         // create an alert
         bx_import('BxDolPrivacy');
@@ -115,6 +120,12 @@ class BxBaseModTextFormsEntryHelper extends BxBaseModGeneralFormsEntryHelper
         if (!($aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId)))
             return MsgBox(_t('_sys_txt_error_occured'));
 
+        if (!empty($CNF['OBJECT_METATAGS'])) {
+            bx_import('BxDolMetatags');
+            $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
+            $oMetatags->keywordsAdd($aContentInfo[$CNF['FIELD_ID']], $aContentInfo[$CNF['FIELD_TEXT']]);
+        }
+
         // alert
         bx_import('BxDolPrivacy');
         $aParams = isset($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) ? array('privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) : array();

+ 6 - 0
modules/base/text/classes/BxBaseModTextTemplate.php

@@ -99,6 +99,12 @@ class BxBaseModTextTemplate extends BxBaseModGeneralTemplate
         $aVars['entry_title'] = isset($aData[$CNF['FIELD_TITLE']]) ? $aData[$CNF['FIELD_TITLE']] : '';
         $aVars['entry_text'] = $aData[$CNF['FIELD_TEXT']];
 
+        if (!empty($CNF['OBJECT_METATAGS'])) {
+            bx_import('BxDolMetatags');
+            $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
+            $aVars['text'] = $oMetatags->keywordsParse($aData[$CNF['FIELD_ID']], $aVars['text']);
+        }
+
         return $this->parseHtmlByName($sTemplateName, $aVars);
     }
 

+ 1 - 0
modules/boonex/notes/classes/BxNotesConfig.php

@@ -52,6 +52,7 @@ class BxNotesConfig extends BxBaseModTextConfig
             'OBJECT_IMAGES_TRANSCODER_PREVIEW' => 'bx_notes_preview',
             'OBJECT_VIEWS' => 'bx_notes',
             'OBJECT_VOTES' => 'bx_notes',
+            'OBJECT_METATAGS' => 'bx_notes',
             'OBJECT_COMMENTS' => 'bx_notes',
             'OBJECT_PRIVACY_VIEW' => 'bx_notes_allow_view_to',
             'OBJECT_FORM_ENTRY' => 'bx_notes',

+ 1 - 1
modules/boonex/notes/classes/BxNotesSearchResult.php

@@ -87,7 +87,7 @@ class BxNotesSearchResult extends BxBaseModTextSearchResult
                 $this->isError = true;
         }
 
-        // add replacable markers and replace them
+        // add replaceable markers and replace them
         if ($oProfileAuthor) {
             $this->addMarkers($oProfileAuthor->getInfo()); // profile info is replacable
             $this->addMarkers(array('profile_id' => $oProfileAuthor->id())); // profile id is replacable

+ 3 - 0
modules/boonex/notes/install/sql/disable.sql

@@ -25,3 +25,6 @@ DELETE FROM `sys_acl_actions` WHERE `Module` = 'bx_notes';
 -- SEARCH
 DELETE FROM `sys_objects_search` WHERE `ObjectName` = 'bx_notes';
 
+-- METATAGS
+DELETE FROM `sys_objects_metatags` WHERE `object` = 'bx_notes';
+

+ 4 - 0
modules/boonex/notes/install/sql/enable.sql

@@ -245,3 +245,7 @@ INSERT INTO `sys_acl_matrix` (`IDLevel`, `IDAction`) VALUES
 INSERT INTO `sys_objects_search` (`ObjectName`, `Title`, `ClassName`, `ClassPath`) VALUES
 ('bx_notes', '_bx_notes', 'BxNotesSearchResult', 'modules/boonex/notes/classes/BxNotesSearchResult.php');
 
+-- METATAGS
+INSERT INTO `sys_objects_metatags` (`object`, `table_keywords`, `table_locations`, `table_mentions`, `table_pictures`, `override_class_name`, `override_class_file`) VALUES
+('bx_notes', 'bx_notes_meta_keywords', '', '', '', '', '');
+

+ 10 - 0
modules/boonex/notes/install/sql/install.sql

@@ -99,6 +99,16 @@ CREATE TABLE `bx_notes_views_track` (
   KEY `id` (`object_id`,`viewer_id`,`viewer_nip`)
 ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
 
+-- TABLE: metas
+
+CREATE TABLE `bx_notes_meta_keywords` (
+  `object_id` int(10) unsigned NOT NULL,
+  `keyword` varchar(255) NOT NULL,
+  KEY `object_id` (`object_id`),
+  KEY `keyword` (`keyword`)
+) ENGINE=MyISAM DEFAULT CHARSET=utf8;
+
+
 -- STORAGES & TRANSCODERS
 
 INSERT INTO `sys_objects_storage` (`object`, `engine`, `params`, `token_life`, `cache_control`, `levels`, `table_files`, `ext_mode`, `ext_allow`, `ext_deny`, `quota_size`, `current_size`, `quota_number`, `current_number`, `max_file_size`, `ts`) VALUES

+ 1 - 1
modules/boonex/notes/install/sql/uninstall.sql

@@ -1,7 +1,7 @@
 
 -- TABLES
 
-DROP TABLE IF EXISTS `bx_notes_posts`, `bx_notes_files`, `bx_notes_photos_resized`, `bx_notes_cmts`, `bx_notes_votes`, `bx_notes_votes_track`, `bx_notes_views_track`;
+DROP TABLE IF EXISTS `bx_notes_posts`, `bx_notes_files`, `bx_notes_photos_resized`, `bx_notes_cmts`, `bx_notes_votes`, `bx_notes_votes_track`, `bx_notes_views_track`, `bx_notes_meta_keywords`;
 
 -- STORAGES & TRANSCODERS