Esempio n. 1
0
 public function process()
 {
     //require_once dirname(dirname(dirname(__file__))) . '/model/migx/migx.class.php';
     //$migx = new Migx($this->modx);
     $modx =& $this->modx;
     $corePath = dirname(dirname(dirname(dirname(__FILE__)))) . '/';
     require_once $corePath . 'model/chunkie/chunkie.class.inc.php';
     //require_once $modx->getOption('core_path') . 'model/modx/modtemplatevar.class.php';
     require_once $corePath . '/model/migxangular/modtemplatevarinputrendermigxangular.class.php';
     $template = '@FILE form.tpl';
     $controller = new migxangularChunkie($template, $corePath . 'templates/web/form/');
     $scriptProperties = $this->getProperties();
     // special actions, for example the selectFromGrid - action
     $tempParams = $this->modx->getOption('tempParams', $scriptProperties, '');
     $action = '';
     if (!empty($tempParams)) {
         $tempParams = $this->modx->fromJson($tempParams);
         if (array_key_exists('action', $tempParams) && !empty($tempParams['action'])) {
             $action = strtolower($tempParams['action']);
             if ($action == 'selectfromgrid') {
                 $scriptProperties['configs'] = !empty($tempParams['selectorconfig']) ? $tempParams['selectorconfig'] : $action;
             }
             $action = '_' . $action;
         }
     }
     //$controller->loadControllersPath();
     // we will need a way to get a context-key, if in CMP-mode, from config, from dataset..... thoughts??
     // can be overridden in custom-processors for now, but whats with the preparegrid-method and working-context?
     // ok let's see when we need this.
     $this->modx->migxangular->working_context = 'web';
     if ($this->modx->resource = $this->modx->getObject('modResource', $scriptProperties['resource_id'])) {
         $this->modx->migxangular->working_context = $this->modx->resource->get('context_key');
         //$_REQUEST['id']=$scriptProperties['resource_id'];
     }
     // get properties from TV, if tv_name was given
     if ($tv = $this->modx->getObject('modTemplateVar', array('name' => $scriptProperties['tv_name']))) {
         $this->modx->migx->source = $tv->getSource($this->modx->migxangular->working_context, false);
         $properties = $tv->get('input_properties');
         //$properties = isset($properties['formtabs']) ? $properties : $tv->getProperties();
     }
     //$controller->setPlaceholder('_config', $this->modx->config);
     $task = $this->modx->migxangular->getTask();
     $filename = str_replace(array('.class', '.php'), '', basename(__FILE__)) . $action . '.php';
     $processorspath = $this->modx->migxangular->config['migxCorePath'] . 'processors/mgr/';
     $filenames = array();
     if ($processor_file = $this->modx->migxangular->findProcessor($processorspath, $filename, $filenames)) {
         include_once $processor_file;
     }
     $sender = isset($sender) ? $sender : '';
     $configs = !empty($this->modx->migxangular->config['configs']) ? $this->modx->migxangular->config['configs'] : '';
     $configs = isset($properties['configs']) && !empty($properties['configs']) ? $properties['configs'] : $configs;
     if (!empty($configs)) {
         $this->modx->migxangualr->config['configs'] = $configs;
         $this->modx->migxangular->loadConfigs(true, true, $scriptProperties, $sender);
     }
     $formtabs = $this->modx->migx->getTabs();
     $fieldid = 0;
     /*actual record */
     $record = $this->modx->fromJSON($scriptProperties['record_json']);
     $allfields = array();
     $formnames = array();
     $field = array();
     $field['field'] = 'MIGX_id';
     $field['tv_id'] = 'migxid';
     $allfields[] = $field;
     if ($scriptProperties['isnew'] == '1') {
         $migxid = $scriptProperties['autoinc'] + 1;
     } else {
         $migxid = $record['MIGX_id'];
     }
     $controller->setPlaceholder('migxid', $migxid);
     $formtabs = $this->modx->migxangular->checkMultipleForms($formtabs, $controller, $allfields, $record);
     if (empty($formtabs)) {
         //old stuff
         $default_formtabs = '[{"caption":"Default", "fields": [{"field":"title","caption":"Title"}]}]';
         $formtabs = $this->modx->fromJSON($this->modx->getOption('formtabs', $properties, $default_formtabs));
         $formtabs = empty($properties['formtabs']) ? $this->modx->fromJSON($default_formtabs) : $formtabs;
         $fieldid = 0;
         $tabid = 0;
         //multiple different Forms
         // Note: use same field-names and inputTVs in all forms
         if (isset($formtabs[0]['formtabs'])) {
             $forms = $formtabs;
             $tabs = array();
             foreach ($forms as $form) {
                 $formname = array();
                 $formname['value'] = $form['formname'];
                 $formname['text'] = $form['formname'];
                 $formname['selected'] = 0;
                 if (isset($record['MIGX_formname']) && $form['formname'] == $record['MIGX_formname']) {
                     $formname['selected'] = 1;
                 }
                 $formnames[] = $formname;
                 foreach ($form['formtabs'] as $tab) {
                     $tabs[$form['formname']][] = $tab;
                 }
             }
             $controller->setPlaceholder('formnames', $formnames);
             if (isset($record['MIGX_formname'])) {
                 $formtabs = $tabs[$record['MIGX_formname']];
             } else {
                 //if no formname requested use the first form
                 $formtabs = $tabs[$formnames[0]['value']];
             }
             $field = array();
             $field['field'] = 'MIGX_formname';
             $field['tv_id'] = 'Formname';
             $allfields[] = $field;
         }
     }
     //$fieldid = 0;
     //$allfields[] = array();
     $categories = array();
     $xtypes = array();
     $data = array();
     $controllerscripts = array();
     $template = '@FILE fields.tpl';
     $this->modx->controller = new migxangularChunkie($template, $corePath . 'templates/web/form/');
     //needed because TVs are using smarty
     //$this->modx->getService('smarty', 'smarty.modSmarty');
     //$this->modx->controller = new MigxFormController($this->modx);
     //$this->modx->controller->loadTemplatesPath();
     $this->modx->controller->setPlaceholder('_config', $this->modx->config);
     $this->modx->migxangular->createForm($formtabs, $record, $allfields, $categories, $scriptProperties, $xtypes, $data, $controllerscripts);
     $formcaption = $this->modx->migxangular->customconfigs['formcaption'];
     //collected custom TV-xtypes
     $xtypesoutput = implode('', $xtypes);
     //$innerrows['tab']='Test';<- parse categories to tabs here
     //{if count($categories) < 2 OR ($smarty.foreach.cat.first AND $category.print_before_tabs)}
     $tabsoutput = array();
     if (is_array($categories)) {
         $i = 0;
         foreach ($categories as $category) {
             $i++;
             $template = '@FILE tab.tpl';
             $parser = new migxangularChunkie($template, $corePath . 'templates/web/form/');
             $parser->createVars($category);
             $parser->setPlaceholder('innerrows.input', implode(',', $category['inputs']));
             $is_xtab = count($categories) < 2 || $i == 1 && $category['print_before_tabs'] ? '0' : '1';
             $parser->setPlaceholder('is_xtab', $is_xtab);
             //print_r($parser->getPlaceholders());
             $tabsoutput[] = $parser->render();
             $template = '@FILE tab_content.tpl';
             $parser = new migxangularChunkie($template, $corePath . 'templates/web/form/');
             $parser->createVars($category);
             $parser->setPlaceholder('innerrows.input', implode('', $category['inputs']));
             $is_xtab = count($categories) < 2 || $i == 1 && $category['print_before_tabs'] ? '0' : '1';
             $parser->setPlaceholder('is_xtab', $is_xtab);
             //print_r($parser->getPlaceholders());
             $tabscontents[] = $parser->render();
         }
     }
     $innerrows['tab'] = implode('', $tabsoutput);
     $innercounts['tab'] = count($categories);
     $innerrows['tab_content'] = implode('', $tabscontents);
     $innercounts['tab_content'] = count($categories);
     $formcaption = $this->modx->migxangular->renderChunk($formcaption, $record, false, false);
     $formcaption = addslashes($formcaption);
     $formcaption = str_replace(array("\n", "\r"), array("\\n", "\\r"), $formcaption);
     $o_array = array();
     if ($object) {
         $o_array = $object->toArray();
     }
     $o_array['id'] = empty($o_array['id']) ? 'new' : $o_array['id'];
     //add formbuttons
     $formbuttons = '[{"button":"cancel"},{"button":"doneandclosemigx"}]';
     $formbuttons = $this->modx->getOption('winbuttons', $this->modx->migxangular->customconfigs, $formbuttons);
     $formbuttons = $this->modx->fromJson($formbuttons);
     $winbuttons = array();
     foreach ($formbuttons as $button) {
         //$template = '@FILE ' . $button['button'] . '.tpl';
         //$parser = new migxangularChunkie($template, $corePath . 'templates/web/form/buttons/');
         $template = $button['button'] . '.tpl';
         $path = 'web/form/buttons/';
         $filenames = array();
         $winbutton = '';
         if ($parser = $this->modx->migxangular->createParser($template, $path, $filenames)) {
             $parser->createVars($controller->getPlaceholders());
             $winbutton = $parser->render();
         }
         //extract script, must be at the end of file and start with '<script>'
         $winbutton = explode('<script>', $winbutton);
         $winbuttons[] = $winbutton[0];
         //remove closing '</script>'
         if (isset($winbutton[1])) {
             $controllerscripts[] = str_replace('</script>', '', $winbutton[1]);
         }
     }
     $innerrows['button'] = implode('', $winbuttons);
     $innercounts['button'] = count($winbuttons);
     $innerrows['controller_scripts'] = implode('', $controllerscripts);
     $innercounts['controller_scripts'] = count($controllerscripts);
     $controller->setPlaceholder('xtypes', $xtypesoutput);
     $controller->setPlaceholder('formcaption', $formcaption);
     $controller->setPlaceholder('fields', $this->modx->toJSON($allfields));
     $controller->setPlaceholder('customconfigs', $this->modx->migxangular->customconfigs);
     $controller->setPlaceholder('object', $o_array);
     $controller->setPlaceholder('innerrows', $innerrows);
     $controller->setPlaceholder('innercounts', $innercounts);
     $controller->setPlaceholder('request', $_REQUEST);
     $controller->setPlaceholder('migxangularconfig', $this->modx->migxangular->config);
     $controller->setPlaceholder('OnMigxfeFormPrerender', '');
     $controller->setPlaceholder('OnMigxfeFormRender', '');
     //$controller->setPlaceholder('win_id', $scriptProperties['tv_id']);
     $controller->setPlaceholder('win_id', isset($this->modx->migxangular->customconfigs['win_id']) ? $this->modx->migxangular->customconfigs['win_id'] : $scriptProperties['tv_id']);
     //$c->setPlaceholder('id_update_window', 'modx-window-midb-grid-update');
     $controller->setPlaceholder('onsubmitsuccess', $this->modx->getOption('onsubmitsuccess', $this->modx->migxangular->customconfigs, ''));
     $controller->setPlaceholder('winbuttons', $this->modx->getOption('winbuttons', $this->modx->migxangular->customconfigs, $winbuttons));
     $controller->setPlaceholder('submitparams', $this->modx->getOption('submitparams', $this->modx->migxangular->customconfigs, ''));
     $tabs_js = '';
     if (count($categories) > 1) {
         $template = '@FILE tabs_js.tpl';
         $parser = new migxangularChunkie($template, $corePath . 'templates/web/form/');
         $parser->createVars($controller->getPlaceholders());
         $tabs_js = $parser->render();
     }
     $controller->setPlaceholder('tabs_js', $tabs_js);
     if (!empty($_REQUEST['showCheckbox'])) {
         $controller->setPlaceholder('showCheckbox', 1);
     }
     $output['html'] = $controller->render();
     //$output['html'] .= '<pre>' .print_r($controller->getPlaceholders(),1) .'</pre>';
     //$this->modx->getParser();
     /*parse all non-cacheable tags and remove unprocessed tags, if you want to parse only cacheable tags set param 3 as false*/
     //$this->modx->parser->processElementTags('', $output, true, true, '[[', ']]', array(), $counts);
     $output['data'] = $data;
     return $this->modx->toJson($output);
 }
 function createForm(&$tabs, &$record, &$allfields, &$categories, $scriptProperties, &$xtypes, &$data, &$controllerscripts)
 {
     $fieldid = 0;
     $tabid = 1;
     foreach ($tabs as $key => $tab) {
         $tvs = array();
         $inputs = array();
         $fields = is_array($tab['fields']) ? $tab['fields'] : $this->modx->fromJson($tab['fields']);
         if (is_array($fields) && count($fields) > 0) {
             foreach ($fields as &$field) {
                 if (isset($field['restrictive_condition'])) {
                     $props = $record;
                     $rc = $this->renderChunk($field['restrictive_condition'], $props, false, false);
                     if (!empty($rc)) {
                         continue;
                     }
                 }
                 $fieldid++;
                 /*generate unique tvid, must be numeric*/
                 /*todo: find a better solution*/
                 $field['tv_id'] = ($scriptProperties['tv_id'] . '99' . $fieldid) * 1;
                 if (isset($field['description_is_code']) && !empty($field['description_is_code'])) {
                     $tv = $this->modx->newObject('modTemplateVar');
                     $tv->set('description', $this->renderChunk($field['description'], $record, false, false));
                     $tv->set('type', 'description_is_code');
                     $tv->set('id', $field['tv_id']);
                     $tv->set('fieldname', $field['field']);
                 } else {
                     $params = array();
                     if (isset($field['inputTV']) && ($tv = $this->modx->getObject('modTemplateVar', array('name' => $field['inputTV'])))) {
                         $params = $tv->get('input_properties');
                     } else {
                         $tv = $this->modx->newObject('modTemplateVar');
                         $tv->set('type', !empty($field['inputTVtype']) ? $field['inputTVtype'] : 'text');
                     }
                     $tv->set('fieldname', $field['field']);
                     if (!empty($field['inputOptionValues'])) {
                         $tv->set('elements', $field['inputOptionValues']);
                     }
                     $tv->set('default_text', (string) $tv->processBindings($field['default']));
                     if ($field['default'] === '0' || !empty($field['default'])) {
                     }
                     if (!empty($field['configs'])) {
                         $cfg = $this->modx->fromJson($field['configs']);
                         if (is_array($cfg)) {
                             $params = array_merge($params, $cfg);
                         } else {
                             $params['configs'] = $field['configs'];
                         }
                     }
                     /*insert actual value from requested record, convert arrays to ||-delimeted string */
                     $fieldvalue = '';
                     if (isset($record[$field['field']])) {
                         $fieldvalue = $record[$field['field']];
                         if (is_array($fieldvalue)) {
                             $fieldvalue = is_array($fieldvalue[0]) ? $this->modx->toJson($fieldvalue) : implode('||', $fieldvalue);
                         }
                     }
                     $tv->set('value', (string) $fieldvalue);
                     if (!empty($field['caption'])) {
                         $field['caption'] = htmlentities($field['caption'], ENT_QUOTES, $this->modx->getOption('modx_charset'));
                         $tv->set('caption', $field['caption']);
                     }
                     if (!empty($field['description'])) {
                         $field['description'] = htmlentities($field['description'], ENT_QUOTES, $this->modx->getOption('modx_charset'));
                         $tv->set('description', $field['description']);
                     }
                     $field['array_tv_id'] = $field['tv_id'] . '[]';
                     $allfield = array();
                     $allfield['field'] = $field['field'];
                     $allfield['tv_id'] = $field['tv_id'];
                     $allfield['array_tv_id'] = $field['array_tv_id'];
                     $allfields[] = $allfield;
                     $mediasource = $this->getFieldSource($field, $tv);
                     $tv->setSource($mediasource);
                     $tv->set('id', $field['tv_id']);
                     /*
                     $default = $tv->processBindings($tv->get('default_text'), $resourceId);
                     if (strpos($tv->get('default_text'), '@INHERIT') > -1 && (strcmp($default, $tv->get('value')) == 0 || $tv->get('value') == null)) {
                     $tv->set('inherited', true);
                     }
                     */
                     if ($tv->get('value') == null) {
                         $v = $tv->get('default_text');
                         if ($tv->get('type') == 'checkbox' && $tv->get('value') == '') {
                             $v = '';
                         }
                         $tv->set('value', $v);
                     }
                     //$this->modx->smarty->assign('tv', $tv);
                     /* move this part into a plugin onMediaSourceGetProperties and create a mediaSource - property 'autoCreateFolder'
                                             * may be performancewise its better todo that here?
                                             
                                             if (!empty($properties['basePath'])) {
                                             if ($properties['autoResourceFolders'] == 'true') {
                                             $params['basePath'] = $basePath . $scriptProperties['resource_id'] . '/';
                                             $targetDir = $params['basePath'];
                     
                                             $cacheManager = $this->modx->getCacheManager();
                                             // if directory doesnt exist, create it 
                                             if (!file_exists($targetDir) || !is_dir($targetDir)) {
                                             if (!$cacheManager->writeTree($targetDir)) {
                                             $this->modx->log(modX::LOG_LEVEL_ERROR, '[MIGX] Could not create directory: ' . $targetDir);
                                             return $this->modx->error->failure('Could not create directory: ' . $targetDir);
                                             }
                                             }
                                             // make sure directory is readable/writable 
                                             if (!is_readable($targetDir) || !is_writable($targetDir)) {
                                             $this->modx->log(xPDO::LOG_LEVEL_ERROR, '[MIGX] Could not write to directory: ' . $targetDir);
                                             return $this->modx->error->failure('Could not write to directory: ' . $targetDir);
                                             }
                                             } else {
                                             $params['basePath'] = $basePath;
                                             }
                                             }
                                             */
                     if (!isset($params['allowBlank'])) {
                         $params['allowBlank'] = 1;
                     }
                     $value = $tv->get('value');
                     if ($value === null) {
                         $value = $tv->get('default_text');
                     }
                     //$this->modx->smarty->assign('params', $params);
                     /* find the correct renderer for the TV, if not one, render a textbox */
                     //Todo: Create plugin-event OnTVInputRenderListMIGXangular to get custom TV-rendererPaths
                     //$inputRenderPaths = $tv->getRenderDirectories('OnTVInputRenderList', 'input');
                     //print_r($inputRenderPaths);
                     $config = $this->customconfigs;
                     $packageName = $this->modx->getOption('packageName', $config);
                     $inputRenderPaths = $this->config['corePath'] . 'elements/tv/input/';
                     $inputRenderPaths = explode(',', $inputRenderPaths);
                     if (!empty($packageName)) {
                         $customPaths = array();
                         $custompath = $this->modx->getOption('core_path') . 'components/' . $packageName . '/' . 'elements/migxangular/tv/input/';
                         $customPaths[] = $custompath;
                         $inputRenderPaths = array_merge($customPaths, $inputRenderPaths);
                     }
                     $type = $tv->get('type');
                     $method = 'input';
                     if ($className = $tv->checkForRegisteredRenderMethod($type, $method)) {
                     } else {
                         $typefound = false;
                         foreach ($inputRenderPaths as $path) {
                             $renderFile = $path . $type . '.class.php';
                             if (file_exists($renderFile)) {
                                 $className = (include $renderFile);
                                 $tv->registerRenderMethod($type, $method, $className);
                                 if (class_exists($className)) {
                                     /**
                                      *  *  *  *  *  *  *  * @var modTemplateVarOutputRender $render */
                                     $typefound = true;
                                 }
                                 break;
                             }
                         }
                         if (!$typefound) {
                             $tv->set('type', 'text');
                         }
                     }
                     $inputForm = $tv->getRender($params, $value, $inputRenderPaths, 'input', null, $tv->get('type'));
                     $this->modx->getParser();
                     /*parse all non-cacheable tags and remove unprocessed tags, if you want to parse only cacheable tags set param 3 as false*/
                     $this->modx->parser->processElementTags('', $inputForm, true, true, '[[', ']]', array(), $counts);
                     //echo '<input type="hidden" id="original' . $_REQUEST['win_id'] . '-' . $tv->get('id') . '" name="original' . $tv->get('id') . '" value="' . htmlspecialchars($tv->get('value'), ENT_QUOTES) . '" />';
                     //extract script, must be at the end of file and start with '<script>'
                     $parts = explode('<script>', $inputForm);
                     $inputForm = $parts[0];
                     //remove closing '</script>'
                     if (isset($parts[1])) {
                         $controllerscripts[] = str_replace('</script>', '', $parts[1]);
                     }
                     $data[$field['field']] = $tv->get('value');
                     if (is_array($xtypes)) {
                         $xtype = $tv->get('xtype_template');
                         if (file_exists($xtype)) {
                             $template = '@FILE ' . $xtype;
                             $parser = new migxangularChunkie($template, '', false);
                             //print_r($parser->getPlaceholders());
                             $xtypes[$tv->get('xtype_template')] = $parser->render();
                         }
                     }
                     if (empty($inputForm)) {
                         continue;
                     }
                     $tv->set('formElement', $inputForm);
                     $inputs[] = $inputForm;
                 }
                 $tvs[] = $tv->toArray();
             }
         }
         $cat = array();
         $cat['category'] = $tab['caption'];
         $cat['print_before_tabs'] = isset($tab['print_before_tabs']) && !empty($tab['print_before_tabs']) ? true : false;
         $cat['id'] = $tabid;
         $cat['tvs'] = $tvs;
         $cat['inputs'] = $inputs;
         $cat['request'] = $_REQUEST;
         $categories[] = $cat;
         $tabid++;
     }
 }