Last active
September 25, 2018 16:19
-
-
Save eonarik/acfe4e3f2f90e1b3cd6b7c7ff3fc22d7 to your computer and use it in GitHub Desktop.
modx snippet requestForm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* snippet requestForm v.2.0.3 | |
* for ModX & Bootstrap ^3 | |
* | |
* @param string [type] (normal|modal|modal-sm) - form type | |
* @param string [title] - form title | |
* @param string [code] - form id | |
* @param string [prefixCode] - prefix form id | |
* @param json|array [btns] - btns properties | |
* submit | |
* title :Submit | |
* show (true|false) :true | |
* clear | |
* title :Clear | |
* show (true|false) :false | |
* cancel | |
* title :Cancel | |
* show (true|false) :false | |
* @param json|array [formAttrs] - form attributes | |
* @param json|array [inner] - form fields | |
**/ | |
$type = isset($type) ? $type : 'normal'; | |
$title = isset($title) ? $title : 'Request form'; | |
$code = isset($code) ? $code : 'requestForm'; | |
$prefixCode = isset($prefixCode) ? $prefixCode : ''; | |
if (!empty($btns) && !is_array($btns)) | |
{ | |
$btns = json_decode($btns, true); | |
} | |
$btns = array( | |
'submit' => array_merge(array( | |
'title' => 'Submit', | |
'classes' => 'btn btn-primary', | |
'show' => true | |
), (array)$btns['submit']), | |
'clear' => array_merge(array( | |
'title' => 'Clear', | |
'classes' => 'btn btn-default', | |
'show' => false | |
), (array)$btns['clear']), | |
'cancel' => array_merge(array( | |
'title' => 'Cancel', | |
'classes' => 'btn btn-default', | |
'show' => false | |
), (array)$btns['cancel']), | |
); | |
$formAttrs = isset($formAttrs) ? $formAttrs : (isset($formAttributes) ? $formAttributes : array('action' => '')); | |
$inner = isset($inner) ? $inner : array(); | |
if( | |
!empty($inner) | |
AND !is_array($inner) | |
) | |
{ | |
$inner = json_decode($inner, true); | |
} | |
$tmp = $inner; | |
$inner = array(); | |
foreach ($tmp as $key=>$field) | |
{ | |
$field['name'] = $key; | |
if(empty($field['type'])) $field['type'] = 'text'; | |
$field['id'] = "{$code}_{$field['name']}"; | |
$field['class'] = "form-control {$field['classes']}". (!empty($field['required']) ? " required" : ""); | |
$tpl = array(); | |
switch ($field['type']) | |
{ | |
case 'hidden': | |
$tpl[] = "<input type=\"{$field['type']}\" name=\"{$field['name']}\" value=\"{$field['value']}\">"; | |
break; | |
case 'checkbox': | |
$tpl[] = " | |
<div class='checkbox'> | |
<label for=\"{$code}_{$field['name']}\"> | |
<input id='{$code}_{$field['name']}' type='{$field['type']}' name='{$field['name']}' value='{$field['value']}' /> | |
{$field['title']} ". (!empty($field['required']) ? '<span class="text-danger">*</span>' : '') ." | |
</label> | |
</div>"; | |
break; | |
default: | |
$tpl[] = "<div class=\"form-group\">"; | |
if (!empty($field['title'])) | |
{ | |
$tpl[] = "<label for=\"{$code}_{$field['name']}\">{$field['title']} ". (!empty($field['required']) ? '<span class="text-danger">*</span>' : '') ."</label>"; | |
} | |
if ($field['type'] == 'file') | |
{ | |
$field['name'] = $field['name'] . '[]'; | |
} | |
$attrs = array(); | |
foreach ($field as $key => $value) | |
{ | |
switch($key){ | |
case 'disabled': | |
case 'required': | |
if($value) | |
$attrs[] = "{$key}"; | |
break; | |
default: | |
if($value) | |
$attrs[] = "{$key}=\"{$value}\""; | |
} | |
} | |
switch ($field['type']) | |
{ | |
case 'dropdown': | |
$tpl[] = "<select ". implode(' ',$attrs) .">"; | |
if( | |
$field['value'] | |
AND $values = explode('||', $field['value']) | |
AND count($values) > 0 | |
) | |
{ | |
foreach($values as $value) | |
{ | |
$value = explode('==', $value); | |
$tpl[] = "<option value=\"" . ($value[1] ? $value[1] : '') . "\">{$value[0]}</option>"; | |
} | |
} | |
$tpl[] = "</select>"; | |
break; | |
case 'textarea': | |
$tpl[] = " | |
<textarea ". implode(' ',$attrs) ."></textarea>"; | |
break; | |
default: | |
$tpl[] = " | |
<input ". implode(' ',$attrs) ." />"; | |
} | |
$tpl[] = "</div>"; | |
} | |
$inner[] = implode("\n",$tpl); | |
} | |
unset($tmp); | |
$inner = implode("\n",$inner); | |
$ar = array( | |
'action' => $action | |
,'type' => $type | |
,'title' => $title | |
,'code' => $code | |
,'prefix_code' => $prefixCode | |
,'submit_title' => $submitTitle | |
,'inner' => $inner | |
,'hidden' => $hidden | |
); | |
switch ($ar['type']) | |
{ | |
case 'normal': | |
$isModal = false; | |
break; | |
case 'modal': | |
$isModal = $ar['type']=='modal'; | |
break; | |
default: | |
if (strpos($ar['type'], 'modal') !== false) | |
{ | |
$isModal = true; | |
$typeClass = $ar['type']; | |
} | |
} | |
$attrs = array(); | |
if (!is_array($formAttrs)) | |
{ | |
$formAttrs = json_decode($formAttrs, true); | |
} | |
foreach ($formAttrs as $key => $attr) | |
{ | |
$attrs[] = "{$key}=\"{$attr}\""; | |
} | |
$tpl = array(); | |
$tpl[] = "<div id=\"{$ar['prefix_code']}{$ar['code']}\"".($isModal ? " class=\"modal fade\"":"") .">"; | |
$tpl[] = "<form ". implode(' ', $attrs) . ' ' . ($isModal ? " class=\"modal-dialog ". ($typeClass) ."\"" : "") .">"; | |
if ($isModal) | |
{ | |
$tpl[] = " | |
<div class=\"modal-content\"> | |
<div class=\"modal-header\"> | |
<button type=\"button\" class=\"close\" data-dismiss=\"modal\" aria-hidden=\"true\">×</button> | |
<h3 class=\"modal-title\">{$ar['title']}</h3> | |
</div> | |
<div class=\"modal-body af-success-message\">"; | |
} | |
else | |
{ | |
$tpl[] = ' | |
<h3 class="modal-title">'. $ar['title'] .'</h3> | |
<div class="af-success-message">'; | |
} | |
$tpl[] = $ar['inner']; | |
if ($isModal) | |
{ | |
$tpl[] = " | |
</div> | |
<div class=\"modal-footer text-right\">"; | |
if(!empty($btns['cancel']) && $btns['cancel']['show']) | |
$tpl[] = "<button type=\"button\" data-dismiss=\"modal\" class=\"{$btns['cancel']['classes']}\">{$btns['cancel']['title']}</button>"; | |
if(!empty($btns['submit']) && $btns['submit']['show']) | |
{ | |
$attrs = array(); | |
if (!empty($btns['submit']['attributes'])) | |
{ | |
foreach ($btns['submit']['attributes'] as $key => $value) | |
{ | |
$attrs[] = "{$key}=\"{$value}\""; | |
} | |
} | |
$tpl[] = "<button type=\"submit\" class=\"{$btns['submit']['classes']}\"" . implode(' ', $attrs) . ">{$btns['submit']['title']}</button>"; | |
} | |
$tpl[] = " | |
</div> | |
</div>"; | |
} | |
else | |
{ | |
if (!empty($btns['clear']) && $btns['clear']['show']) | |
{ | |
$tpl[] = "<button type=\"reset\" class=\"{$btns['clear']['classes']}\">{$btns['clear']['title']}</button>"; | |
} | |
if (!empty($btns['submit']) && $btns['submit']['show']) | |
{ | |
$attrs = array(); | |
if (!empty($btns['submit']['attributes'])) | |
{ | |
foreach ($btns['submit']['attributes'] as $key => $value) | |
{ | |
$attrs[] = "{$key}=\"{$value}\""; | |
} | |
} | |
$tpl[] = "<button type=\"submit\" class=\"{$btns['submit']['classes']}\" " . implode(' ', $attrs) . ">{$btns['submit']['title']}</button>"; | |
} | |
$tpl[] = "</div>"; | |
} | |
$tpl[] = "</form>"; | |
$tpl[] = "</div>"; | |
$out[] = implode("\n",$tpl); | |
$out = implode("\n",$out); | |
return $out; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment