/** * Add calculations * * @param array $a of field elements $a * @param string &$str to out put as csv file $str * * @return null */ protected function addCalculations($a, &$str) { $input = $this->app->input; if ($input->get('inccalcs') == 1) { $incRaw = $input->get('incraw', true); $calKeys = array('sums', 'avgs', 'medians', 'count'); foreach ($calKeys as $calKey) { $calculations[$calKey] = FArrayHelper::array_fill(0, count($a) + 1, ' '); $calculations[$calKey][0] = $calKey; $calcs = $this->model->getCalculations(); foreach ($calcs[$calKey] as $key => $cal) { $x = 0; $found = false; // $$$rob if grouped data and calc split then get the formatted string as $cal['calc] wont exist below foreach ($a as $aKey => $aVal) { if (trim($aKey) == trim($key) && $x != 0) { $json = $calcs[$calKey][$aKey . '_obj']; unset($json['']); if (count($json) == 1) { $default = $json['Total']->value; } else { $default = json_encode($json); } } $x++; } $x = 0; foreach ($a as $aKey => $aVal) { if ($aKey == JString::substr($key, 0, JString::strlen($key) - 4) && $x != 0) { $found = true; break; } $x++; } if ($found) { if (array_key_exists('calc', $cal)) { $calculations[$calKey][$x] = $cal['calc']->value; if ($incRaw) { $calculations[$calKey][$x + 1] = $cal['calc']->value; } } else { $calculations[$calKey][$x] = $default; if ($incRaw) { $calculations[$calKey][$x + 1] = $default; } } } } $str .= implode($this->delimiter, array_map(array($this, 'quote'), $calculations[$calKey])); $str .= "\n"; } } }
/** * Returns javascript which creates an instance of the class defined in formJavascriptClass() * * @param int $repeatCounter Repeat group counter * * @return array */ public function elementJavascript($repeatCounter) { $params = $this->getParams(); $id = $this->getHTMLId($repeatCounter); FabrikHelperHTML::mcl(); $j3 = FabrikWorker::j3(); $element = $this->getElement(); $paramsKey = $this->getFullName(true, false); $paramsKey = Fabrikstring::rtrimword($paramsKey, $this->getElement()->name); $paramsKey .= 'params'; $formData = $this->getFormModel()->data; $imgParams = FArrayHelper::getValue($formData, $paramsKey); // Above paramsKey stuff looks really wonky - lets test if null and use something which seems to build the correct key if (is_null($imgParams)) { $paramsKey = $this->getFullName(true, false) . '___params'; $imgParams = FArrayHelper::getValue($formData, $paramsKey); } $value = $this->getValue(array(), $repeatCounter); $value = is_array($value) ? $value : FabrikWorker::JSONtoData($value, true); $value = $this->checkForSingleCropValue($value); // Repeat_image_repeat_image___params $rawValues = count($value) == 0 ? array() : FArrayHelper::array_fill(0, count($value), 0); $fileData = $this->getFormModel()->data; $rawKey = $this->getFullName(true, false) . '_raw'; $rawValues = FArrayHelper::getValue($fileData, $rawKey, $rawValues); if (!is_array($rawValues)) { $rawValues = explode(GROUPSPLITTER, $rawValues); } else { /* * $$$ hugh - nasty hack for now, if repeat group with simple * uploads, all raw values are in an array in $rawValues[0] */ if (array_key_exists(0, $rawValues) && is_array(FArrayHelper::getValue($rawValues, 0))) { $rawValues = $rawValues[0]; } } if (!is_array($imgParams)) { $imgParams = explode(GROUPSPLITTER, $imgParams); } $oFiles = new stdClass(); $iCounter = 0; // Failed validation for ajax upload elements if (is_array($value) && array_key_exists('id', $value)) { $imgParams = array_values($value['crop']); $value = array_keys($value['id']); /** * Another nasty hack for failed validations, need to massage $rawValues back into expected shape, * as they will be keyed by filename instead of parent ID after validation fail. */ $newRawValues = array(); foreach ($value as $k => $v) { $newRawValues[$k] = $rawValues['id'][$v]; } $rawValues = $newRawValues; } for ($x = 0; $x < count($value); $x++) { if (is_array($value)) { if (array_key_exists($x, $value) && $value[$x] !== '') { if (is_array($value[$x])) { // From failed validation foreach ($value[$x]['id'] as $tKey => $parts) { $o = new stdClass(); $o->id = 'alreadyuploaded_' . $element->id . '_' . $iCounter; $o->name = array_pop(explode(DIRECTORY_SEPARATOR, $tKey)); $o->path = $tKey; if ($fileInfo = $this->getStorage()->getFileInfo($o->path)) { $o->size = $fileInfo['filesize']; } else { $o->size = 'unknown'; } $o->type = strstr($fileInfo['mime_type'], 'image/') ? 'image' : 'file'; $o->url = $this->getStorage()->pathToURL($tKey); $o->recordid = $rawValues[$x]; $o->params = json_decode($value[$x]['crop'][$tKey]); $oFiles->{$iCounter} = $o; $iCounter++; } } else { if (is_object($value[$x])) { // Single crop image (not sure about the 0 settings in here) $parts = explode(DIRECTORY_SEPARATOR, $value[$x]->file); $o = new stdClass(); $o->id = 'alreadyuploaded_' . $element->id . '_0'; $o->name = array_pop($parts); $o->path = $value[$x]->file; if ($fileInfo = $this->getStorage()->getFileInfo($o->path)) { $o->size = $fileInfo['filesize']; } else { $o->size = 'unknown'; } $o->type = strstr($fileInfo['mime_type'], 'image/') ? 'image' : 'file'; $o->url = $this->getStorage()->pathToURL($value[$x]->file); $o->recordid = 0; $o->params = json_decode($value[$x]->params); $oFiles->{$iCounter} = $o; $iCounter++; } else { $parts = explode('/', $value[$x]); $o = new stdClass(); $o->id = 'alreadyuploaded_' . $element->id . '_' . $rawValues[$x]; $o->name = array_pop($parts); $o->path = $value[$x]; if ($fileInfo = $this->getStorage()->getFileInfo($o->path)) { $o->size = $fileInfo['filesize']; } else { $o->size = 'unknown'; } $o->type = strstr($fileInfo['mime_type'], 'image/') ? 'image' : 'file'; $o->url = $this->getStorage()->pathToURL($value[$x]); $o->recordid = $rawValues[$x]; $o->params = json_decode(FArrayHelper::getValue($imgParams, $x, '{}')); $oFiles->{$iCounter} = $o; $iCounter++; } } } } } $opts = $this->getElementJSOptions($repeatCounter); $opts->id = $this->getId(); if ($this->isJoin()) { $opts->isJoin = true; $opts->joinId = $this->getJoinModel()->getJoin()->id; } $opts->elid = $element->id; $opts->defaultImage = $params->get('default_image', ''); $opts->folderSelect = $params->get('upload_allow_folderselect', 0); $opts->quality = (double) $params->get('image_quality') / 100; $opts->dir = JPATH_SITE . '/' . $params->get('ul_directory'); $opts->ajax_upload = (bool) $params->get('ajax_upload', false); $opts->ajax_runtime = $params->get('ajax_runtime', 'html5'); $opts->ajax_silverlight_path = COM_FABRIK_LIVESITE . 'plugins/fabrik_element/fileupload/lib/plupload/js/plupload.flash.swf'; $opts->ajax_flash_path = COM_FABRIK_LIVESITE . 'plugins/fabrik_element/fileupload/lib/plupload/js/plupload.flash.swf'; $opts->max_file_size = (double) $params->get('ul_max_file_size'); $opts->device_capture = (double) $params->get('ul_device_capture'); $opts->ajax_chunk_size = (int) $params->get('ajax_chunk_size', 0); $opts->filters = $this->ajaxFileFilters(); $opts->crop = $this->canCrop(); $opts->canvasSupport = FabrikHelperHTML::canvasSupport(); $opts->modalId = $this->modalId($repeatCounter); $opts->elementName = $this->getFullName(); $opts->cropwidth = (int) $params->get('fileupload_crop_width'); $opts->cropheight = (int) $params->get('fileupload_crop_height'); $opts->ajax_max = (int) $params->get('ajax_max', 4); $opts->dragdrop = true; $icon = $j3 ? 'picture' : 'image.png'; $resize = $j3 ? 'expand-2' : 'resize.png'; $opts->previewButton = FabrikHelperHTML::image($icon, 'form', @$this->tmpl, array('alt' => FText::_('PLG_ELEMENT_FILEUPLOAD_VIEW'))); $opts->resizeButton = FabrikHelperHTML::image($resize, 'form', @$this->tmpl, array('alt' => FText::_('PLG_ELEMENT_FILEUPLOAD_RESIZE'))); $opts->files = $oFiles; $opts->winWidth = (int) $params->get('win_width', 400); $opts->winHeight = (int) $params->get('win_height', 400); $opts->elementShortName = $element->name; $opts->listName = $this->getListModel()->getTable()->db_table_name; $opts->useWIP = (bool) $params->get('upload_use_wip', '0') == '1'; $opts->page_url = COM_FABRIK_LIVESITE; JText::script('PLG_ELEMENT_FILEUPLOAD_MAX_UPLOAD_REACHED'); JText::script('PLG_ELEMENT_FILEUPLOAD_DRAG_FILES_HERE'); JText::script('PLG_ELEMENT_FILEUPLOAD_UPLOAD_ALL_FILES'); JText::script('PLG_ELEMENT_FILEUPLOAD_RESIZE'); JText::script('PLG_ELEMENT_FILEUPLOAD_CROP_AND_SCALE'); JText::script('PLG_ELEMENT_FILEUPLOAD_PREVIEW'); JText::script('PLG_ELEMENT_FILEUPLOAD_CONFIRM_SOFT_DELETE'); JText::script('PLG_ELEMENT_FILEUPLOAD_CONFIRM_HARD_DELETE'); JText::script('PLG_ELEMENT_FILEUPLOAD_FILE_TOO_LARGE_SHORT'); return array('FbFileUpload', $id, $opts); }
/** * Get the prefilter settings from list/module/menu options * Use in listModel::getPrefilterArray() and formModel::getElementIds() * * @return multitype:array */ public function prefilterSetting() { $params = $this->getParams(); $properties = $this->menuModulePrefilters(); // List pre-filter properties $listFields = (array) $params->get('filter-fields'); $listConditions = (array) $params->get('filter-conditions'); $listValue = (array) $params->get('filter-value'); $listAccess = (array) $params->get('filter-access'); $listEval = (array) $params->get('filter-eval'); $listJoins = (array) $params->get('filter-join'); $listGrouped = (array) $params->get('filter-grouped'); $listSearchType = FArrayHelper::array_fill(0, count($listJoins), 'prefilter'); /* If we are rendering as a module don't pick up the menu item options (params already set in list module) * so first statement when rendering a module, 2nd when posting to the component from a module. */ if ($properties !== '') { $prefilters = ArrayHelper::fromObject(json_decode($properties)); $conditions = (array) $prefilters['filter-conditions']; if (!empty($conditions)) { $fields = FArrayHelper::getValue($prefilters, 'filter-fields', array()); $conditions = FArrayHelper::getValue($prefilters, 'filter-conditions', array()); $values = FArrayHelper::getValue($prefilters, 'filter-value', array()); $access = FArrayHelper::getValue($prefilters, 'filter-access', array()); $eval = FArrayHelper::getValue($prefilters, 'filter-eval', array()); $joins = FArrayHelper::getValue($prefilters, 'filter-join', array()); $searchType = FArrayHelper::array_fill(0, count($joins), 'menuPrefilter'); $overrideListPrefilters = $params->get('menu_module_prefilters_override', true); if ($overrideListPrefilters) { // Original behavior $listFields = $fields; $listConditions = $conditions; $listValue = $values; $listAccess = $access; $listEval = $eval; $listJoins = $joins; $listSearchType = $searchType; } else { // Preferred behavior but for backwards compat we need to ask users to // set this option in the menu/module settings $joins[0] = 'AND'; $listFields = array_merge($listFields, $fields); $listConditions = array_merge($listConditions, $conditions); $listValue = array_merge($listValue, $values); $listAccess = array_merge($listAccess, $access); $listEval = array_merge($listEval, $eval); $listSearchType = array_merge($listSearchType, $searchType); //$listGrouped[count($listGrouped) -1] = '1'; $listJoins = array_merge($listJoins, $joins); $listGrouped = array_merge($listGrouped, FArrayHelper::array_fill(0, count($joins), 0)); } } } return array($listFields, $listConditions, $listValue, $listAccess, $listEval, $listJoins, $listGrouped, $listSearchType); }
/** * Called at end of form record save. Used for many-many join elements to save their data * * @param array &$data Form data * * @since 3.1rc1 * * @return void */ public function onFinalStoreRow(&$data) { if (!$this->isJoin()) { return; } $groupModel = $this->getGroupModel(); $listModel = $this->getListModel(); $db = $listModel->getDb(); $query = $db->getQuery(true); $formData =& $this->getFormModel()->formDataWithTableName; // I set this to raw for cdd. $name = $this->getFullName(true, false); $ajaxSubmit = $this->app->input->get('fabrik_ajax'); $rawName = $name . '_raw'; $shortName = $this->getElement()->name; $join = $this->getJoin(); // The submitted element's values $d = FArrayHelper::getValue($formData, $rawName, FArrayHelper::getValue($formData, $name)); // set $emptyish to false so if no selection, we don't save a bogus empty row $allJoinValues = FabrikWorker::JSONtoData($d, true, false); if ($groupModel->isJoin()) { $groupJoinModel = $groupModel->getJoinModel(); $k = str_replace('`', '', str_replace('.', '___', $groupJoinModel->getJoin()->params->get('pk'))); $parentIds = (array) $formData[$k]; } else { $k = 'rowid'; $parentIds = empty($allJoinValues) ? array() : FArrayHelper::array_fill(0, count($allJoinValues), $formData[$k]); } $paramsKey = $this->getJoinParamsKey(); $allParams = (array) FArrayHelper::getValue($formData, $paramsKey, array()); $allParams = array_values($allParams); $i = 0; $idsToKeep = array(); foreach ($parentIds as $parentId) { if (!array_key_exists($parentId, $idsToKeep)) { $idsToKeep[$parentId] = array(); } if ($groupModel->canRepeat()) { $joinValues = FArrayHelper::getValue($allJoinValues, $i, array()); } else { $joinValues = $allJoinValues; } $joinValues = (array) $joinValues; // Get existing records if ($parentId == '') { $ids = array(); } else { $query->clear(); $query->select('id, ' . $shortName)->from($join->table_join)->where('parent_id = ' . $parentId); $db->setQuery($query); $ids = (array) $db->loadObjectList($shortName); } // If doing an ajax form submit and the element is an ajax file upload then its data is different. if (get_class($this) === 'PlgFabrik_ElementFileupload' && $ajaxSubmit) { $allParams = array_key_exists('crop', $joinValues) ? array_values($joinValues['crop']) : array(); $joinValues = array_key_exists('id', $joinValues) ? array_keys($joinValues['id']) : $joinValues; } foreach ($joinValues as $jIndex => $jid) { $record = new stdClass(); $record->parent_id = $parentId; $fkVal = FArrayHelper::getValue($joinValues, $jIndex); $record->{$shortName} = $fkVal; $record->params = FArrayHelper::getValue($allParams, $jIndex); // Stop notice with file-upload where fkVal is an array if (array_key_exists($fkVal, $ids)) { $record->id = $ids[$fkVal]->id; $idsToKeep[$parentId][] = $record->id; } else { $record->id = 0; } if ($record->id == 0) { $ok = $listModel->insertObject($join->table_join, $record); $lastInsertId = $listModel->getDb()->insertid(); if (!$this->allowDuplicates) { $newId = new stdClass(); $newId->id = $lastInsertId; $newId->{$shortName} = $record->{$shortName}; $ids[$record->{$shortName}] = $newId; } $idsToKeep[$parentId][] = $lastInsertId; } else { $ok = $listModel->updateObject($join->table_join, $record, 'id'); } if (!$ok) { throw new RuntimeException('Didn\'t save dbjoined repeat element'); } } $i++; } // Delete any records that were unselected. $this->deleteDeselectedItems($idsToKeep, $k); }