| Conditions | 142 | 
| Paths | 0 | 
| Total Lines | 1109 | 
| Code Lines | 774 | 
| Lines | 140 | 
| Ratio | 12.62 % | 
| Changes | 5 | ||
| Bugs | 0 | Features | 0 | 
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php  | 
            ||
| 79 | public function __form()  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 80 |         { | 
            ||
| 81 | $formHasErrors = (is_array($this->_errors) && !empty($this->_errors));  | 
            ||
| 82 | |||
| 83 | View Code Duplication |             if ($formHasErrors) { | 
            |
| 84 | $this->pageAlert(  | 
            ||
| 85 |                     __('An error occurred while processing this form. See below for details.'), | 
            ||
| 86 | Alert::ERROR  | 
            ||
| 87 | );  | 
            ||
| 88 | |||
| 89 | // These alerts are only valid if the form doesn't have errors  | 
            ||
| 90 |             } elseif (isset($this->_context['flag'])) { | 
            ||
| 91 | $time = Widget::Time();  | 
            ||
| 92 | |||
| 93 |                 switch ($this->_context['flag']) { | 
            ||
| 94 | case 'saved':  | 
            ||
| 95 |                         $message = __('Data Source updated at %s.', array($time->generate())); | 
            ||
| 96 | break;  | 
            ||
| 97 | case 'created':  | 
            ||
| 98 |                         $message = __('Data Source created at %s.', array($time->generate())); | 
            ||
| 99 | }  | 
            ||
| 100 | |||
| 101 | $this->pageAlert(  | 
            ||
| 102 | $message  | 
            ||
| 103 | . ' <a href="' . SYMPHONY_URL . '/blueprints/datasources/new/" accesskey="c">'  | 
            ||
| 104 |                     . __('Create another?') | 
            ||
| 105 | . '</a> <a href="' . SYMPHONY_URL . '/blueprints/datasources/" accesskey="a">'  | 
            ||
| 106 |                     . __('View all Data Sources') | 
            ||
| 107 | . '</a>',  | 
            ||
| 108 | Alert::SUCCESS  | 
            ||
| 109 | );  | 
            ||
| 110 | }  | 
            ||
| 111 | |||
| 112 | $providers = Symphony::ExtensionManager()->getProvidersOf(iProvider::DATASOURCE);  | 
            ||
| 113 | $isEditing = $this->_context['action'] === 'edit';  | 
            ||
| 114 | $about = $handle = null;  | 
            ||
| 115 | $fields = array(  | 
            ||
| 116 | 'name' => null,  | 
            ||
| 117 | 'source' => null,  | 
            ||
| 118 | 'filter' => null,  | 
            ||
| 119 | 'required_url_param' => null,  | 
            ||
| 120 | 'negate_url_param' => null,  | 
            ||
| 121 | 'param' => null  | 
            ||
| 122 | );  | 
            ||
| 123 | |||
| 124 |             if (isset($_POST['fields'])) { | 
            ||
| 125 | $fields = $_POST['fields'];  | 
            ||
| 126 | |||
| 127 | if (  | 
            ||
| 128 |                     !in_array($fields['source'], array('authors', 'navigation', 'static_xml')) | 
            ||
| 129 | && !empty($fields['filter']) && is_array($fields['filter'])  | 
            ||
| 130 |                 ) { | 
            ||
| 131 | $filters = array();  | 
            ||
| 132 |                     foreach ($fields['filter'] as $f) { | 
            ||
| 133 |                         foreach ($f as $key => $val) { | 
            ||
| 134 | $filters[$key] = $val;  | 
            ||
| 135 | }  | 
            ||
| 136 | }  | 
            ||
| 137 | |||
| 138 | $fields['filter'][$fields['source']] = $filters;  | 
            ||
| 139 | }  | 
            ||
| 140 | |||
| 141 |                 if (!isset($fields['xml_elements']) || !is_array($fields['xml_elements'])) { | 
            ||
| 142 | $fields['xml_elements'] = array();  | 
            ||
| 143 | }  | 
            ||
| 144 |             } elseif ($isEditing) { | 
            ||
| 145 | $handle = $this->_context['handle'];  | 
            ||
| 146 | $existing = DatasourceManager::create($handle, array(), false);  | 
            ||
| 147 | $order = isset($existing->dsParamORDER) ? $existing->dsParamORDER : 'asc';  | 
            ||
| 148 | |||
| 149 |                 if (!$existing->allowEditorToParse()) { | 
            ||
| 150 | redirect(SYMPHONY_URL . '/blueprints/datasources/info/' . $handle . '/');  | 
            ||
| 151 | }  | 
            ||
| 152 | |||
| 153 | $about = $existing->about();  | 
            ||
| 154 | $fields['name'] = $about['name'];  | 
            ||
| 155 | |||
| 156 | $fields['order'] = ($order === 'rand') ? 'random' : $order;  | 
            ||
| 157 | $fields['param'] = isset($existing->dsParamPARAMOUTPUT) ? $existing->dsParamPARAMOUTPUT : null;  | 
            ||
| 158 | $fields['required_url_param'] = isset($existing->dsParamREQUIREDPARAM) ? trim($existing->dsParamREQUIREDPARAM) : null;  | 
            ||
| 159 | $fields['negate_url_param'] = isset($existing->dsParamNEGATEPARAM) ? trim($existing->dsParamNEGATEPARAM) : null;  | 
            ||
| 160 | |||
| 161 |                 if (isset($existing->dsParamINCLUDEDELEMENTS) && is_array($existing->dsParamINCLUDEDELEMENTS)) { | 
            ||
| 162 | $fields['xml_elements'] = $existing->dsParamINCLUDEDELEMENTS;  | 
            ||
| 163 |                 } else { | 
            ||
| 164 | $fields['xml_elements'] = array();  | 
            ||
| 165 | }  | 
            ||
| 166 | |||
| 167 | $fields['sort'] = isset($existing->dsParamSORT) ? $existing->dsParamSORT : null;  | 
            ||
| 168 | $fields['paginate_results'] = isset($existing->dsParamPAGINATERESULTS) ? $existing->dsParamPAGINATERESULTS : 'yes';  | 
            ||
| 169 | $fields['page_number'] = isset($existing->dsParamSTARTPAGE) ? $existing->dsParamSTARTPAGE : '1';  | 
            ||
| 170 | $fields['group'] = isset($existing->dsParamGROUP) ? $existing->dsParamGROUP : null;  | 
            ||
| 171 | $fields['html_encode'] = isset($existing->dsParamHTMLENCODE) ? $existing->dsParamHTMLENCODE : 'no';  | 
            ||
| 172 | $fields['associated_entry_counts'] = isset($existing->dsParamASSOCIATEDENTRYCOUNTS) ? $existing->dsParamASSOCIATEDENTRYCOUNTS : 'no';  | 
            ||
| 173 | $fields['redirect_on_empty'] = isset($existing->dsParamREDIRECTONEMPTY) ? $existing->dsParamREDIRECTONEMPTY : 'no';  | 
            ||
| 174 | $fields['redirect_on_forbidden'] = isset($existing->dsParamREDIRECTONFORBIDDEN) ? $existing->dsParamREDIRECTONFORBIDDEN : 'no';  | 
            ||
| 175 | $fields['redirect_on_required'] = isset($existing->dsParamREDIRECTONREQUIRED) ? $existing->dsParamREDIRECTONREQUIRED : 'no';  | 
            ||
| 176 | |||
| 177 |                 if (!isset($existing->dsParamFILTERS) || !is_array($existing->dsParamFILTERS)) { | 
            ||
| 178 | $existing->dsParamFILTERS = array();  | 
            ||
| 179 | }  | 
            ||
| 180 | |||
| 181 |                 if (!empty($existing->dsParamFILTERS)) { | 
            ||
| 182 |                     $existing->dsParamFILTERS = array_map('stripslashes', $existing->dsParamFILTERS); | 
            ||
| 183 | }  | 
            ||
| 184 | |||
| 185 | $fields['source'] = $existing->getSource();  | 
            ||
| 186 | |||
| 187 | $provided = false;  | 
            ||
| 188 | |||
| 189 | View Code Duplication |                 if (!empty($providers)) { | 
            |
| 190 |                     foreach ($providers as $providerClass => $provider) { | 
            ||
| 191 |                         if ($fields['source'] === call_user_func(array($providerClass, 'getClass'))) { | 
            ||
| 192 | $fields = array_merge($fields, $existing->settings());  | 
            ||
| 193 | $provided = true;  | 
            ||
| 194 | break;  | 
            ||
| 195 | }  | 
            ||
| 196 | }  | 
            ||
| 197 | }  | 
            ||
| 198 | |||
| 199 |                 if ($provided === false) { | 
            ||
| 200 |                     switch ($fields['source']) { | 
            ||
| 201 | case 'authors':  | 
            ||
| 202 | $fields['filter']['author'] = $existing->dsParamFILTERS;  | 
            ||
| 203 | break;  | 
            ||
| 204 | case 'navigation':  | 
            ||
| 205 | $fields['filter']['navigation'] = $existing->dsParamFILTERS;  | 
            ||
| 206 | break;  | 
            ||
| 207 | case 'static_xml':  | 
            ||
| 208 | // Symphony 2.3+  | 
            ||
| 209 |                             if (isset($existing->dsParamSTATIC)) { | 
            ||
| 210 | $fields['static_xml'] = trim($existing->dsParamSTATIC);  | 
            ||
| 211 | |||
| 212 | // Handle Symphony 2.2.2 to 2.3 DS's  | 
            ||
| 213 |                             } elseif (isset($existing->dsSTATIC)) { | 
            ||
| 214 | $fields['static_xml'] = trim($existing->dsSTATIC);  | 
            ||
| 215 | |||
| 216 | // Handle pre Symphony 2.2.1 Static DS's  | 
            ||
| 217 |                             } else { | 
            ||
| 218 | $fields['static_xml'] = trim($existing->grab());  | 
            ||
| 219 | }  | 
            ||
| 220 | break;  | 
            ||
| 221 | default:  | 
            ||
| 222 | $fields['filter'][$fields['source']] = $existing->dsParamFILTERS;  | 
            ||
| 223 | $fields['max_records'] = $existing->dsParamLIMIT;  | 
            ||
| 224 | break;  | 
            ||
| 225 | }  | 
            ||
| 226 | }  | 
            ||
| 227 |             } else { | 
            ||
| 228 | $fields['max_records'] = '20';  | 
            ||
| 229 | $fields['page_number'] = '1';  | 
            ||
| 230 | $fields['order'] = 'desc';  | 
            ||
| 231 | }  | 
            ||
| 232 | |||
| 233 | // Handle name on edited changes, or from reading an edited datasource  | 
            ||
| 234 | View Code Duplication |             if (isset($about['name'])) { | 
            |
| 235 | $name = $about['name'];  | 
            ||
| 236 |             } elseif (isset($fields['name'])) { | 
            ||
| 237 | $name = $fields['name'];  | 
            ||
| 238 | }  | 
            ||
| 239 | |||
| 240 |             $this->setPageType('form'); | 
            ||
| 241 | $this->setTitle(__(($isEditing ? '%1$s – %2$s – %3$s' : '%2$s – %3$s'),  | 
            ||
| 242 |                 array($name, __('Data Sources'), __('Symphony')))); | 
            ||
| 243 |             $this->appendSubheading(($isEditing ? $name : __('Untitled'))); | 
            ||
| 244 | $this->insertBreadcrumbs(array(  | 
            ||
| 245 |                 Widget::Anchor(__('Data Sources'), SYMPHONY_URL . '/blueprints/datasources/'), | 
            ||
| 246 | ));  | 
            ||
| 247 | |||
| 248 | // Sources  | 
            ||
| 249 |             $sources = new XMLElement('div', null, array('class' => 'apply actions')); | 
            ||
| 250 |             $div = new XMLElement('div'); | 
            ||
| 251 |             $label = Widget::Label(__('Source'), null, 'apply-label-left'); | 
            ||
| 252 | $sources->appendChild($label);  | 
            ||
| 253 | $sources->appendChild($div);  | 
            ||
| 254 | |||
| 255 | $sections = SectionManager::fetch(null, 'ASC', 'name');  | 
            ||
| 256 | |||
| 257 |             if (!is_array($sections)) { | 
            ||
| 258 | $sections = array();  | 
            ||
| 259 | }  | 
            ||
| 260 | |||
| 261 | $field_groups = array();  | 
            ||
| 262 | |||
| 263 | View Code Duplication |             foreach ($sections as $section) { | 
            |
| 264 |                 $field_groups[$section->get('id')] = array('fields' => $section->fetchFields(), 'section' => $section); | 
            ||
| 265 | }  | 
            ||
| 266 | |||
| 267 | $options = array(  | 
            ||
| 268 | array(  | 
            ||
| 269 |                     'label' => __('System'), | 
            ||
| 270 | 'data-label' => 'system',  | 
            ||
| 271 | 'options' => array(  | 
            ||
| 272 | array(  | 
            ||
| 273 | 'authors',  | 
            ||
| 274 | ($fields['source'] === 'authors'),  | 
            ||
| 275 |                             __('Authors'), | 
            ||
| 276 | null,  | 
            ||
| 277 | null,  | 
            ||
| 278 |                             array('data-context' => 'authors') | 
            ||
| 279 | ),  | 
            ||
| 280 | array(  | 
            ||
| 281 | 'navigation',  | 
            ||
| 282 | ($fields['source'] === 'navigation'),  | 
            ||
| 283 |                             __('Navigation'), | 
            ||
| 284 | null,  | 
            ||
| 285 | null,  | 
            ||
| 286 |                             array('data-context' => 'navigation') | 
            ||
| 287 | ),  | 
            ||
| 288 | )  | 
            ||
| 289 | ),  | 
            ||
| 290 | array(  | 
            ||
| 291 |                     'label' => __('Custom XML'), | 
            ||
| 292 | 'data-label' => 'custom-xml',  | 
            ||
| 293 | 'options' => array(  | 
            ||
| 294 | array(  | 
            ||
| 295 | 'static_xml',  | 
            ||
| 296 | ($fields['source'] === 'static_xml'),  | 
            ||
| 297 |                             __('Static XML'), | 
            ||
| 298 | null,  | 
            ||
| 299 | null,  | 
            ||
| 300 |                             array('data-context' => 'static-xml') | 
            ||
| 301 | ),  | 
            ||
| 302 | )  | 
            ||
| 303 | ),  | 
            ||
| 304 | );  | 
            ||
| 305 | |||
| 306 | // Loop over the datasource providers  | 
            ||
| 307 |             if (!empty($providers)) { | 
            ||
| 308 |                 $p = array('label' => __('From extensions'), 'data-label' => 'from_extensions', 'options' => array()); | 
            ||
| 309 | |||
| 310 |                 foreach ($providers as $providerClass => $provider) { | 
            ||
| 311 | $p['options'][] = array(  | 
            ||
| 312 | $providerClass,  | 
            ||
| 313 | ($fields['source'] === $providerClass),  | 
            ||
| 314 | $provider,  | 
            ||
| 315 | null,  | 
            ||
| 316 | null,  | 
            ||
| 317 |                         array('data-context' => Lang::createHandle($provider)) | 
            ||
| 318 | );  | 
            ||
| 319 | }  | 
            ||
| 320 | |||
| 321 | $options[] = $p;  | 
            ||
| 322 | }  | 
            ||
| 323 | |||
| 324 | // Add Sections  | 
            ||
| 325 |             if (is_array($sections) && !empty($sections)) { | 
            ||
| 326 | array_unshift($options,  | 
            ||
| 327 |                     array('label' => __('Sections'), 'data-label' => 'sections', 'options' => array())); | 
            ||
| 328 | |||
| 329 | View Code Duplication |                 foreach ($sections as $s) { | 
            |
| 330 | $options[0]['options'][] = array(  | 
            ||
| 331 |                         $s->get('id'), | 
            ||
| 332 |                         ($fields['source'] === $s->get('id')), | 
            ||
| 333 |                         General::sanitize($s->get('name')) | 
            ||
| 334 | );  | 
            ||
| 335 | }  | 
            ||
| 336 | }  | 
            ||
| 337 | |||
| 338 |             $div->appendChild(Widget::Select('source', $options, array('id' => 'ds-context'))); | 
            ||
| 339 | $this->Context->prependChild($sources);  | 
            ||
| 340 | |||
| 341 | $this->Form->appendChild(  | 
            ||
| 342 |                 Widget::Input('fields[source]', null, 'hidden', array('id' => 'ds-source')) | 
            ||
| 343 | );  | 
            ||
| 344 | |||
| 345 | // Name  | 
            ||
| 346 |             $fieldset = new XMLElement('fieldset'); | 
            ||
| 347 |             $fieldset->setAttribute('class', 'settings'); | 
            ||
| 348 |             $fieldset->appendChild(new XMLElement('legend', __('Essentials'))); | 
            ||
| 349 | |||
| 350 |             $group = new XMLElement('div'); | 
            ||
| 351 | |||
| 352 |             $label = Widget::Label(__('Name')); | 
            ||
| 353 |             $label->appendChild(Widget::Input('fields[name]', General::sanitize($fields['name']))); | 
            ||
| 354 | |||
| 355 |             if (isset($this->_errors['name'])) { | 
            ||
| 356 | $group->appendChild(Widget::Error($label, $this->_errors['name']));  | 
            ||
| 357 |             } else { | 
            ||
| 358 | $group->appendChild($label);  | 
            ||
| 359 | }  | 
            ||
| 360 | |||
| 361 | $fieldset->appendChild($group);  | 
            ||
| 362 | $this->Form->appendChild($fieldset);  | 
            ||
| 363 | |||
| 364 | // Conditions  | 
            ||
| 365 |             $fieldset = new XMLElement('fieldset'); | 
            ||
| 366 |             $this->setContext($fieldset, array('sections', 'system', 'custom-xml')); | 
            ||
| 367 |             $fieldset->appendChild(new XMLElement('legend', __('Conditions'))); | 
            ||
| 368 |             $p = new XMLElement('p', __('Leaving these fields empty will always execute the data source.')); | 
            ||
| 369 |             $p->setAttribute('class', 'help'); | 
            ||
| 370 | $fieldset->appendChild($p);  | 
            ||
| 371 | |||
| 372 |             $group = new XMLElement('div'); | 
            ||
| 373 |             $group->setAttribute('class', 'two columns'); | 
            ||
| 374 | |||
| 375 |             $label = Widget::Label(__('Required Parameter')); | 
            ||
| 376 |             $label->setAttribute('class', 'column ds-param'); | 
            ||
| 377 |             $label->appendChild(new XMLElement('i', __('Optional'))); | 
            ||
| 378 |             $input = Widget::Input('fields[required_url_param]', trim($fields['required_url_param']), 'text', array( | 
            ||
| 379 |                 'placeholder' => __('$param'), | 
            ||
| 380 | 'data-search-types' => 'parameters',  | 
            ||
| 381 | 'data-trigger' => '$'  | 
            ||
| 382 | ));  | 
            ||
| 383 | $label->appendChild($input);  | 
            ||
| 384 | $group->appendChild($label);  | 
            ||
| 385 | |||
| 386 |             $label = Widget::Label(__('Forbidden Parameter')); | 
            ||
| 387 |             $label->setAttribute('class', 'column ds-param'); | 
            ||
| 388 |             $label->appendChild(new XMLElement('i', __('Optional'))); | 
            ||
| 389 |             $input = Widget::Input('fields[negate_url_param]', trim($fields['negate_url_param']), 'text', array( | 
            ||
| 390 |                 'placeholder' => __('$param'), | 
            ||
| 391 | 'data-search-types' => 'parameters',  | 
            ||
| 392 | 'data-trigger' => '$'  | 
            ||
| 393 | ));  | 
            ||
| 394 | $label->appendChild($input);  | 
            ||
| 395 | $group->appendChild($label);  | 
            ||
| 396 | |||
| 397 | $fieldset->appendChild($group);  | 
            ||
| 398 | |||
| 399 |             $group = new XMLElement('div'); | 
            ||
| 400 |             $group->setAttribute('class', 'two columns ds-param'); | 
            ||
| 401 | |||
| 402 |             $label = Widget::Checkbox('fields[redirect_on_required]', $fields['redirect_on_required'], | 
            ||
| 403 |                 __('Redirect to 404 page when the required parameter is not present')); | 
            ||
| 404 |             $label->setAttribute('class', 'column'); | 
            ||
| 405 | $group->appendChild($label);  | 
            ||
| 406 | |||
| 407 |             $label = Widget::Checkbox('fields[redirect_on_forbidden]', $fields['redirect_on_forbidden'], | 
            ||
| 408 |                 __('Redirect to 404 page when the forbidden parameter is present')); | 
            ||
| 409 |             $label->setAttribute('class', 'column'); | 
            ||
| 410 | $group->appendChild($label);  | 
            ||
| 411 | |||
| 412 | $fieldset->appendChild($group);  | 
            ||
| 413 | |||
| 414 |             $label = Widget::Checkbox('fields[redirect_on_empty]', $fields['redirect_on_empty'], | 
            ||
| 415 |                 __('Redirect to 404 page when no results are found')); | 
            ||
| 416 |             $label->setAttribute('class', 'column'); | 
            ||
| 417 | $fieldset->appendChild($label);  | 
            ||
| 418 | |||
| 419 | $this->Form->appendChild($fieldset);  | 
            ||
| 420 | |||
| 421 | // Filters  | 
            ||
| 422 |             $fieldset = new XMLElement('fieldset'); | 
            ||
| 423 |             $this->setContext($fieldset, array('sections', 'system')); | 
            ||
| 424 |             $fieldset->appendChild(new XMLElement('legend', __('Filters'))); | 
            ||
| 425 |             $p = new XMLElement('p', | 
            ||
| 426 |                 __('Use %s syntax to filter by page parameters. A default value can be set using %s.', array( | 
            ||
| 427 |                     '<code>{' . __('$param') . '}</code>', | 
            ||
| 428 |                     '<code>{' . __('$param:default') . '}</code>' | 
            ||
| 429 | ))  | 
            ||
| 430 | );  | 
            ||
| 431 |             $p->setAttribute('class', 'help'); | 
            ||
| 432 | $fieldset->appendChild($p);  | 
            ||
| 433 | |||
| 434 |             foreach ($field_groups as $section_id => $section_data) { | 
            ||
| 435 |                 $div = new XMLElement('div'); | 
            ||
| 436 |                 $div->setAttribute('class', 'contextual frame filters-duplicator'); | 
            ||
| 437 |                 $div->setAttribute('data-context', 'section-' . $section_id); | 
            ||
| 438 |                 $div->setAttribute('data-interactive', 'data-interactive'); | 
            ||
| 439 | |||
| 440 |                 $ol = new XMLElement('ol'); | 
            ||
| 441 |                 $ol->setAttribute('class', 'suggestable'); | 
            ||
| 442 |                 $ol->setAttribute('data-interactive', 'data-interactive'); | 
            ||
| 443 |                 $ol->setAttribute('data-add', __('Add filter')); | 
            ||
| 444 |                 $ol->setAttribute('data-remove', __('Remove filter')); | 
            ||
| 445 | |||
| 446 | // Add system:id filter  | 
            ||
| 447 | View Code Duplication | if (  | 
            |
| 448 | isset($fields['filter'][$section_id]['system:id'])  | 
            ||
| 449 | || isset($fields['filter'][$section_id]['id'])  | 
            ||
| 450 |                 ) { | 
            ||
| 451 | $id = isset($fields['filter'][$section_id]['system:id'])  | 
            ||
| 452 | ? $fields['filter'][$section_id]['system:id']  | 
            ||
| 453 | : $fields['filter'][$section_id]['id'];  | 
            ||
| 454 | |||
| 455 |                     $li = new XMLElement('li'); | 
            ||
| 456 |                     $li->setAttribute('class', 'unique'); | 
            ||
| 457 |                     $li->setAttribute('data-type', 'system:id'); | 
            ||
| 458 |                     $li->appendChild(new XMLElement('header', '<h4>' . __('System ID') . '</h4>')); | 
            ||
| 459 |                     $label = Widget::Label(__('Value')); | 
            ||
| 460 |                     $input = Widget::Input('fields[filter][' . $section_id . '][system:id]', General::sanitize($id)); | 
            ||
| 461 |                     $input->setAttribute('data-search-types', 'parameters'); | 
            ||
| 462 |                     $input->setAttribute('data-trigger', '{$'); | 
            ||
| 463 | $label->appendChild($input);  | 
            ||
| 464 | $li->appendChild($label);  | 
            ||
| 465 | $ol->appendChild($li);  | 
            ||
| 466 | }  | 
            ||
| 467 | |||
| 468 |                 $li = new XMLElement('li'); | 
            ||
| 469 |                 $li->setAttribute('class', 'unique template'); | 
            ||
| 470 |                 $li->setAttribute('data-type', 'system:id'); | 
            ||
| 471 |                 $li->appendChild(new XMLElement('header', '<h4>' . __('System ID') . '</h4>')); | 
            ||
| 472 |                 $label = Widget::Label(__('Value')); | 
            ||
| 473 |                 $input = Widget::Input('fields[filter][' . $section_id . '][system:id]', General::sanitize($id)); | 
            ||
| 474 |                 $input->setAttribute('data-search-types', 'parameters'); | 
            ||
| 475 |                 $input->setAttribute('data-trigger', '{$'); | 
            ||
| 476 | $label->appendChild($input);  | 
            ||
| 477 | $li->appendChild($label);  | 
            ||
| 478 | $ol->appendChild($li);  | 
            ||
| 479 | |||
| 480 | // Add system:date filter  | 
            ||
| 481 | View Code Duplication | if (  | 
            |
| 482 | isset($fields['filter'][$section_id]['system:creation-date'])  | 
            ||
| 483 | || isset($fields['filter'][$section_id]['system:date'])  | 
            ||
| 484 |                 ) { | 
            ||
| 485 | $creation_date = isset($fields['filter'][$section_id]['system:creation-date'])  | 
            ||
| 486 | ? $fields['filter'][$section_id]['system:creation-date']  | 
            ||
| 487 | : $fields['filter'][$section_id]['system:date'];  | 
            ||
| 488 | |||
| 489 |                     $li = new XMLElement('li'); | 
            ||
| 490 |                     $li->setAttribute('class', 'unique'); | 
            ||
| 491 |                     $li->setAttribute('data-type', 'system:creation-date'); | 
            ||
| 492 |                     $li->appendChild(new XMLElement('header', '<h4>' . __('System Creation Date') . '</h4>')); | 
            ||
| 493 |                     $label = Widget::Label(__('Value')); | 
            ||
| 494 |                     $input = Widget::Input('fields[filter][' . $section_id . '][system:creation-date]', | 
            ||
| 495 | General::sanitize($creation_date));  | 
            ||
| 496 |                     $input->setAttribute('data-search-types', 'parameters'); | 
            ||
| 497 |                     $input->setAttribute('data-trigger', '{$'); | 
            ||
| 498 | $label->appendChild($input);  | 
            ||
| 499 | $li->appendChild($label);  | 
            ||
| 500 | $ol->appendChild($li);  | 
            ||
| 501 | }  | 
            ||
| 502 | |||
| 503 |                 $li = new XMLElement('li'); | 
            ||
| 504 |                 $li->setAttribute('class', 'unique template'); | 
            ||
| 505 |                 $li->setAttribute('data-type', 'system:creation-date'); | 
            ||
| 506 |                 $li->appendChild(new XMLElement('header', '<h4>' . __('System Creation Date') . '</h4>')); | 
            ||
| 507 |                 $label = Widget::Label(__('Value')); | 
            ||
| 508 |                 $input = Widget::Input('fields[filter][' . $section_id . '][system:creation-date]'); | 
            ||
| 509 |                 $input->setAttribute('data-search-types', 'parameters'); | 
            ||
| 510 |                 $input->setAttribute('data-trigger', '{$'); | 
            ||
| 511 | $label->appendChild($input);  | 
            ||
| 512 | $li->appendChild($label);  | 
            ||
| 513 | $ol->appendChild($li);  | 
            ||
| 514 | |||
| 515 |                 if (isset($fields['filter'][$section_id]['system:modification-date'])) { | 
            ||
| 516 |                     $li = new XMLElement('li'); | 
            ||
| 517 |                     $li->setAttribute('class', 'unique'); | 
            ||
| 518 |                     $li->setAttribute('data-type', 'system:modification-date'); | 
            ||
| 519 |                     $li->appendChild(new XMLElement('header', '<h4>' . __('System Modification Date') . '</h4>')); | 
            ||
| 520 |                     $label = Widget::Label(__('Value')); | 
            ||
| 521 |                     $input = Widget::Input('fields[filter][' . $section_id . '][system:modification-date]', | 
            ||
| 522 | General::sanitize($fields['filter'][$section_id]['system:modification-date']));  | 
            ||
| 523 |                     $input->setAttribute('data-search-types', 'parameters'); | 
            ||
| 524 |                     $input->setAttribute('data-trigger', '{$'); | 
            ||
| 525 | $label->appendChild($input);  | 
            ||
| 526 | $li->appendChild($label);  | 
            ||
| 527 | $ol->appendChild($li);  | 
            ||
| 528 | }  | 
            ||
| 529 | |||
| 530 |                 $li = new XMLElement('li'); | 
            ||
| 531 |                 $li->setAttribute('class', 'unique template'); | 
            ||
| 532 |                 $li->setAttribute('data-type', 'system:modification-date'); | 
            ||
| 533 |                 $li->appendChild(new XMLElement('header', '<h4>' . __('System Modification Date') . '</h4>')); | 
            ||
| 534 |                 $label = Widget::Label(__('Value')); | 
            ||
| 535 |                 $input = Widget::Input('fields[filter][' . $section_id . '][system:modification-date]'); | 
            ||
| 536 |                 $input->setAttribute('data-search-types', 'parameters'); | 
            ||
| 537 |                 $input->setAttribute('data-trigger', '{$'); | 
            ||
| 538 | $label->appendChild($input);  | 
            ||
| 539 | $li->appendChild($label);  | 
            ||
| 540 | $ol->appendChild($li);  | 
            ||
| 541 | |||
| 542 |                 if (is_array($section_data['fields']) && !empty($section_data['fields'])) { | 
            ||
| 543 |                     foreach ($section_data['fields'] as $field) { | 
            ||
| 544 |                         if (!$field->canFilter()) { | 
            ||
| 545 | continue;  | 
            ||
| 546 | }  | 
            ||
| 547 | |||
| 548 |                         if (isset($fields['filter'][$section_id], $fields['filter'][$section_id][$field->get('id')])) { | 
            ||
| 549 |                             $wrapper = new XMLElement('li'); | 
            ||
| 550 |                             $wrapper->setAttribute('class', 'unique'); | 
            ||
| 551 |                             $wrapper->setAttribute('data-type', $field->get('element_name')); | 
            ||
| 552 |                             $errors = isset($this->_errors[$field->get('id')]) | 
            ||
| 553 |                                 ? $this->_errors[$field->get('id')] | 
            ||
| 554 | : array();  | 
            ||
| 555 | |||
| 556 | $field->displayDatasourceFilterPanel($wrapper,  | 
            ||
| 557 |                                 $fields['filter'][$section_id][$field->get('id')], $errors, $section_id); | 
            ||
| 558 | $ol->appendChild($wrapper);  | 
            ||
| 559 | }  | 
            ||
| 560 | |||
| 561 |                         $wrapper = new XMLElement('li'); | 
            ||
| 562 |                         $wrapper->setAttribute('class', 'unique template'); | 
            ||
| 563 |                         $wrapper->setAttribute('data-type', $field->get('element_name')); | 
            ||
| 564 | $field->displayDatasourceFilterPanel($wrapper, null, null, $section_id);  | 
            ||
| 565 | $ol->appendChild($wrapper);  | 
            ||
| 566 | }  | 
            ||
| 567 | }  | 
            ||
| 568 | |||
| 569 | $div->appendChild($ol);  | 
            ||
| 570 | |||
| 571 | $fieldset->appendChild($div);  | 
            ||
| 572 | }  | 
            ||
| 573 | |||
| 574 |             $div = new XMLElement('div'); | 
            ||
| 575 |             $div->setAttribute('class', 'contextual frame filters-duplicator'); | 
            ||
| 576 |             $div->setAttribute('data-context', 'authors'); | 
            ||
| 577 |             $div->setAttribute('data-interactive', 'data-interactive'); | 
            ||
| 578 | |||
| 579 |             $ol = new XMLElement('ol'); | 
            ||
| 580 |             $ol->setAttribute('class', 'suggestable'); | 
            ||
| 581 |             $ol->setAttribute('data-interactive', 'data-interactive'); | 
            ||
| 582 |             $ol->setAttribute('data-add', __('Add filter')); | 
            ||
| 583 |             $ol->setAttribute('data-remove', __('Remove filter')); | 
            ||
| 584 | |||
| 585 |             if (!isset($fields['filter']['author'])) { | 
            ||
| 586 | $fields['filter']['author'] = array(  | 
            ||
| 587 | 'id' => null,  | 
            ||
| 588 | 'username' => null,  | 
            ||
| 589 | 'first_name' => null,  | 
            ||
| 590 | 'last_name' => null,  | 
            ||
| 591 | 'email' => null,  | 
            ||
| 592 | 'user_type' => null  | 
            ||
| 593 | );  | 
            ||
| 594 | }  | 
            ||
| 595 | |||
| 596 |             $this->__appendAuthorFilter($ol, __('ID'), 'id', $fields['filter']['author']['id'], | 
            ||
| 597 | (!isset($fields['filter']['author']['id'])));  | 
            ||
| 598 |             $this->__appendAuthorFilter($ol, __('Username'), 'username', $fields['filter']['author']['username'], | 
            ||
| 599 | (!isset($fields['filter']['author']['username'])));  | 
            ||
| 600 |             $this->__appendAuthorFilter($ol, __('First Name'), 'first_name', $fields['filter']['author']['first_name'], | 
            ||
| 601 | (!isset($fields['filter']['author']['first_name'])));  | 
            ||
| 602 |             $this->__appendAuthorFilter($ol, __('Last Name'), 'last_name', $fields['filter']['author']['last_name'], | 
            ||
| 603 | (!isset($fields['filter']['author']['last_name'])));  | 
            ||
| 604 |             $this->__appendAuthorFilter($ol, __('Email'), 'email', $fields['filter']['author']['email'], | 
            ||
| 605 | (!isset($fields['filter']['author']['email'])));  | 
            ||
| 606 |             $this->__appendAuthorFilter($ol, __('User Type'), 'user_type', $fields['filter']['author']['user_type'], | 
            ||
| 607 | (!isset($fields['filter']['author']['user_type'])));  | 
            ||
| 608 | |||
| 609 | $div->appendChild($ol);  | 
            ||
| 610 | |||
| 611 | $fieldset->appendChild($div);  | 
            ||
| 612 | |||
| 613 |             $div = new XMLElement('div'); | 
            ||
| 614 |             $div->setAttribute('class', 'contextual frame filters-duplicator'); | 
            ||
| 615 |             $div->setAttribute('data-context', 'navigation'); | 
            ||
| 616 |             $div->setAttribute('data-interactive', 'data-interactive'); | 
            ||
| 617 | |||
| 618 |             $ol = new XMLElement('ol'); | 
            ||
| 619 |             $ol->setAttribute('class', 'suggestable'); | 
            ||
| 620 |             $ol->setAttribute('data-interactive', 'data-interactive'); | 
            ||
| 621 |             $ol->setAttribute('data-add', __('Add filter')); | 
            ||
| 622 |             $ol->setAttribute('data-remove', __('Remove filter')); | 
            ||
| 623 | |||
| 624 |             $ul = new XMLElement('ul'); | 
            ||
| 625 |             $ul->setAttribute('class', 'tags'); | 
            ||
| 626 |             $ul->setAttribute('data-interactive', 'data-interactive'); | 
            ||
| 627 | |||
| 628 |             $pages = PageManager::fetch(false, array('*'), array(), 'title ASC'); | 
            ||
| 629 | |||
| 630 |             foreach ($pages as $page) { | 
            ||
| 631 |                 $ul->appendChild(new XMLElement('li', | 
            ||
| 632 |                     preg_replace('/\/{2,}/i', '/', '/' . $page['path'] . '/' . $page['handle']))); | 
            ||
| 633 | }  | 
            ||
| 634 | |||
| 635 | View Code Duplication |             if (isset($fields['filter']['navigation']['parent'])) { | 
            |
| 636 |                 $li = new XMLElement('li'); | 
            ||
| 637 |                 $li->setAttribute('class', 'unique'); | 
            ||
| 638 |                 $li->setAttribute('data-type', 'parent'); | 
            ||
| 639 |                 $li->appendChild(new XMLElement('header', '<h4>' . __('Parent Page') . '</h4>')); | 
            ||
| 640 |                 $label = Widget::Label(__('Value')); | 
            ||
| 641 |                 $label->appendChild(Widget::Input('fields[filter][navigation][parent]', | 
            ||
| 642 | General::sanitize($fields['filter']['navigation']['parent'])));  | 
            ||
| 643 | $li->appendChild($label);  | 
            ||
| 644 | $li->appendChild($ul);  | 
            ||
| 645 | $ol->appendChild($li);  | 
            ||
| 646 | }  | 
            ||
| 647 | |||
| 648 |             $li = new XMLElement('li'); | 
            ||
| 649 |             $li->setAttribute('class', 'unique template'); | 
            ||
| 650 |             $li->setAttribute('data-type', 'parent'); | 
            ||
| 651 |             $li->appendChild(new XMLElement('header', '<h4>' . __('Parent Page') . '</h4>')); | 
            ||
| 652 |             $label = Widget::Label(__('Value')); | 
            ||
| 653 |             $label->appendChild(Widget::Input('fields[filter][navigation][parent]')); | 
            ||
| 654 | $li->appendChild($label);  | 
            ||
| 655 | $li->appendChild($ul);  | 
            ||
| 656 | $ol->appendChild($li);  | 
            ||
| 657 | |||
| 658 |             $ul = new XMLElement('ul'); | 
            ||
| 659 |             $ul->setAttribute('class', 'tags'); | 
            ||
| 660 |             $ul->setAttribute('data-interactive', 'data-interactive'); | 
            ||
| 661 | |||
| 662 |             if ($types = PageManager::fetchAvailablePageTypes()) { | 
            ||
| 663 |                 foreach ($types as $type) { | 
            ||
| 664 |                     $ul->appendChild(new XMLElement('li', $type)); | 
            ||
| 665 | }  | 
            ||
| 666 | }  | 
            ||
| 667 | |||
| 668 | View Code Duplication |             if (isset($fields['filter']['navigation']['type'])) { | 
            |
| 669 |                 $li = new XMLElement('li'); | 
            ||
| 670 |                 $li->setAttribute('class', 'unique'); | 
            ||
| 671 |                 $li->setAttribute('data-type', 'type'); | 
            ||
| 672 |                 $li->appendChild(new XMLElement('header', '<h4>' . __('Page Type') . '</h4>')); | 
            ||
| 673 |                 $label = Widget::Label(__('Value')); | 
            ||
| 674 |                 $label->appendChild(Widget::Input('fields[filter][navigation][type]', | 
            ||
| 675 | General::sanitize($fields['filter']['navigation']['type'])));  | 
            ||
| 676 | $li->appendChild($label);  | 
            ||
| 677 | $li->appendChild($ul);  | 
            ||
| 678 | $ol->appendChild($li);  | 
            ||
| 679 | }  | 
            ||
| 680 | |||
| 681 |             $li = new XMLElement('li'); | 
            ||
| 682 |             $li->setAttribute('class', 'unique template'); | 
            ||
| 683 |             $li->appendChild(new XMLElement('header', '<h4>' . __('Page Type') . '</h4>')); | 
            ||
| 684 |             $li->setAttribute('data-type', 'type'); | 
            ||
| 685 |             $label = Widget::Label(__('Value')); | 
            ||
| 686 |             $label->appendChild(Widget::Input('fields[filter][navigation][type]')); | 
            ||
| 687 | $li->appendChild($label);  | 
            ||
| 688 | $li->appendChild($ul);  | 
            ||
| 689 | $ol->appendChild($li);  | 
            ||
| 690 | |||
| 691 | $div->appendChild($ol);  | 
            ||
| 692 | |||
| 693 | $fieldset->appendChild($div);  | 
            ||
| 694 | $this->Form->appendChild($fieldset);  | 
            ||
| 695 | |||
| 696 | // Sorting  | 
            ||
| 697 |             $fieldset = new XMLElement('fieldset'); | 
            ||
| 698 |             $this->setContext($fieldset, array('sections', 'system')); | 
            ||
| 699 |             $fieldset->appendChild(new XMLElement('legend', __('Sorting'))); | 
            ||
| 700 | |||
| 701 |             $p = new XMLElement('p', | 
            ||
| 702 |                 __('Use %s syntax to order by page parameters.', array( | 
            ||
| 703 |                     '<code>{' . __('$param') . '}</code>' | 
            ||
| 704 | ))  | 
            ||
| 705 | );  | 
            ||
| 706 |             $p->setAttribute('class', 'help'); | 
            ||
| 707 | $fieldset->appendChild($p);  | 
            ||
| 708 | |||
| 709 |             $div = new XMLElement('div'); | 
            ||
| 710 | |||
| 711 |             $label = Widget::Label(__('Sort By')); | 
            ||
| 712 | |||
| 713 | $options = array(  | 
            ||
| 714 | array(  | 
            ||
| 715 |                     'label' => __('Authors'), | 
            ||
| 716 | 'data-label' => 'authors',  | 
            ||
| 717 | 'options' => array(  | 
            ||
| 718 |                         array('id', ($fields['source'] === 'authors' && $fields['sort'] === 'id'), __('Author ID')), | 
            ||
| 719 | array(  | 
            ||
| 720 | 'username',  | 
            ||
| 721 | ($fields['source'] === 'authors' && $fields['sort'] === 'username'),  | 
            ||
| 722 |                             __('Username') | 
            ||
| 723 | ),  | 
            ||
| 724 | array(  | 
            ||
| 725 | 'first-name',  | 
            ||
| 726 | ($fields['source'] === 'authors' && $fields['sort'] === 'first-name'),  | 
            ||
| 727 |                             __('First Name') | 
            ||
| 728 | ),  | 
            ||
| 729 | array(  | 
            ||
| 730 | 'last-name',  | 
            ||
| 731 | ($fields['source'] === 'authors' && $fields['sort'] === 'last-name'),  | 
            ||
| 732 |                             __('Last Name') | 
            ||
| 733 | ),  | 
            ||
| 734 |                         array('email', ($fields['source'] === 'authors' && $fields['sort'] === 'email'), __('Email')), | 
            ||
| 735 | array(  | 
            ||
| 736 | 'status',  | 
            ||
| 737 | ($fields['source'] === 'authors' && $fields['sort'] === 'status'),  | 
            ||
| 738 |                             __('Status') | 
            ||
| 739 | ),  | 
            ||
| 740 | )  | 
            ||
| 741 | ),  | 
            ||
| 742 | |||
| 743 | array(  | 
            ||
| 744 |                     'label' => __('Navigation'), | 
            ||
| 745 | 'data-label' => 'navigation',  | 
            ||
| 746 | 'options' => array(  | 
            ||
| 747 |                         array('id', ($fields['source'] === 'navigation' && $fields['sort'] === 'id'), __('Page ID')), | 
            ||
| 748 | array(  | 
            ||
| 749 | 'handle',  | 
            ||
| 750 | ($fields['source'] === 'navigation' && $fields['sort'] === 'handle'),  | 
            ||
| 751 |                             __('Handle') | 
            ||
| 752 | ),  | 
            ||
| 753 | array(  | 
            ||
| 754 | 'sortorder',  | 
            ||
| 755 | ($fields['source'] === 'navigation' && $fields['sort'] === 'sortorder'),  | 
            ||
| 756 |                             __('Sort Order') | 
            ||
| 757 | ),  | 
            ||
| 758 | )  | 
            ||
| 759 | ),  | 
            ||
| 760 | );  | 
            ||
| 761 | |||
| 762 |             foreach ($field_groups as $section_id => $section_data) { | 
            ||
| 763 | $optgroup = array(  | 
            ||
| 764 |                     'label' => General::sanitize($section_data['section']->get('name')), | 
            ||
| 765 |                     'data-label' => 'section-' . $section_data['section']->get('id'), | 
            ||
| 766 | 'options' => array(  | 
            ||
| 767 | array(  | 
            ||
| 768 | 'system:id',  | 
            ||
| 769 | ($fields['source'] === $section_id && $fields['sort'] === 'system:id'),  | 
            ||
| 770 |                             __('System ID') | 
            ||
| 771 | ),  | 
            ||
| 772 | array(  | 
            ||
| 773 | 'system:creation-date',  | 
            ||
| 774 | ($fields['source'] === $section_id && ($fields['sort'] === 'system:creation-date' || $fields['sort'] === 'system:date')),  | 
            ||
| 775 |                             __('System Creation Date') | 
            ||
| 776 | ),  | 
            ||
| 777 | array(  | 
            ||
| 778 | 'system:modification-date',  | 
            ||
| 779 | ($fields['source'] === $section_id && $fields['sort'] === 'system:modification-date'),  | 
            ||
| 780 |                             __('System Modification Date') | 
            ||
| 781 | ),  | 
            ||
| 782 | )  | 
            ||
| 783 | );  | 
            ||
| 784 | |||
| 785 | View Code Duplication |                 if (is_array($section_data['fields']) && !empty($section_data['fields'])) { | 
            |
| 786 |                     foreach ($section_data['fields'] as $input) { | 
            ||
| 787 |                         if (!$input->isSortable()) { | 
            ||
| 788 | continue;  | 
            ||
| 789 | }  | 
            ||
| 790 | |||
| 791 | $optgroup['options'][] = array(  | 
            ||
| 792 |                             $input->get('element_name'), | 
            ||
| 793 |                             ($fields['source'] === $section_id && $input->get('element_name') === $fields['sort']), | 
            ||
| 794 |                             $input->get('label') | 
            ||
| 795 | );  | 
            ||
| 796 | }  | 
            ||
| 797 | }  | 
            ||
| 798 | |||
| 799 | $options[] = $optgroup;  | 
            ||
| 800 | }  | 
            ||
| 801 | |||
| 802 |             $label->appendChild(Widget::Select('fields[sort]', $options)); | 
            ||
| 803 | $div->appendChild($label);  | 
            ||
| 804 | |||
| 805 |             $label = Widget::Label(__('Sort Order')); | 
            ||
| 806 |             $label->setAttribute('class', 'ds-param'); | 
            ||
| 807 | |||
| 808 |             $input = Widget::Input('fields[order]', $fields['order'], 'text', array( | 
            ||
| 809 |                 'placeholder' => __('{$param}'), | 
            ||
| 810 | 'data-search-types' => 'parameters',  | 
            ||
| 811 |                 'data-trigger' => '{$' | 
            ||
| 812 | ));  | 
            ||
| 813 | $label->appendChild($input);  | 
            ||
| 814 | $div->appendChild($label);  | 
            ||
| 815 | |||
| 816 |             $orders = new XMLElement('ul'); | 
            ||
| 817 |             $orders->setAttribute('class', 'tags singular'); | 
            ||
| 818 |             $orders->setAttribute('data-interactive', 'data-interactive'); | 
            ||
| 819 |             $orders->appendChild(new XMLElement('li', 'asc')); | 
            ||
| 820 |             $orders->appendChild(new XMLElement('li', 'desc')); | 
            ||
| 821 |             $orders->appendChild(new XMLElement('li', 'random')); | 
            ||
| 822 | $div->appendChild($orders);  | 
            ||
| 823 | |||
| 824 | $fieldset->appendChild($div);  | 
            ||
| 825 | $this->Form->appendChild($fieldset);  | 
            ||
| 826 | |||
| 827 | // Grouping  | 
            ||
| 828 |             $fieldset = new XMLElement('fieldset'); | 
            ||
| 829 |             $this->setContext($fieldset, array('sections', 'authors')); | 
            ||
| 830 |             $fieldset->appendChild(new XMLElement('legend', __('Grouping'))); | 
            ||
| 831 | |||
| 832 |             $label = Widget::Label(__('Group By')); | 
            ||
| 833 | $options = array(  | 
            ||
| 834 |                 array('', null, __('None')), | 
            ||
| 835 | );  | 
            ||
| 836 | |||
| 837 |             foreach ($field_groups as $section_id => $section_data) { | 
            ||
| 838 | $optgroup = array(  | 
            ||
| 839 |                     'label' => $section_data['section']->get('name'), | 
            ||
| 840 |                     'data-label' => 'section-' . $section_data['section']->get('id'), | 
            ||
| 841 | 'options' => array()  | 
            ||
| 842 | );  | 
            ||
| 843 | |||
| 844 | View Code Duplication |                 if (is_array($section_data['fields']) && !empty($section_data['fields'])) { | 
            |
| 845 |                     foreach ($section_data['fields'] as $input) { | 
            ||
| 846 |                         if (!$input->allowDatasourceOutputGrouping()) { | 
            ||
| 847 | continue;  | 
            ||
| 848 | }  | 
            ||
| 849 | |||
| 850 | $optgroup['options'][] = array(  | 
            ||
| 851 |                             $input->get('id'), | 
            ||
| 852 |                             ($fields['source'] === $section_id && $fields['group'] === $input->get('id')), | 
            ||
| 853 |                             $input->get('label') | 
            ||
| 854 | );  | 
            ||
| 855 | }  | 
            ||
| 856 | }  | 
            ||
| 857 | |||
| 858 | $options[] = $optgroup;  | 
            ||
| 859 | }  | 
            ||
| 860 | |||
| 861 |             $label->appendChild(Widget::Select('fields[group]', $options)); | 
            ||
| 862 | $fieldset->appendChild($label);  | 
            ||
| 863 | |||
| 864 | $this->Form->appendChild($fieldset);  | 
            ||
| 865 | |||
| 866 | // Pagination  | 
            ||
| 867 |             $fieldset = new XMLElement('fieldset'); | 
            ||
| 868 |             $this->setContext($fieldset, array('sections')); | 
            ||
| 869 |             $fieldset->appendChild(new XMLElement('legend', __('Pagination'))); | 
            ||
| 870 | |||
| 871 |             $p = new XMLElement('p', | 
            ||
| 872 |                 __('Use %s syntax to limit by page parameters.', array( | 
            ||
| 873 |                     '<code>{' . __('$param') . '}</code>' | 
            ||
| 874 | ))  | 
            ||
| 875 | );  | 
            ||
| 876 |             $p->setAttribute('class', 'help'); | 
            ||
| 877 | $fieldset->appendChild($p);  | 
            ||
| 878 | |||
| 879 |             $group = new XMLElement('div'); | 
            ||
| 880 |             $group->setAttribute('class', 'two columns pagination'); | 
            ||
| 881 | |||
| 882 |             $label = Widget::Label(__('Entries per Page')); | 
            ||
| 883 |             $label->setAttribute('class', 'column ds-param'); | 
            ||
| 884 |             $input = Widget::Input('fields[max_records]', isset($fields['max_records']) ? $fields['max_records'] : '10', | 
            ||
| 885 | 'text', array(  | 
            ||
| 886 |                     'placeholder' => __('{$param}'), | 
            ||
| 887 | 'data-search-types' => 'parameters',  | 
            ||
| 888 |                     'data-trigger' => '{$' | 
            ||
| 889 | ));  | 
            ||
| 890 | $label->appendChild($input);  | 
            ||
| 891 | $group->appendChild($label);  | 
            ||
| 892 | |||
| 893 |             $label = Widget::Label(__('Page Number')); | 
            ||
| 894 |             $label->setAttribute('class', 'column ds-param'); | 
            ||
| 895 |             $input = Widget::Input('fields[page_number]', $fields['page_number'], 'text', array( | 
            ||
| 896 |                 'placeholder' => __('{$param}'), | 
            ||
| 897 | 'data-search-types' => 'parameters',  | 
            ||
| 898 |                 'data-trigger' => '{$' | 
            ||
| 899 | ));  | 
            ||
| 900 | $label->appendChild($input);  | 
            ||
| 901 | $group->appendChild($label);  | 
            ||
| 902 | |||
| 903 | $fieldset->appendChild($group);  | 
            ||
| 904 | |||
| 905 |             $label = Widget::Checkbox('fields[paginate_results]', $fields['paginate_results'], __('Enable pagination')); | 
            ||
| 906 | $fieldset->appendChild($label);  | 
            ||
| 907 | $this->Form->appendChild($fieldset);  | 
            ||
| 908 | |||
| 909 | // Content  | 
            ||
| 910 |             $fieldset = new XMLElement('fieldset'); | 
            ||
| 911 |             $this->setContext($fieldset, array('sections', 'authors')); | 
            ||
| 912 |             $fieldset->appendChild(new XMLElement('legend', __('Content'))); | 
            ||
| 913 | |||
| 914 | // XML  | 
            ||
| 915 |             $group = new XMLElement('div', null, array('class' => 'two columns')); | 
            ||
| 916 | |||
| 917 |             $label = Widget::Label(__('Included Elements')); | 
            ||
| 918 |             $label->setAttribute('class', 'column'); | 
            ||
| 919 | |||
| 920 | $options = array(  | 
            ||
| 921 | array(  | 
            ||
| 922 |                     'label' => __('Authors'), | 
            ||
| 923 | 'data-label' => 'authors',  | 
            ||
| 924 | 'options' => array(  | 
            ||
| 925 | array(  | 
            ||
| 926 | 'username',  | 
            ||
| 927 |                             ($fields['source'] === 'authors' && in_array('username', $fields['xml_elements'])), | 
            ||
| 928 | 'username'  | 
            ||
| 929 | ),  | 
            ||
| 930 | array(  | 
            ||
| 931 | 'name',  | 
            ||
| 932 |                             ($fields['source'] === 'authors' && in_array('name', $fields['xml_elements'])), | 
            ||
| 933 | 'name'  | 
            ||
| 934 | ),  | 
            ||
| 935 | array(  | 
            ||
| 936 | 'email',  | 
            ||
| 937 |                             ($fields['source'] === 'authors' && in_array('email', $fields['xml_elements'])), | 
            ||
| 938 | 'email'  | 
            ||
| 939 | ),  | 
            ||
| 940 | array(  | 
            ||
| 941 | 'author-token',  | 
            ||
| 942 |                             ($fields['source'] === 'authors' && in_array('author-token', $fields['xml_elements'])), | 
            ||
| 943 | 'author-token'  | 
            ||
| 944 | ),  | 
            ||
| 945 | array(  | 
            ||
| 946 | 'default-area',  | 
            ||
| 947 |                             ($fields['source'] === 'authors' && in_array('default-area', $fields['xml_elements'])), | 
            ||
| 948 | 'default-area'  | 
            ||
| 949 | ),  | 
            ||
| 950 | )  | 
            ||
| 951 | ),  | 
            ||
| 952 | );  | 
            ||
| 953 | |||
| 954 |             foreach ($field_groups as $section_id => $section_data) { | 
            ||
| 955 | $optgroup = array(  | 
            ||
| 956 |                     'label' => General::sanitize($section_data['section']->get('name')), | 
            ||
| 957 |                     'data-label' => 'section-' . $section_data['section']->get('id'), | 
            ||
| 958 | 'options' => array(  | 
            ||
| 959 | array(  | 
            ||
| 960 | 'system:pagination',  | 
            ||
| 961 |                             ($fields['source'] === $section_id && in_array('system:pagination', | 
            ||
| 962 | $fields['xml_elements'])),  | 
            ||
| 963 | 'system: pagination'  | 
            ||
| 964 | ),  | 
            ||
| 965 | array(  | 
            ||
| 966 | 'system:date',  | 
            ||
| 967 |                             ($fields['source'] === $section_id && in_array('system:date', $fields['xml_elements'])), | 
            ||
| 968 | 'system: date'  | 
            ||
| 969 | )  | 
            ||
| 970 | )  | 
            ||
| 971 | );  | 
            ||
| 972 | |||
| 973 |                 if (is_array($section_data['fields']) && !empty($section_data['fields'])) { | 
            ||
| 974 |                     foreach ($section_data['fields'] as $field) { | 
            ||
| 975 | $elements = $field->fetchIncludableElements();  | 
            ||
| 976 | |||
| 977 |                         if (is_array($elements) && !empty($elements)) { | 
            ||
| 978 |                             foreach ($elements as $name) { | 
            ||
| 979 | $selected = false;  | 
            ||
| 980 | |||
| 981 |                                 if ($fields['source'] === $section_id && in_array($name, $fields['xml_elements'])) { | 
            ||
| 982 | $selected = true;  | 
            ||
| 983 | }  | 
            ||
| 984 | |||
| 985 | $optgroup['options'][] = array($name, $selected, $name);  | 
            ||
| 986 | }  | 
            ||
| 987 | }  | 
            ||
| 988 | }  | 
            ||
| 989 | }  | 
            ||
| 990 | |||
| 991 | $options[] = $optgroup;  | 
            ||
| 992 | }  | 
            ||
| 993 | |||
| 994 |             $label->appendChild(Widget::Select('fields[xml_elements][]', $options, array('multiple' => 'multiple'))); | 
            ||
| 995 | $group->appendChild($label);  | 
            ||
| 996 | |||
| 997 | // Support multiple parameters  | 
            ||
| 998 |             if (!isset($fields['param'])) { | 
            ||
| 999 | $fields['param'] = array();  | 
            ||
| 1000 |             } elseif (!is_array($fields['param'])) { | 
            ||
| 1001 | $fields['param'] = array($fields['param']);  | 
            ||
| 1002 | }  | 
            ||
| 1003 | |||
| 1004 |             $label = Widget::Label(__('Parameters')); | 
            ||
| 1005 |             $label->setAttribute('class', 'column'); | 
            ||
| 1006 |             $prefix = '$ds-' . (isset($this->_context['handle']) ? Lang::createHandle($fields['name']) : __('untitled')) . '.'; | 
            ||
| 1007 | |||
| 1008 | $options = array(  | 
            ||
| 1009 |                 array('label' => __('Authors'), 'data-label' => 'authors', 'options' => array()) | 
            ||
| 1010 | );  | 
            ||
| 1011 | |||
| 1012 |             foreach (array('id', 'username', 'name', 'email', 'user_type') as $p) { | 
            ||
| 1013 | $options[0]['options'][] = array(  | 
            ||
| 1014 | $p,  | 
            ||
| 1015 | ($fields['source'] === 'authors' && in_array($p, $fields['param'])),  | 
            ||
| 1016 | $prefix . $p,  | 
            ||
| 1017 | null,  | 
            ||
| 1018 | null,  | 
            ||
| 1019 | array(  | 
            ||
| 1020 | 'data-handle' => $p  | 
            ||
| 1021 | )  | 
            ||
| 1022 | );  | 
            ||
| 1023 | }  | 
            ||
| 1024 | |||
| 1025 |             foreach ($field_groups as $section_id => $section_data) { | 
            ||
| 1026 | $optgroup = array(  | 
            ||
| 1027 |                     'label' => $section_data['section']->get('name'), | 
            ||
| 1028 |                     'data-label' => 'section-' . $section_data['section']->get('id'), | 
            ||
| 1029 | 'options' => array()  | 
            ||
| 1030 | );  | 
            ||
| 1031 | |||
| 1032 |                 foreach (array('id', 'creation-date', 'modification-date', 'author') as $p) { | 
            ||
| 1033 | $option = array(  | 
            ||
| 1034 | 'system:' . $p,  | 
            ||
| 1035 |                         ($fields['source'] === $section_id && in_array('system:' . $p, $fields['param'])), | 
            ||
| 1036 | $prefix . 'system-' . $p,  | 
            ||
| 1037 | null,  | 
            ||
| 1038 | null,  | 
            ||
| 1039 | array(  | 
            ||
| 1040 | 'data-handle' => 'system-' . $p  | 
            ||
| 1041 | )  | 
            ||
| 1042 | );  | 
            ||
| 1043 | |||
| 1044 | // Handle 'system:date' as an output paramater (backwards compatibility)  | 
            ||
| 1045 |                     if ($p === 'creation-date') { | 
            ||
| 1046 |                         if ($fields['source'] === $section_id && in_array('system:date', $fields['param'])) { | 
            ||
| 1047 | $option[1] = true;  | 
            ||
| 1048 | }  | 
            ||
| 1049 | }  | 
            ||
| 1050 | |||
| 1051 | $optgroup['options'][] = $option;  | 
            ||
| 1052 | }  | 
            ||
| 1053 | |||
| 1054 |                 if (is_array($section_data['fields']) && !empty($section_data['fields'])) { | 
            ||
| 1055 |                     foreach ($section_data['fields'] as $input) { | 
            ||
| 1056 |                         if (!$input->allowDatasourceParamOutput()) { | 
            ||
| 1057 | continue;  | 
            ||
| 1058 | }  | 
            ||
| 1059 | |||
| 1060 | $optgroup['options'][] = array(  | 
            ||
| 1061 |                             $input->get('element_name'), | 
            ||
| 1062 |                             ($fields['source'] === $section_id && in_array($input->get('element_name'), | 
            ||
| 1063 | $fields['param'])),  | 
            ||
| 1064 |                             $prefix . $input->get('element_name'), | 
            ||
| 1065 | null,  | 
            ||
| 1066 | null,  | 
            ||
| 1067 | array(  | 
            ||
| 1068 |                                 'data-handle' => $input->get('element_name') | 
            ||
| 1069 | )  | 
            ||
| 1070 | );  | 
            ||
| 1071 | }  | 
            ||
| 1072 | }  | 
            ||
| 1073 | |||
| 1074 | $options[] = $optgroup;  | 
            ||
| 1075 | }  | 
            ||
| 1076 | |||
| 1077 |             $label->appendChild(Widget::Select('fields[param][]', $options, array('multiple' => 'multiple'))); | 
            ||
| 1078 | $group->appendChild($label);  | 
            ||
| 1079 | |||
| 1080 | $fieldset->appendChild($group);  | 
            ||
| 1081 | |||
| 1082 | // Associations  | 
            ||
| 1083 |             $label = Widget::Checkbox('fields[associated_entry_counts]', $fields['associated_entry_counts'], | 
            ||
| 1084 |                 __('Include a count of entries in associated sections')); | 
            ||
| 1085 |             $this->setContext($label, array('sections')); | 
            ||
| 1086 | $fieldset->appendChild($label);  | 
            ||
| 1087 | |||
| 1088 | // Encoding  | 
            ||
| 1089 |             $label = Widget::Checkbox('fields[html_encode]', $fields['html_encode'], __('HTML-encode text')); | 
            ||
| 1090 |             $this->setContext($label, array('sections')); | 
            ||
| 1091 | $fieldset->appendChild($label);  | 
            ||
| 1092 | |||
| 1093 | $this->Form->appendChild($fieldset);  | 
            ||
| 1094 | |||
| 1095 | // Static XML  | 
            ||
| 1096 |             if (!isset($fields['static_xml'])) { | 
            ||
| 1097 | $fields['static_xml'] = null;  | 
            ||
| 1098 | }  | 
            ||
| 1099 | |||
| 1100 |             $fieldset = new XMLElement('fieldset'); | 
            ||
| 1101 |             $this->setContext($fieldset, array('static-xml')); | 
            ||
| 1102 |             $fieldset->appendChild(new XMLElement('legend', __('Static XML'))); | 
            ||
| 1103 |             $p = new XMLElement('p', __('Enter valid XML, exclude XML declaration')); | 
            ||
| 1104 |             $p->setAttribute('class', 'help'); | 
            ||
| 1105 | $fieldset->appendChild($p);  | 
            ||
| 1106 | |||
| 1107 | $label = Widget::Label();  | 
            ||
| 1108 |             $label->appendChild(Widget::Textarea('fields[static_xml]', 12, 50, | 
            ||
| 1109 | General::sanitize(stripslashes($fields['static_xml'])),  | 
            ||
| 1110 |                 array('class' => 'code', 'placeholder' => '<static>content</static>'))); | 
            ||
| 1111 | |||
| 1112 |             if (isset($this->_errors['static_xml'])) { | 
            ||
| 1113 | $fieldset->appendChild(Widget::Error($label, $this->_errors['static_xml']));  | 
            ||
| 1114 |             } else { | 
            ||
| 1115 | $fieldset->appendChild($label);  | 
            ||
| 1116 | }  | 
            ||
| 1117 | |||
| 1118 | $this->Form->appendChild($fieldset);  | 
            ||
| 1119 | |||
| 1120 | // Connections  | 
            ||
| 1121 |             $fieldset = new XMLElement('fieldset'); | 
            ||
| 1122 |             $fieldset->setAttribute('class', 'settings'); | 
            ||
| 1123 |             $fieldset->appendChild(new XMLElement('legend', __('Attach to Pages'))); | 
            ||
| 1124 |             $p = new XMLElement('p', __('The data will only be available on the selected pages.')); | 
            ||
| 1125 |             $p->setAttribute('class', 'help'); | 
            ||
| 1126 | $fieldset->appendChild($p);  | 
            ||
| 1127 | |||
| 1128 |             $div = new XMLElement('div'); | 
            ||
| 1129 |             $label = Widget::Label(__('Pages')); | 
            ||
| 1130 | |||
| 1131 | $pages = PageManager::fetch();  | 
            ||
| 1132 |             $ds_handle = str_replace('-', '_', Lang::createHandle($fields['name'])); | 
            ||
| 1133 | $connections = ResourceManager::getAttachedPages(ResourceManager::RESOURCE_TYPE_DS, $ds_handle);  | 
            ||
| 1134 | $selected = array();  | 
            ||
| 1135 | |||
| 1136 |             foreach ($connections as $connection) { | 
            ||
| 1137 | $selected[] = $connection['id'];  | 
            ||
| 1138 | }  | 
            ||
| 1139 | |||
| 1140 | $options = array();  | 
            ||
| 1141 | |||
| 1142 | View Code Duplication |             foreach ($pages as $page) { | 
            |
| 1143 | $options[] = array(  | 
            ||
| 1144 | $page['id'],  | 
            ||
| 1145 | in_array($page['id'], $selected),  | 
            ||
| 1146 | PageManager::resolvePageTitle($page['id'])  | 
            ||
| 1147 | );  | 
            ||
| 1148 | }  | 
            ||
| 1149 | |||
| 1150 |             $label->appendChild(Widget::Select('fields[connections][]', $options, array('multiple' => 'multiple'))); | 
            ||
| 1151 | $div->appendChild($label);  | 
            ||
| 1152 | |||
| 1153 | $fieldset->appendChild($div);  | 
            ||
| 1154 | $this->Form->appendChild($fieldset);  | 
            ||
| 1155 | |||
| 1156 | |||
| 1157 | // Call the provided datasources to let them inject their filters  | 
            ||
| 1158 | // @todo Ideally when a new Datasource is chosen an AJAX request will fire  | 
            ||
| 1159 | // to get the HTML from the extension. This is hardcoded for now into  | 
            ||
| 1160 | // creating a 'big' page and then hiding the fields with JS  | 
            ||
| 1161 |             if (!empty($providers)) { | 
            ||
| 1162 |                 foreach ($providers as $providerClass => $provider) { | 
            ||
| 1163 | call_user_func_array(array($providerClass, 'buildEditor'),  | 
            ||
| 1164 | array($this->Form, &$this->_errors, $fields, $handle));  | 
            ||
| 1165 | }  | 
            ||
| 1166 | }  | 
            ||
| 1167 | |||
| 1168 |             $div = new XMLElement('div'); | 
            ||
| 1169 |             $div->setAttribute('class', 'actions'); | 
            ||
| 1170 |             $div->appendChild(Widget::Input('action[save]', | 
            ||
| 1171 |                 ($isEditing ? __('Save Changes') : __('Create Data Source')), 'submit', array('accesskey' => 's'))); | 
            ||
| 1172 | |||
| 1173 | View Code Duplication |             if ($isEditing) { | 
            |
| 1174 |                 $button = new XMLElement('button', __('Delete')); | 
            ||
| 1175 | $button->setAttributeArray(array(  | 
            ||
| 1176 | 'name' => 'action[delete]',  | 
            ||
| 1177 | 'class' => 'button confirm delete',  | 
            ||
| 1178 |                     'title' => __('Delete this data source'), | 
            ||
| 1179 | 'type' => 'submit',  | 
            ||
| 1180 | 'accesskey' => 'd',  | 
            ||
| 1181 |                     'data-message' => __('Are you sure you want to delete this data source?') | 
            ||
| 1182 | ));  | 
            ||
| 1183 | $div->appendChild($button);  | 
            ||
| 1184 | }  | 
            ||
| 1185 | |||
| 1186 | $this->Form->appendChild($div);  | 
            ||
| 1187 | }  | 
            ||
| 1188 | |||
| 1817 | 
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: