| 1 |  |  | <?php | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  *@author: nicolaas[at]sunnysideup.co.nz | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  *@description: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * a log history and counted history of searches done (e.g. 100 people searched for "sunshine") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * it allows gives the opportunity to link zero or more pages to a particular search phrase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  **/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class SearchHistory extends DataObject | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     private static $db = array( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         "Title" => "Varchar(255)", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         "RedirectTo" => "Varchar(255)" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private static $has_many = array( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         "LogEntries" => "SearchHistoryLog" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private static $many_many = array( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         "Recommendations" => "SiteTree" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private static $singular_name = 'Search History Phrase'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private static $plural_name = 'Search History Phrases'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     private static $default_sort = 'Title'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private static $separator = " | "; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     private static $minimum_length = 3; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     private static $number_of_keyword_repeats = 3; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     private static $searchable_fields = array( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         "Title", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         "RedirectTo" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     private static $summary_fields = array( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         "Title", "RedirectTo" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     private static $field_labels = array( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         "Title" => "Phrase Searched For", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         "RedirectTo" => "Redirect To Search Phrase (if any)", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         "Recommendations" => "Recommended Pages - must already be part of the natural result set", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     public static function add_entry($KeywordString) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         if ($parent = self::find_entry($KeywordString)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             //do nothing | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             $parent = new SearchHistory(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $parent->Title = $KeywordString; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             $parent->write(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         if ($parent) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             $obj = new SearchHistoryLog(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             $obj->SearchedForID = $parent->ID; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             $obj->write(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             return $parent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |     public static function find_entry($KeywordString) | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         $KeywordString = self::clean_keywordstring($KeywordString); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         return SearchHistory::get()->filter(array("Title" => $KeywordString))->first(); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     public static function clean_keywordstring($KeywordString) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         Convert::raw2sql($KeywordString); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $KeywordString = trim(eregi_replace(" +", " ", $KeywordString)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         return $KeywordString; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     public function getCMSFields() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $fields = parent::getCMSFields(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $fields->removeByName("Title"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $fields->addFieldToTab("Root.Main", new HeaderField($name = "TitleHeader", "search for: '".$this->Title."'", 1), "RedirectTo"); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $fields->removeByName("Recommendations"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         if (!$this->RedirectTo) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             $source = SiteTree::get() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 ->filter(array("ShowInSearch" => 1)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 ->exclude(array("ClassName" => "SearchPlusPage")); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             $sourceArray = $sourc->map()->toArray(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             //$fields->addFieldToTab("Root.Main", new MultiSelectField($name = "Recommendations", $title = "Recommendations", $sourceArray)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             $fields->addFieldToTab("Root.Main", new TreeMultiselectField($name = "Recommendations", $title = "Recommendations", "SiteTree")); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             $fields->addFieldToTab("Root.Main", new LiteralField($name = "Recommendations", '<p>This search phrase cannot have recommendations, because it redirects to <i>'.$this->RedirectTo.'</i></p>')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $page = SearchPlusPage::get()->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         if (!$page) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             user_error("Make sure to create a SearchPlusPage to make proper use of this module", E_USER_NOTICE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             $fields->addFieldToTab("Root.Main", new LiteralField( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                 $name = "BackLinks", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                 $content = | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                     '<p>
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 						Review a graph of all <a href="'.$page->Link().'popularsearchwords/100/10/">Popular Search Phrases</a> OR
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 						<a href="'.$page->Link().'results/?Search='.urlencode($this->Title).'&action_results=Search&redirect=1">try this search</a>.
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 					</p>' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         return $fields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     public function onAfterWrite() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $bt = defined('DB::USE_ANSI_SQL') ? "\"" : "`"; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         parent::onAfterWrite(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         //add recommendations that are not actually matching | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $combos = $this->Recommendations(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         if ($combos) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             $idArray = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             foreach ($combos as $combo) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                 $idArray[$combo->SiteTreeID] = $combo->SiteTreeID; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             if (count($idArray)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                 $pages = SiteTree::get() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                     ->filter(array("ID" => $idArray)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                 if ($pages->count()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                     foreach ($pages as $page) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                         $changed = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                         $title = Config::inst()->get("SearchHistory", "seperator").$this->getTitle(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                         $multipliedTitle = Config::inst()->get("SearchHistory", "seperator").str_repeat($this->getTitle(), Config::inst()->get("SearchHistory", "number_of_keyword_repeats")); | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 135 |  | View Code Duplication |                         if (stripos($page->MetaKeywords." ", $multipliedTitle) === false) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                             $page->MetaKeywords. $multipliedTitle; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                             $changed = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |                         } | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 139 |  | View Code Duplication |                         if (stripos($page->MetaKeywords." ", $multipliedTitle) === false) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |                             $page->MetaKeywords = $page->MetaKeywords. $multipliedTitle; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                             $changed = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |                         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |                         if ($changed) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |                             $page->writeToStage('Stage'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |                             $page->Publish('Stage', 'Live'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |                             $page->Status = "Published"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |                         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         //delete useless ones | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         if (strlen($this->Title) < Config::inst()->get("SearchHistory", "minimum_length")) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |             $this->delete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     public function requireDefaultRecords() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         parent::requireDefaultRecords(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         $dos = SearchHistory::get() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |             ->where("\"Title\" = '' OR \"Title\" IS NULL OR LENGTH(\"Title\") < ".Config::inst()->get("SearchHistory", "minimum_length")); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         if ($dos) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |             foreach ($dos as $do) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |                 DB::alteration_message("deleting #".$do->ID." from SearchHistory as it does not have a search phrase", "deleted"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |                 $do->delete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | class SearchHistoryLog extends DataObject | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     private static $has_one = array( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         "SearchedFor" => "SearchHistory" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     private static $singular_name = 'Search History Log Entry'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |     private static $plural_name = 'Search History Log Entries'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |     private static $default_sort = 'Created DESC'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 185 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 186 |  |  |  |