| | 1 | <?php |
| | 2 | /* |
| | 3 | |
| | 4 | Copyright (c) 2010 Euranova |
| | 5 | |
| | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| | 7 | of this software and associated documentation files (the "Software"), to deal |
| | 8 | in the Software without restriction, including without limitation the rights |
| | 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| | 10 | copies of the Software, and to permit persons to whom the Software is |
| | 11 | furnished to do so, subject to the following conditions: |
| | 12 | |
| | 13 | The above copyright notice and this permission notice shall be included in |
| | 14 | all copies or substantial portions of the Software. |
| | 15 | |
| | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| | 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| | 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| | 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| | 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| | 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| | 22 | THE SOFTWARE. |
| | 23 | */ |
| | 24 | |
| | 25 | /** |
| | 26 | * rebuildslugsTask |
| | 27 | * |
| | 28 | * Generate slugs from apostrophe tree. |
| | 29 | * |
| | 30 | * @author Jehan Bruggeman <jehanbruggeman at gmail dot com> |
| | 31 | * |
| | 32 | */ |
| | 33 | |
| | 34 | class rebuildslugsTask extends sfBaseTask |
| | 35 | { |
| | 36 | protected function configure() |
| | 37 | { |
| | 38 | |
| | 39 | $this->addOptions(array( |
| | 40 | new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name'), |
| | 41 | new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'), |
| | 42 | new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'), |
| | 43 | )); |
| | 44 | |
| | 45 | $this->namespace = 'apostrophe'; |
| | 46 | $this->name = 'rebuild-slugs'; |
| | 47 | $this->briefDescription = 'Rebuild the slugs to reflect the current tree'; |
| | 48 | $this->detailedDescription = <<<EOF |
| | 49 | The [apostrohe:rebuild-slugs|INFO] task updates the slugs of your pages to reflect the Doctrine |
| | 50 | nested set tree of your site. |
| | 51 | |
| | 52 | Call it with: |
| | 53 | |
| | 54 | [php symfony a:rebuild-slugs|INFO] |
| | 55 | EOF; |
| | 56 | } |
| | 57 | |
| | 58 | protected function execute($arguments = array(), $options = array()) |
| | 59 | { |
| | 60 | // retreive pages |
| | 61 | $databaseManager = new sfDatabaseManager($this->configuration); |
| | 62 | $connection = $databaseManager->getDatabase($options['connection'] ? $options['connection'] : null)->getConnection(); |
| | 63 | $root = aPageTable::retrieveBySlugWithTitles('/'); |
| | 64 | $tree = $root->getTreeInfo(false); |
| | 65 | |
| | 66 | // launch the rebuild |
| | 67 | $this->cleanChildren($tree, '/'); |
| | 68 | } |
| | 69 | |
| | 70 | private function cleanChildren($tree,$currentPath){ |
| | 71 | foreach($tree as $p) |
| | 72 | { |
| | 73 | // Retreive page object |
| | 74 | $page = Doctrine_Query::Create()-> |
| | 75 | select("p.*")-> |
| | 76 | from("aPage p")->where('p.id = ?', array($p['id']))-> |
| | 77 | fetchOne(); |
| | 78 | |
| | 79 | // New slug based on the page title |
| | 80 | // Source : BaseaActions.class.php line 205 and 247 (in BaseaActions::executeCreate) |
| | 81 | $baseSlug = strtolower(preg_replace("/[\W]+/", "-", htmlspecialchars_decode($page->title))); |
| | 82 | |
| | 83 | // children |
| | 84 | if($page->hasChildren()){ |
| | 85 | $this->cleanChildren($p['children'],$currentPath.$baseSlug.'/'); |
| | 86 | } |
| | 87 | |
| | 88 | // save current page's slug |
| | 89 | $page->slug = $currentPath.$baseSlug; |
| | 90 | $page->save(); |
| | 91 | } |
| | 92 | } |
| | 93 | } |
| | 94 | |
| | 95 | |
| | 96 | |