@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | if (!class_exists('GridFieldExportAllAutomatedLinksButton')) { |
| 3 | - class GridFieldExportAllAutomatedLinksButton extends GridFieldExportButton { |
|
| 3 | + class GridFieldExportAllAutomatedLinksButton extends GridFieldExportButton { |
|
| 4 | 4 | /** |
| 5 | 5 | * Generate export fields for CSV. |
| 6 | 6 | * |
@@ -14,44 +14,44 @@ discard block |
||
| 14 | 14 | * |
| 15 | 15 | */ |
| 16 | 16 | public function generateExportFileData($gridField) { |
| 17 | - $separator = $this->csvSeparator; |
|
| 18 | - $csvColumns =$gridField->getColumns(); |
|
| 19 | - $fileData = ''; |
|
| 20 | - $columnData = array(); |
|
| 21 | - $fieldItems = new ArrayList(); |
|
| 22 | - if($this->csvHasHeader) { |
|
| 23 | - $headers = array(); |
|
| 17 | + $separator = $this->csvSeparator; |
|
| 18 | + $csvColumns =$gridField->getColumns(); |
|
| 19 | + $fileData = ''; |
|
| 20 | + $columnData = array(); |
|
| 21 | + $fieldItems = new ArrayList(); |
|
| 22 | + if($this->csvHasHeader) { |
|
| 23 | + $headers = array(); |
|
| 24 | 24 | // determine the CSV headers. If a field is callable (e.g. anonymous function) then use the |
| 25 | 25 | // source name as the header instead |
| 26 | - foreach($csvColumns as $columnSource => $columnHeader) { |
|
| 27 | - $headers[] = (!is_string($columnHeader) && is_callable($columnHeader)) ? $columnSource : $columnHeader; |
|
| 28 | - } |
|
| 29 | - $fileData .= "\"" . implode("\"{$separator}\"", array_values($headers)) . "\""; |
|
| 30 | - $fileData .= "\n"; |
|
| 31 | - } |
|
| 26 | + foreach($csvColumns as $columnSource => $columnHeader) { |
|
| 27 | + $headers[] = (!is_string($columnHeader) && is_callable($columnHeader)) ? $columnSource : $columnHeader; |
|
| 28 | + } |
|
| 29 | + $fileData .= "\"" . implode("\"{$separator}\"", array_values($headers)) . "\""; |
|
| 30 | + $fileData .= "\n"; |
|
| 31 | + } |
|
| 32 | 32 | // ----------------------- |
| 33 | 33 | //$items = $gridField->getManipulatedList(); |
| 34 | 34 | |
| 35 | - $items = $gridField->getList(); |
|
| 35 | + $items = $gridField->getList(); |
|
| 36 | 36 | // ----------------------- |
| 37 | 37 | // @todo should GridFieldComponents change behaviour based on whether others are available in the config? |
| 38 | - foreach($gridField->getConfig()->getComponents() as $component){ |
|
| 39 | - if($component instanceof GridFieldFilterHeader || $component instanceof GridFieldSortableHeader) { |
|
| 40 | - $items = $component->getManipulatedData($gridField, $items); |
|
| 41 | - } |
|
| 42 | - } |
|
| 43 | - foreach($items->limit(null) as $item) { |
|
| 44 | - $columnData = array(); |
|
| 45 | - foreach($csvColumns as $columnSource => $columnHeader) { |
|
| 46 | - $value = ( $item->hasMethod( $columnHeader ) ) ? $item->$columnHeader() : $item->$columnHeader; |
|
| 47 | - $value = str_replace(array("\r", "\n"), "\n", $value); |
|
| 48 | - $columnData[] = '"' . str_replace('"', '\"', $value) . '"'; |
|
| 49 | - } |
|
| 50 | - $fileData .= implode($separator, $columnData); |
|
| 51 | - $fileData .= "\n"; |
|
| 52 | - $item->destroy(); |
|
| 53 | - } |
|
| 54 | - return $fileData; |
|
| 38 | + foreach($gridField->getConfig()->getComponents() as $component){ |
|
| 39 | + if($component instanceof GridFieldFilterHeader || $component instanceof GridFieldSortableHeader) { |
|
| 40 | + $items = $component->getManipulatedData($gridField, $items); |
|
| 41 | + } |
|
| 42 | + } |
|
| 43 | + foreach($items->limit(null) as $item) { |
|
| 44 | + $columnData = array(); |
|
| 45 | + foreach($csvColumns as $columnSource => $columnHeader) { |
|
| 46 | + $value = ( $item->hasMethod( $columnHeader ) ) ? $item->$columnHeader() : $item->$columnHeader; |
|
| 47 | + $value = str_replace(array("\r", "\n"), "\n", $value); |
|
| 48 | + $columnData[] = '"' . str_replace('"', '\"', $value) . '"'; |
|
| 49 | + } |
|
| 50 | + $fileData .= implode($separator, $columnData); |
|
| 51 | + $fileData .= "\n"; |
|
| 52 | + $item->destroy(); |
|
| 53 | + } |
|
| 54 | + return $fileData; |
|
| 55 | 55 | } |
| 56 | 56 | } // end class GridFieldExportAllButton |
| 57 | 57 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | if (!class_exists('GridFieldPrintAllAutomatedLinksButton')) { |
| 3 | - class GridFieldPrintAllAutomatedLinksButton extends GridFieldPrintButton { |
|
| 3 | + class GridFieldPrintAllAutomatedLinksButton extends GridFieldPrintButton { |
|
| 4 | 4 | /** |
| 5 | 5 | * Export core. |
| 6 | 6 | * |
@@ -13,42 +13,42 @@ discard block |
||
| 13 | 13 | * |
| 14 | 14 | */ |
| 15 | 15 | public function generatePrintData(GridField $gridField) { |
| 16 | - $printColumns = $this->getPrintColumnsForGridField($gridField); |
|
| 17 | - $header = null; |
|
| 18 | - if($this->printHasHeader) { |
|
| 19 | - $header = new ArrayList(); |
|
| 20 | - foreach($printColumns as $field => $label){ |
|
| 21 | - $header->push(new ArrayData(array( |
|
| 22 | - "CellString" => $label, |
|
| 23 | - ))); |
|
| 24 | - } |
|
| 25 | - } |
|
| 16 | + $printColumns = $this->getPrintColumnsForGridField($gridField); |
|
| 17 | + $header = null; |
|
| 18 | + if($this->printHasHeader) { |
|
| 19 | + $header = new ArrayList(); |
|
| 20 | + foreach($printColumns as $field => $label){ |
|
| 21 | + $header->push(new ArrayData(array( |
|
| 22 | + "CellString" => $label, |
|
| 23 | + ))); |
|
| 24 | + } |
|
| 25 | + } |
|
| 26 | 26 | // ----------------------- |
| 27 | 27 | //$items = $gridField->getManipulatedList(); |
| 28 | - $items = $gridField->getList(); |
|
| 28 | + $items = $gridField->getList(); |
|
| 29 | 29 | // ----------------------- |
| 30 | - $itemRows = new ArrayList(); |
|
| 31 | - foreach($items as $item) { |
|
| 32 | - $itemRow = new ArrayList(); |
|
| 33 | - foreach($printColumns as $field => $label) { |
|
| 34 | - $value = $gridField->getDataFieldValue($item, $field); |
|
| 35 | - $itemRow->push(new ArrayData(array( |
|
| 36 | - "CellString" => $value, |
|
| 37 | - ))); |
|
| 38 | - } |
|
| 39 | - $itemRows->push(new ArrayData(array( |
|
| 40 | - "ItemRow" => $itemRow |
|
| 41 | - ))); |
|
| 42 | - $item->destroy(); |
|
| 43 | - } |
|
| 44 | - $ret = new ArrayData(array( |
|
| 45 | - "Title" => $this->getTitle($gridField), |
|
| 46 | - "Header" => $header, |
|
| 47 | - "ItemRows" => $itemRows, |
|
| 48 | - "Datetime" => SS_Datetime::now(), |
|
| 49 | - "Member" => Member::currentUser(), |
|
| 50 | - )); |
|
| 51 | - return $ret; |
|
| 30 | + $itemRows = new ArrayList(); |
|
| 31 | + foreach($items as $item) { |
|
| 32 | + $itemRow = new ArrayList(); |
|
| 33 | + foreach($printColumns as $field => $label) { |
|
| 34 | + $value = $gridField->getDataFieldValue($item, $field); |
|
| 35 | + $itemRow->push(new ArrayData(array( |
|
| 36 | + "CellString" => $value, |
|
| 37 | + ))); |
|
| 38 | + } |
|
| 39 | + $itemRows->push(new ArrayData(array( |
|
| 40 | + "ItemRow" => $itemRow |
|
| 41 | + ))); |
|
| 42 | + $item->destroy(); |
|
| 43 | + } |
|
| 44 | + $ret = new ArrayData(array( |
|
| 45 | + "Title" => $this->getTitle($gridField), |
|
| 46 | + "Header" => $header, |
|
| 47 | + "ItemRows" => $itemRows, |
|
| 48 | + "Datetime" => SS_Datetime::now(), |
|
| 49 | + "Member" => Member::currentUser(), |
|
| 50 | + )); |
|
| 51 | + return $ret; |
|
| 52 | 52 | } |
| 53 | 53 | } // end class GridFieldPrintAllButton |
| 54 | 54 | } |
@@ -11,20 +11,20 @@ discard block |
||
| 11 | 11 | */ |
| 12 | 12 | class GlobalAutoLinkSettings extends DataObject{ |
| 13 | 13 | |
| 14 | - public static $enabled = true; |
|
| 14 | + public static $enabled = true; |
|
| 15 | 15 | public static $encoding = 'UTF-8'; |
| 16 | 16 | |
| 17 | - private static $db = array( |
|
| 18 | - 'MaxLinksPerPage' => 'INT', |
|
| 19 | - 'ExcludeTags' => 'VARCHAR(255)', |
|
| 20 | - 'IncludeIn' => 'Text', |
|
| 21 | - 'AddTo' => 'Text', |
|
| 17 | + private static $db = array( |
|
| 18 | + 'MaxLinksPerPage' => 'INT', |
|
| 19 | + 'ExcludeTags' => 'VARCHAR(255)', |
|
| 20 | + 'IncludeIn' => 'Text', |
|
| 21 | + 'AddTo' => 'Text', |
|
| 22 | 22 | 'CrawlID' => 'VARCHAR(15)' |
| 23 | - ); |
|
| 23 | + ); |
|
| 24 | 24 | |
| 25 | - private static $defaults = array( |
|
| 26 | - 'IncludeIn' => 'Content' |
|
| 27 | - ); |
|
| 25 | + private static $defaults = array( |
|
| 26 | + 'IncludeIn' => 'Content' |
|
| 27 | + ); |
|
| 28 | 28 | |
| 29 | 29 | public static $default_create_config = array( |
| 30 | 30 | 'MaxLinksPerPage' => 0, |
@@ -32,13 +32,13 @@ discard block |
||
| 32 | 32 | 'IncludeIn' => 'Content' |
| 33 | 33 | ); |
| 34 | 34 | |
| 35 | - public function ExcludeTags(){ |
|
| 36 | - return array_unique( explode( ',', str_replace( ' ', '', $this->ExcludeTags ).',a,img,iframe,video,object' ) ); |
|
| 37 | - } |
|
| 35 | + public function ExcludeTags(){ |
|
| 36 | + return array_unique( explode( ',', str_replace( ' ', '', $this->ExcludeTags ).',a,img,iframe,video,object' ) ); |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - public function IncludeInFields(){ |
|
| 40 | - return explode( ',', str_replace( ' ', '', $this->IncludeIn ) ); |
|
| 41 | - } |
|
| 39 | + public function IncludeInFields(){ |
|
| 40 | + return explode( ',', str_replace( ' ', '', $this->IncludeIn ) ); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | 43 | public function requireDefaultRecords() { |
| 44 | 44 | $hasData = self::get()->first(); |
@@ -76,7 +76,7 @@ discard block |
||
| 76 | 76 | * |
| 77 | 77 | * @return array |
| 78 | 78 | */ |
| 79 | - public function AllowedIn(){ |
|
| 79 | + public function AllowedIn(){ |
|
| 80 | 80 | $classes = array_values( ClassInfo::subclassesFor( 'SiteTree' ) ); |
| 81 | 81 | if( !$this->AddTo ) return $classes; |
| 82 | 82 | |
@@ -97,7 +97,7 @@ discard block |
||
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | return (array) $sanitized; |
| 100 | - } |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | 102 | /** |
| 103 | 103 | * Gets the current config |
@@ -11,18 +11,18 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class SEOToolboxAdmin extends ModelAdmin{ |
| 13 | 13 | |
| 14 | - private static $menu_title = 'SEO Tool box'; |
|
| 15 | - private static $url_segment = 'seo-tool-box'; |
|
| 16 | - private static $managed_models = array( 'AutomatedLink' ); |
|
| 14 | + private static $menu_title = 'SEO Tool box'; |
|
| 15 | + private static $url_segment = 'seo-tool-box'; |
|
| 16 | + private static $managed_models = array( 'AutomatedLink' ); |
|
| 17 | 17 | private static $menu_priority = 10; |
| 18 | 18 | |
| 19 | 19 | |
| 20 | - public function getEditForm($id = null, $fields = null) { |
|
| 21 | - $form = parent::getEditForm( $id, $fields ); |
|
| 22 | - $class = $this->sanitiseClassName($this->modelClass); |
|
| 23 | - $grid = $form->Fields()->fieldByName($class); |
|
| 24 | - $grid->getConfig()->addComponent( new GridFieldSortableRows( 'Priority' ) ); |
|
| 20 | + public function getEditForm($id = null, $fields = null) { |
|
| 21 | + $form = parent::getEditForm( $id, $fields ); |
|
| 22 | + $class = $this->sanitiseClassName($this->modelClass); |
|
| 23 | + $grid = $form->Fields()->fieldByName($class); |
|
| 24 | + $grid->getConfig()->addComponent( new GridFieldSortableRows( 'Priority' ) ); |
|
| 25 | 25 | |
| 26 | - return $form; |
|
| 27 | - } |
|
| 26 | + return $form; |
|
| 27 | + } |
|
| 28 | 28 | } |
@@ -25,29 +25,29 @@ discard block |
||
| 25 | 25 | // Do something here |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | - private $maxLinksPerPage; |
|
| 28 | + private $maxLinksPerPage; |
|
| 29 | 29 | private $settings = null; |
| 30 | - private $linkCount = 0; |
|
| 30 | + private $linkCount = 0; |
|
| 31 | 31 | private $addLinks = true; |
| 32 | 32 | private $excludeTags = array(); |
| 33 | 33 | private $maxLinks = 0; |
| 34 | 34 | |
| 35 | - public function index(){ |
|
| 35 | + public function index(){ |
|
| 36 | 36 | $this->addAutomatedLinks(); |
| 37 | 37 | |
| 38 | - // If we have a crawl request check the CrawlID so we're sure we didn't hit another SS site running our module |
|
| 39 | - if( $crawl_id = $this->owner->request->getHeader('X-Crawl-Id') ){ |
|
| 40 | - return( $crawl_id == GlobalAutoLinkSettings::get_current()->CrawlID ) |
|
| 38 | + // If we have a crawl request check the CrawlID so we're sure we didn't hit another SS site running our module |
|
| 39 | + if( $crawl_id = $this->owner->request->getHeader('X-Crawl-Id') ){ |
|
| 40 | + return( $crawl_id == GlobalAutoLinkSettings::get_current()->CrawlID ) |
|
| 41 | 41 | ? $this->crawl_response() |
| 42 | 42 | : $this->owner->redirect(SEOTestSiteTreeController::getPermissionDeniedPage()->Link()); |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | - return array(); |
|
| 46 | - } |
|
| 45 | + return array(); |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - private function crawl_response(){ |
|
| 48 | + private function crawl_response(){ |
|
| 49 | 49 | // Encoded version to detect which fields are being used |
| 50 | - $customize = array(); |
|
| 50 | + $customize = array(); |
|
| 51 | 51 | foreach( Config::inst()->get($this->owner->ClassName, 'db') as $field => $type ){ |
| 52 | 52 | if( strtolower( $type ) == 'htmltext' ){ |
| 53 | 53 | $data = ($this->owner->hasMethod($field)) ? $this->owner->$field() : $this->owner->$field; |
@@ -72,7 +72,7 @@ discard block |
||
| 72 | 72 | '/<!--(.*?)-->/im', |
| 73 | 73 | ), '', $this->owner->customise($customize)->render()) |
| 74 | 74 | ); |
| 75 | - } |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | 77 | /** |
| 78 | 78 | * Get the global settings and check if we should be adding |
@@ -94,13 +94,13 @@ discard block |
||
| 94 | 94 | return $this->settings; |
| 95 | 95 | } |
| 96 | 96 | |
| 97 | - /** |
|
| 98 | - * Goes through all the automated link settings and adds |
|
| 99 | - * the links where necessary |
|
| 100 | - * |
|
| 101 | - * @return void |
|
| 102 | - */ |
|
| 103 | - public function addAutomatedLinks(){ |
|
| 97 | + /** |
|
| 98 | + * Goes through all the automated link settings and adds |
|
| 99 | + * the links where necessary |
|
| 100 | + * |
|
| 101 | + * @return void |
|
| 102 | + */ |
|
| 103 | + public function addAutomatedLinks(){ |
|
| 104 | 104 | if( GlobalAutoLinkSettings::$enabled && $this->owner->class != 'RedirectorPage' ) { |
| 105 | 105 | $this->getSettings(); |
| 106 | 106 | if( !$this->addLinks ) return; |
@@ -132,60 +132,60 @@ discard block |
||
| 132 | 132 | } |
| 133 | 133 | } |
| 134 | 134 | } |
| 135 | - } |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | - /** |
|
| 138 | - * Parse the provided field and add the necessary links |
|
| 139 | - * |
|
| 140 | - * @param DOMDocument $html |
|
| 137 | + /** |
|
| 138 | + * Parse the provided field and add the necessary links |
|
| 139 | + * |
|
| 140 | + * @param DOMDocument $html |
|
| 141 | 141 | * @param String $field |
| 142 | - * @return string |
|
| 143 | - */ |
|
| 144 | - private function parseField( DOMDocument $html, $field ){ |
|
| 145 | - $this->owner->extend( 'beforeParseField', $html, $field ); |
|
| 146 | - |
|
| 147 | - // Remove Tags from Content we wown't be using |
|
| 148 | - $excluded = array(); |
|
| 149 | - foreach( $this->excludeTags as $eTag ){ |
|
| 150 | - while( $tags = $html->getElementsByTagName( $eTag ) ){ |
|
| 151 | - if( !$tags->length ) break 1; |
|
| 152 | - $tag = $tags->item(0); |
|
| 142 | + * @return string |
|
| 143 | + */ |
|
| 144 | + private function parseField( DOMDocument $html, $field ){ |
|
| 145 | + $this->owner->extend( 'beforeParseField', $html, $field ); |
|
| 146 | + |
|
| 147 | + // Remove Tags from Content we wown't be using |
|
| 148 | + $excluded = array(); |
|
| 149 | + foreach( $this->excludeTags as $eTag ){ |
|
| 150 | + while( $tags = $html->getElementsByTagName( $eTag ) ){ |
|
| 151 | + if( !$tags->length ) break 1; |
|
| 152 | + $tag = $tags->item(0); |
|
| 153 | 153 | $value = $html->saveHTML( $tag ); |
| 154 | 154 | $key = (string) crc32( $value ); |
| 155 | 155 | |
| 156 | - // Convert back children nodes of this node if they were already hashed |
|
| 157 | - $excluded[$key] = str_replace( array_keys( $excluded ), array_values( $excluded ), $value ); |
|
| 156 | + // Convert back children nodes of this node if they were already hashed |
|
| 157 | + $excluded[$key] = str_replace( array_keys( $excluded ), array_values( $excluded ), $value ); |
|
| 158 | 158 | |
| 159 | 159 | $tag->parentNode->replaceChild( $html->createTextNode( $key ), $tag ); |
| 160 | - } |
|
| 161 | - } |
|
| 160 | + } |
|
| 161 | + } |
|
| 162 | 162 | |
| 163 | 163 | $body = (string)$html->saveHTML( $html->getElementsByTagName('body')->item(0) ); |
| 164 | 164 | $content = preg_replace( array( '/\<body\>/is', '/\<\/body\>/is' ), '', $body, 1 ); |
| 165 | 165 | |
| 166 | - // Create the links |
|
| 167 | - $links = AutomatedLink::get()->sort('Priority'); |
|
| 168 | - foreach( $links as $link ){ |
|
| 169 | - // Check if self-linking is allowed and if current pagetype is allowed |
|
| 170 | - if( !$link->canBeAdded( $this->owner, $field ) ) continue; |
|
| 166 | + // Create the links |
|
| 167 | + $links = AutomatedLink::get()->sort('Priority'); |
|
| 168 | + foreach( $links as $link ){ |
|
| 169 | + // Check if self-linking is allowed and if current pagetype is allowed |
|
| 170 | + if( !$link->canBeAdded( $this->owner, $field ) ) continue; |
|
| 171 | 171 | |
| 172 | - $max = (int) ( $link->MaxLinksPerPage > 0 ) ? $link->MaxLinksPerPage : PHP_INT_MAX; |
|
| 172 | + $max = (int) ( $link->MaxLinksPerPage > 0 ) ? $link->MaxLinksPerPage : PHP_INT_MAX; |
|
| 173 | 173 | $escape = (string) preg_quote( $link->Phrase, '/' ); |
| 174 | - $regex = (string) ( $link->CaseSensitive ) ? "/(\b{$escape}\b)/" : "/(\b{$escape}\b)/i"; |
|
| 174 | + $regex = (string) ( $link->CaseSensitive ) ? "/(\b{$escape}\b)/" : "/(\b{$escape}\b)/i"; |
|
| 175 | 175 | |
| 176 | 176 | // Count the matches |
| 177 | 177 | preg_match_all( $regex, $content, $count ); |
| 178 | 178 | $count = ( is_array( $count ) && isset( $count[0] ) ) ? count( $count[0] ) : 0; |
| 179 | 179 | if( $count < 1 ) continue; |
| 180 | 180 | |
| 181 | - if( isset( $this->maxLinksPerPage[ $link->ID ] ) ) |
|
| 182 | - $max -= $this->maxLinksPerPage[ $link->ID ]; |
|
| 183 | - else |
|
| 184 | - $this->maxLinksPerPage[ $link->ID ] = 0; |
|
| 181 | + if( isset( $this->maxLinksPerPage[ $link->ID ] ) ) |
|
| 182 | + $max -= $this->maxLinksPerPage[ $link->ID ]; |
|
| 183 | + else |
|
| 184 | + $this->maxLinksPerPage[ $link->ID ] = 0; |
|
| 185 | 185 | |
| 186 | - for( $x = 0; $x < $count; $x++ ){ |
|
| 187 | - // Stop adding links if we reached the link or page limit |
|
| 188 | - if( $x >= $max || $this->linkCount >= $this->maxLinks ) break; |
|
| 186 | + for( $x = 0; $x < $count; $x++ ){ |
|
| 187 | + // Stop adding links if we reached the link or page limit |
|
| 188 | + if( $x >= $max || $this->linkCount >= $this->maxLinks ) break; |
|
| 189 | 189 | |
| 190 | 190 | // Check if there is anything else to replace else stop |
| 191 | 191 | preg_match( $regex, $content, $match ); |
@@ -195,18 +195,18 @@ discard block |
||
| 195 | 195 | $key = (string) crc32( $html ); |
| 196 | 196 | $excluded[ $key ] = (string) $html; |
| 197 | 197 | |
| 198 | - $content = preg_replace( $regex, $key, $content, 1 ); |
|
| 199 | - $this->linkCount++; |
|
| 198 | + $content = preg_replace( $regex, $key, $content, 1 ); |
|
| 199 | + $this->linkCount++; |
|
| 200 | 200 | $this->maxLinksPerPage[ $link->ID ]++; |
| 201 | 201 | } |
| 202 | 202 | |
| 203 | - // Stop Adding links if we reached the page limit |
|
| 204 | - if( $this->linkCount >= $this->maxLinks ) break; |
|
| 205 | - } |
|
| 203 | + // Stop Adding links if we reached the page limit |
|
| 204 | + if( $this->linkCount >= $this->maxLinks ) break; |
|
| 205 | + } |
|
| 206 | 206 | |
| 207 | - // Re-add the excluded Tags |
|
| 208 | - $content = str_replace( array_keys( $excluded ), array_values( $excluded ), $content ); |
|
| 207 | + // Re-add the excluded Tags |
|
| 208 | + $content = str_replace( array_keys( $excluded ), array_values( $excluded ), $content ); |
|
| 209 | 209 | |
| 210 | - return $content; |
|
| 211 | - } |
|
| 210 | + return $content; |
|
| 211 | + } |
|
| 212 | 212 | } |
@@ -9,45 +9,45 @@ discard block |
||
| 9 | 9 | */ |
| 10 | 10 | class AutomatedLink extends DataObject implements PermissionProvider{ |
| 11 | 11 | |
| 12 | - private static $db = array( |
|
| 13 | - 'Phrase' => 'VARCHAR(255)', |
|
| 14 | - 'TitleTag' => 'VARCHAR(255)', |
|
| 15 | - 'AnchorTag' => 'VARCHAR(255)', |
|
| 16 | - 'NewWindow' => 'Boolean', |
|
| 17 | - 'NoFollow' => 'Boolean', |
|
| 18 | - 'SelfLinking' => 'Boolean', |
|
| 19 | - 'CaseSensitive' => 'Boolean', |
|
| 20 | - 'MaxLinksPerPage' => 'INT', |
|
| 21 | - 'Priority' => 'Int' |
|
| 22 | - ); |
|
| 23 | - |
|
| 24 | - private static $defaults = array( |
|
| 25 | - 'MaxLinksPerPage' => 10 |
|
| 26 | - ); |
|
| 27 | - |
|
| 28 | - private static $default_sort = 'Priority'; |
|
| 29 | - |
|
| 30 | - private static $has_one = array( |
|
| 31 | - 'Page' => 'SiteTree' |
|
| 32 | - ); |
|
| 33 | - |
|
| 34 | - private static $summary_fields = array( 'Phrase', 'PointsTo' ); |
|
| 35 | - private static $searchable_fields = array( 'Phrase' ); |
|
| 36 | - private static $singular_name = 'Automated Link'; |
|
| 37 | - private static $plural_name = 'Automated Links'; |
|
| 12 | + private static $db = array( |
|
| 13 | + 'Phrase' => 'VARCHAR(255)', |
|
| 14 | + 'TitleTag' => 'VARCHAR(255)', |
|
| 15 | + 'AnchorTag' => 'VARCHAR(255)', |
|
| 16 | + 'NewWindow' => 'Boolean', |
|
| 17 | + 'NoFollow' => 'Boolean', |
|
| 18 | + 'SelfLinking' => 'Boolean', |
|
| 19 | + 'CaseSensitive' => 'Boolean', |
|
| 20 | + 'MaxLinksPerPage' => 'INT', |
|
| 21 | + 'Priority' => 'Int' |
|
| 22 | + ); |
|
| 23 | + |
|
| 24 | + private static $defaults = array( |
|
| 25 | + 'MaxLinksPerPage' => 10 |
|
| 26 | + ); |
|
| 27 | + |
|
| 28 | + private static $default_sort = 'Priority'; |
|
| 29 | + |
|
| 30 | + private static $has_one = array( |
|
| 31 | + 'Page' => 'SiteTree' |
|
| 32 | + ); |
|
| 33 | + |
|
| 34 | + private static $summary_fields = array( 'Phrase', 'PointsTo' ); |
|
| 35 | + private static $searchable_fields = array( 'Phrase' ); |
|
| 36 | + private static $singular_name = 'Automated Link'; |
|
| 37 | + private static $plural_name = 'Automated Links'; |
|
| 38 | 38 | private static $parsableFields = array(); |
| 39 | 39 | |
| 40 | 40 | public function PointsTo(){ |
| 41 | 41 | return $this->Page()->Link(); |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | - public function Title(){ |
|
| 45 | - return $this->Phrase; |
|
| 46 | - } |
|
| 44 | + public function Title(){ |
|
| 45 | + return $this->Phrase; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - public function forTemplate(){ |
|
| 49 | - return $this->getHTML(); |
|
| 50 | - } |
|
| 48 | + public function forTemplate(){ |
|
| 49 | + return $this->getHTML(); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | 52 | function canView( $member = false ){ |
| 53 | 53 | return Permission::check('AUTOMATEDLINK_VIEW'); |
@@ -66,129 +66,129 @@ discard block |
||
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | function providePermissions() { |
| 69 | - return array( |
|
| 70 | - 'AUTOMATEDLINK_VIEW' => 'View Automated Links', |
|
| 71 | - 'AUTOMATEDLINK_EDIT' => 'Edit Automated Links', |
|
| 72 | - 'AUTOMATEDLINK_DELETE' => 'Delete Automated Links', |
|
| 73 | - 'AUTOMATEDLINK_CREATE' => 'Create Automated Links', |
|
| 74 | - ); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - public function requireDefaultRecords(){ |
|
| 78 | - parent::requireDefaultRecords(); |
|
| 79 | - |
|
| 80 | - // Update all links to redirector pages during dev/build |
|
| 81 | - foreach( self::get() as $link ) $link->CheckAndUpdateDestination( true ); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Returns the HTML Representation of this object |
|
| 86 | - * |
|
| 69 | + return array( |
|
| 70 | + 'AUTOMATEDLINK_VIEW' => 'View Automated Links', |
|
| 71 | + 'AUTOMATEDLINK_EDIT' => 'Edit Automated Links', |
|
| 72 | + 'AUTOMATEDLINK_DELETE' => 'Delete Automated Links', |
|
| 73 | + 'AUTOMATEDLINK_CREATE' => 'Create Automated Links', |
|
| 74 | + ); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + public function requireDefaultRecords(){ |
|
| 78 | + parent::requireDefaultRecords(); |
|
| 79 | + |
|
| 80 | + // Update all links to redirector pages during dev/build |
|
| 81 | + foreach( self::get() as $link ) $link->CheckAndUpdateDestination( true ); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Returns the HTML Representation of this object |
|
| 86 | + * |
|
| 87 | 87 | * @param String $originalPhrase |
| 88 | - * @return String |
|
| 89 | - */ |
|
| 90 | - public function getHTML( $originalPhrase = NULL ){ |
|
| 91 | - $link = ( $this->PageID ) ? $this->Page()->Link() : '#'; |
|
| 92 | - $title = ( $this->TitleTag ) ? "title='{$this->TitleTag}'" : ''; |
|
| 93 | - $nofollow = ( $this->NoFollow ) ? 'rel="nofollow"' : ''; |
|
| 94 | - $newtab = ( $this->NewWindow ) ? 'target="_blank"' : ''; |
|
| 88 | + * @return String |
|
| 89 | + */ |
|
| 90 | + public function getHTML( $originalPhrase = NULL ){ |
|
| 91 | + $link = ( $this->PageID ) ? $this->Page()->Link() : '#'; |
|
| 92 | + $title = ( $this->TitleTag ) ? "title='{$this->TitleTag}'" : ''; |
|
| 93 | + $nofollow = ( $this->NoFollow ) ? 'rel="nofollow"' : ''; |
|
| 94 | + $newtab = ( $this->NewWindow ) ? 'target="_blank"' : ''; |
|
| 95 | 95 | $anchor = ( $originalPhrase ) ? $originalPhrase : $this->Phrase; |
| 96 | - $link = ( $this->AnchorTag ) ? rtrim( $link, '#' ).'#'.$this->AnchorTag : $link; |
|
| 97 | - return "<a href=\"$link\" $title $nofollow $newtab data-id=\"{$this->ID}\">{$anchor}</a>"; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - public function getCMSFields(){ |
|
| 101 | - $fields = FieldList::create( TabSet::create( 'Root' ) ); |
|
| 102 | - |
|
| 103 | - $fields->addFieldsToTab( 'Root.LinkSettings', array( |
|
| 104 | - TextField::create( 'Phrase', 'Phrase to search for', $this->Phrase, 255 ), |
|
| 105 | - TextField::create( 'TitleTag', 'Title Tag', $this->TitleTag, 255 ), |
|
| 106 | - TextField::create( 'AnchorTag', 'Anchor Tag(#)', $this->AnchorTag, 255 ), |
|
| 107 | - FieldGroup::create( |
|
| 108 | - CheckboxField::create( 'NoFollow' ), |
|
| 109 | - CheckboxField::create( 'NewWindow' ), |
|
| 110 | - CheckboxField::create( 'SelfLinking', 'Allow page to link to itself' ), |
|
| 111 | - CheckboxField::create( 'CaseSensitive', 'Match the case of the phrase' ) |
|
| 112 | - ), |
|
| 113 | - NumericField::create( 'MaxLinksPerPage', 'Maximum amount of this link to be created on a single page( 0 = unlimited )' ), |
|
| 114 | - TreeDropdownField::create( 'PageID', 'Page to link to', 'SiteTree' ) |
|
| 115 | - ) ); |
|
| 116 | - |
|
| 117 | - $settings = GlobalAutoLinkSettings::get_current(); |
|
| 118 | - if( $settings ){ |
|
| 119 | - $fields->addFieldsToTab( 'Root.Global', array( |
|
| 120 | - NumericField::create( |
|
| 121 | - 'Global_MaxLinksPerPage', |
|
| 122 | - 'Maximum amount of links a single page can have ( 0 = unlimited )', |
|
| 123 | - $settings->MaxLinksPerPage |
|
| 124 | - ), |
|
| 125 | - TextField::create( |
|
| 126 | - 'Global_ExcludeTags', |
|
| 127 | - 'Do not include links into these HTML Tags ( comma seperated )', |
|
| 128 | - $settings->ExcludeTags |
|
| 129 | - ), |
|
| 130 | - TextField::create( |
|
| 131 | - 'Global_AddTo', |
|
| 132 | - 'Page types where links should be created in ( leave blank for all page types )', |
|
| 133 | - $settings->AddTo ), |
|
| 134 | - TextField::create( |
|
| 135 | - 'Global_IncludeIn', |
|
| 136 | - 'Include Links into these fields ( comma seperated & field must support html injection )', |
|
| 137 | - $settings->IncludeIn |
|
| 138 | - ) |
|
| 139 | - ) ); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - return $fields; |
|
| 143 | - } |
|
| 96 | + $link = ( $this->AnchorTag ) ? rtrim( $link, '#' ).'#'.$this->AnchorTag : $link; |
|
| 97 | + return "<a href=\"$link\" $title $nofollow $newtab data-id=\"{$this->ID}\">{$anchor}</a>"; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + public function getCMSFields(){ |
|
| 101 | + $fields = FieldList::create( TabSet::create( 'Root' ) ); |
|
| 102 | + |
|
| 103 | + $fields->addFieldsToTab( 'Root.LinkSettings', array( |
|
| 104 | + TextField::create( 'Phrase', 'Phrase to search for', $this->Phrase, 255 ), |
|
| 105 | + TextField::create( 'TitleTag', 'Title Tag', $this->TitleTag, 255 ), |
|
| 106 | + TextField::create( 'AnchorTag', 'Anchor Tag(#)', $this->AnchorTag, 255 ), |
|
| 107 | + FieldGroup::create( |
|
| 108 | + CheckboxField::create( 'NoFollow' ), |
|
| 109 | + CheckboxField::create( 'NewWindow' ), |
|
| 110 | + CheckboxField::create( 'SelfLinking', 'Allow page to link to itself' ), |
|
| 111 | + CheckboxField::create( 'CaseSensitive', 'Match the case of the phrase' ) |
|
| 112 | + ), |
|
| 113 | + NumericField::create( 'MaxLinksPerPage', 'Maximum amount of this link to be created on a single page( 0 = unlimited )' ), |
|
| 114 | + TreeDropdownField::create( 'PageID', 'Page to link to', 'SiteTree' ) |
|
| 115 | + ) ); |
|
| 116 | + |
|
| 117 | + $settings = GlobalAutoLinkSettings::get_current(); |
|
| 118 | + if( $settings ){ |
|
| 119 | + $fields->addFieldsToTab( 'Root.Global', array( |
|
| 120 | + NumericField::create( |
|
| 121 | + 'Global_MaxLinksPerPage', |
|
| 122 | + 'Maximum amount of links a single page can have ( 0 = unlimited )', |
|
| 123 | + $settings->MaxLinksPerPage |
|
| 124 | + ), |
|
| 125 | + TextField::create( |
|
| 126 | + 'Global_ExcludeTags', |
|
| 127 | + 'Do not include links into these HTML Tags ( comma seperated )', |
|
| 128 | + $settings->ExcludeTags |
|
| 129 | + ), |
|
| 130 | + TextField::create( |
|
| 131 | + 'Global_AddTo', |
|
| 132 | + 'Page types where links should be created in ( leave blank for all page types )', |
|
| 133 | + $settings->AddTo ), |
|
| 134 | + TextField::create( |
|
| 135 | + 'Global_IncludeIn', |
|
| 136 | + 'Include Links into these fields ( comma seperated & field must support html injection )', |
|
| 137 | + $settings->IncludeIn |
|
| 138 | + ) |
|
| 139 | + ) ); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + return $fields; |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | 145 | public function getCMSValidator(){ |
| 146 | 146 | return new RequiredFields( array( 'Phrase', 'PageID' ) ); |
| 147 | 147 | } |
| 148 | 148 | |
| 149 | - /** |
|
| 150 | - * Save the Global Settings into the |
|
| 151 | - * Global Auto Link Settings Object |
|
| 152 | - * |
|
| 153 | - * @return void |
|
| 154 | - */ |
|
| 155 | - public function onBeforeWrite(){ |
|
| 156 | - parent::onBeforeWrite(); |
|
| 157 | - |
|
| 158 | - $settings = GlobalAutoLinkSettings::get_current(); |
|
| 159 | - if( $settings ){ |
|
| 160 | - |
|
| 161 | - foreach( $this->getChangedFields() as $field => $value ){ |
|
| 162 | - if( strpos( $field, 'Global_' ) === 0 && isset( $value['after'] ) ){ |
|
| 163 | - $field = str_replace( 'Global_', '', $field ); |
|
| 164 | - $settings->$field = $value['after']; |
|
| 165 | - } |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - $settings->write(); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - $this->CheckAndUpdateDestination(); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * Checks if the destination is a redirector page if so |
|
| 176 | - * it updates it to the destination of the redirector page |
|
| 177 | - * |
|
| 178 | - * @Boolean $write - Write the changes if any |
|
| 179 | - * @return void |
|
| 180 | - */ |
|
| 181 | - public function CheckAndUpdateDestination( $write = false ){ |
|
| 182 | - $this->extend('beforeCheckAndUpdateDestination', $write); |
|
| 183 | - |
|
| 184 | - if( $this->PageID && $this->Page() && |
|
| 185 | - $this->Page()->ClassName == 'RedirectorPage' && |
|
| 186 | - $this->Page()->LinkToID && $this->Page()->RedirectionType == 'Internal' ) |
|
| 187 | - { |
|
| 188 | - $this->PageID = $this->Page()->LinkToID; |
|
| 189 | - if( $write ) $this->write(); |
|
| 190 | - } |
|
| 191 | - } |
|
| 149 | + /** |
|
| 150 | + * Save the Global Settings into the |
|
| 151 | + * Global Auto Link Settings Object |
|
| 152 | + * |
|
| 153 | + * @return void |
|
| 154 | + */ |
|
| 155 | + public function onBeforeWrite(){ |
|
| 156 | + parent::onBeforeWrite(); |
|
| 157 | + |
|
| 158 | + $settings = GlobalAutoLinkSettings::get_current(); |
|
| 159 | + if( $settings ){ |
|
| 160 | + |
|
| 161 | + foreach( $this->getChangedFields() as $field => $value ){ |
|
| 162 | + if( strpos( $field, 'Global_' ) === 0 && isset( $value['after'] ) ){ |
|
| 163 | + $field = str_replace( 'Global_', '', $field ); |
|
| 164 | + $settings->$field = $value['after']; |
|
| 165 | + } |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + $settings->write(); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + $this->CheckAndUpdateDestination(); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * Checks if the destination is a redirector page if so |
|
| 176 | + * it updates it to the destination of the redirector page |
|
| 177 | + * |
|
| 178 | + * @Boolean $write - Write the changes if any |
|
| 179 | + * @return void |
|
| 180 | + */ |
|
| 181 | + public function CheckAndUpdateDestination( $write = false ){ |
|
| 182 | + $this->extend('beforeCheckAndUpdateDestination', $write); |
|
| 183 | + |
|
| 184 | + if( $this->PageID && $this->Page() && |
|
| 185 | + $this->Page()->ClassName == 'RedirectorPage' && |
|
| 186 | + $this->Page()->LinkToID && $this->Page()->RedirectionType == 'Internal' ) |
|
| 187 | + { |
|
| 188 | + $this->PageID = $this->Page()->LinkToID; |
|
| 189 | + if( $write ) $this->write(); |
|
| 190 | + } |
|
| 191 | + } |
|
| 192 | 192 | |
| 193 | 193 | /** |
| 194 | 194 | * Checks if the field is parable |
@@ -220,7 +220,7 @@ discard block |
||
| 220 | 220 | * |
| 221 | 221 | * @return Boolean |
| 222 | 222 | */ |
| 223 | - public function canBeAdded( ContentController $controller ){ |
|
| 223 | + public function canBeAdded( ContentController $controller ){ |
|
| 224 | 224 | return ( $this->SelfLinking || $controller->ID != $this->PageID ); |
| 225 | 225 | } |
| 226 | 226 | } |