|
| 1 | +<?php |
| 2 | +namespace Meta\Controller\Admin; |
| 3 | + |
| 4 | +use App\Controller\AppController; |
| 5 | +use Cake\Event\Event; |
| 6 | +use Cake\ORM\TableRegistry; |
| 7 | +use Cake\Core\Configure; |
| 8 | + |
| 9 | +class MetaController extends AppController |
| 10 | +{ |
| 11 | + public function index() |
| 12 | + { |
| 13 | + $meta = TableRegistry::get('Meta.Meta'); |
| 14 | + $this->set('data', $this->paginate($meta)); |
| 15 | + } |
| 16 | + |
| 17 | + public function add() |
| 18 | + { |
| 19 | + $metaTable = TableRegistry::get('Meta.Meta'); |
| 20 | + if (!empty($this->request->data)) { |
| 21 | + $data = $metaTable->newEntity($this->request->data()); |
| 22 | + if ($metaTable->save($data)) { |
| 23 | + $this->Flash->success('Meta record added.'); |
| 24 | + $this->redirect(['action' => 'index'], null, true); |
| 25 | + } else { |
| 26 | + $this->Flash->error('Error encountered while saving.'); |
| 27 | + } |
| 28 | + } |
| 29 | + $this->set('data', $metaTable->newEntity()); |
| 30 | + $this->render('edit'); |
| 31 | + } |
| 32 | + |
| 33 | + public function delete($id = null) |
| 34 | + { |
| 35 | + $metaTable = TableRegistry::get('Meta.Meta'); |
| 36 | + $data = $metaTable->get($id); |
| 37 | + if ($metaTable->delete($data)) { |
| 38 | + $this->Flash->success('Meta record deleted.'); |
| 39 | + } else { |
| 40 | + $this->Flash->error('Error encourntered while deleting.'); |
| 41 | + } |
| 42 | + $this->redirect(['action' => 'index'], null, true); |
| 43 | + } |
| 44 | + |
| 45 | + public function edit($id = null) |
| 46 | + { |
| 47 | + $metaTable = TableRegistry::get('Meta.Meta'); |
| 48 | + $data = $metaTable->get($id); |
| 49 | + |
| 50 | + if (!empty($this->request->data)) { |
| 51 | + $metaTable->patchEntity($data, $this->request->data()); |
| 52 | + if ($metaTable->save($data)) { |
| 53 | + $this->Flash->success('Meta record updated.'); |
| 54 | + $this->redirect(['action' => 'index'], null, true); |
| 55 | + } else { |
| 56 | + $this->Flash->error('Error encountered while saving.'); |
| 57 | + } |
| 58 | + } |
| 59 | + $this->set('data', $data); |
| 60 | + } |
| 61 | + |
| 62 | + public function autoAdd() |
| 63 | + { |
| 64 | + $count = 0; |
| 65 | + $searchTables = Configure::read('Meta.searchTables'); |
| 66 | + |
| 67 | + $metaTable = TableRegistry::get('Meta.Meta'); |
| 68 | + $meta = $metaTable->find('list', [ |
| 69 | + 'valueField' => 'path' |
| 70 | + ]); |
| 71 | + $meta = $meta->toArray(); |
| 72 | + |
| 73 | + // add meta data for database records |
| 74 | + if (is_array($searchTables) && count($searchTables)) { |
| 75 | + foreach($searchTables as $searchTable => $tableMap) { |
| 76 | + if ($table = TableRegistry::get($searchTable)) { |
| 77 | + $data = $table->find('all'); |
| 78 | + |
| 79 | + foreach($data as $row) { |
| 80 | + $row = $row->toArray(); |
| 81 | + foreach($tableMap as $metaField => &$tableField) { |
| 82 | + if (strstr($tableField, '{')) { |
| 83 | + foreach($row as $field => $value) { |
| 84 | + $tableField = str_replace('{'.$field.'}', $value, $tableField); |
| 85 | + |
| 86 | + if ($metaField == 'description' && (strlen($tableField) > 160 || strstr($tableField, '<'))) { |
| 87 | + $tableField = $this->_extractDescription($tableField); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + $tableMap['controller'] = ucfirst($tableMap['controller']); |
| 94 | + |
| 95 | + if (empty($meta) || in_array($tableMap['path'], $meta) == false) { |
| 96 | + $newMeta = $metaTable->newEntity($tableMap); |
| 97 | + $metaTable->save($newMeta); |
| 98 | + $count++; |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + // add meta data for view files |
| 106 | + $newPaths = []; |
| 107 | + $newPaths = $this->_findPaths(APP . 'Template'); |
| 108 | + if (count($newPaths)) { |
| 109 | + foreach ($newPaths as $path => $info) { |
| 110 | + if (in_array($path, $meta) == false) { |
| 111 | + $newData['Meta']['path'] = $path; |
| 112 | + |
| 113 | + $pathArray = explode('/', substr($path, 1), 3); |
| 114 | + $newData['Meta']['controller'] = ucfirst($pathArray[0]); |
| 115 | + if (stristr($pathArray[0], 'pages')) { |
| 116 | + $newData['Meta']['action'] = 'display'; |
| 117 | + $newData['Meta']['pass'] = $pathArray[1]; |
| 118 | + if (isset($pathArray[2])) { |
| 119 | + $newData['Meta']['pass'] .= '/'.$pathArray[2]; |
| 120 | + } |
| 121 | + } else { |
| 122 | + $newData['Meta']['action'] = $pathArray[1]; |
| 123 | + if (isset($pathArray[2])) { |
| 124 | + $newData['Meta']['pass'] = $pathArray[2]; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + $newData['Meta']['title'] = $info['title']; |
| 129 | + $newData['Meta']['description'] = $info['description']; |
| 130 | + $newMeta = $metaTable->newEntity($newData); |
| 131 | + $metaTable->save($newMeta); |
| 132 | + $count++; |
| 133 | + unset($newData); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + if ($count) { |
| 139 | + $this->Flash->success("$count new paths found and saved."); |
| 140 | + } else { |
| 141 | + $this->Flash->error("No new paths found."); |
| 142 | + } |
| 143 | + |
| 144 | + $this->redirect(['action' => 'index']); |
| 145 | + } |
| 146 | + |
| 147 | + private function _findPaths($dir) |
| 148 | + { |
| 149 | + if (!is_dir($dir)) { |
| 150 | + return []; |
| 151 | + } |
| 152 | + |
| 153 | + $exclusions = [ |
| 154 | + '.', |
| 155 | + '..', |
| 156 | + '.DS_Store', |
| 157 | + '.svn', |
| 158 | + 'empty', |
| 159 | + 'ajax.ctp', |
| 160 | + 'Admin', |
| 161 | + 'Element', |
| 162 | + 'Email', |
| 163 | + 'Error', |
| 164 | + 'Layout', |
| 165 | + 'display.ctp' |
| 166 | + ]; |
| 167 | + $paths = []; |
| 168 | + $dir = dir($dir); |
| 169 | + |
| 170 | + while (($file = $dir->read()) !== false) { |
| 171 | + if (array_search($file, $exclusions) === false) { |
| 172 | + $filePath = $dir->path . DS . $file; |
| 173 | + if (is_dir($filePath)) { |
| 174 | + $paths = array_merge($paths, $this->_findPaths($filePath)); |
| 175 | + } elseif (stristr($file, '.ctp')) { |
| 176 | + $fileExt = strchr($file, '.'); |
| 177 | + $fileName = basename($file, $fileExt); |
| 178 | + $path = str_replace('\\', '/', str_replace(APP . 'Template', '', $dir->path . DS . $fileName)); |
| 179 | + $path = strtolower($path); |
| 180 | + |
| 181 | + $pathParts = array_values(array_filter(explode('/', $path))); |
| 182 | + if ($pathParts[0] == 'Pages') { |
| 183 | + unset($pathParts[0]); |
| 184 | + } |
| 185 | + $title = \Cake\Utility\Inflector::humanize(implode(' - ', $pathParts)); |
| 186 | + |
| 187 | + if (($fileHandle = fopen($filePath, 'r')) !== false) { |
| 188 | + $description = $this->_extractDescription(fread($fileHandle, 3072)); |
| 189 | + } |
| 190 | + $paths = array_merge($paths, [$path => ['title' => $title, 'description' => $description]]); |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + $dir->close(); |
| 195 | + |
| 196 | + ksort($paths); |
| 197 | + return $paths; |
| 198 | + } |
| 199 | + |
| 200 | + private function _extractDescription($content) |
| 201 | + { |
| 202 | + $description = strip_tags($content); |
| 203 | + $description = preg_replace('/\s\s+/', ' ', $description); // strip whitespace |
| 204 | + $description = preg_replace('/\[\{\[.*\]\}\]/', '', $description); // strip element plugins |
| 205 | + $description = substr($description, 0, 150); |
| 206 | + return $description; |
| 207 | + } |
| 208 | +} |
0 commit comments