';
$this->formulario .= $html;
return $this;
}
public function mostrarCategorias($name, $prefijo, $idParentCategoria = 2) {
$sql = ' SELECT '
. ' c.id_category, c.id_parent, cl.name, s.id_category AS checked, s.margen '
. ' FROM '
. ' ' . _DB_PREFIX_ . 'category AS c '
. ' INNER JOIN ' . _DB_PREFIX_ . 'category_lang AS cl '
. ' ON '
. ' cl.id_category = c.id_category '
. ' AND cl.id_lang = ' . $this->idLang . ''
. ' AND c.id_shop_default = cl.id_shop '
. ' AND c.id_shop_default = ' . $this->idShop . ''
. ' LEFT JOIN ' . _DB_PREFIX_ . $prefijo . 'categorias AS s '
. ' ON s.id_category = c.id_category '
. ' WHERE '
. ' c.id_parent = ' . $idParentCategoria . ' '
. ' ORDER BY '
. ' c.id_parent, c.id_category ';
$categorias = Db::getInstance()->executeS($sql);
$html = '
';
$html .= '';
$this->formulario .= $html;
return $this;
}
private function getHijosTable($categoria, &$html, &$num, $prefijo) {
$numInterno = $num;
$sqlHijos = ' SELECT '
. ' c.id_category, c.id_parent, cl.name, s.id_category AS checked, s.margen '
. ' FROM '
. ' ' . _DB_PREFIX_ . 'category AS c '
. ' INNER JOIN ' . _DB_PREFIX_ . 'category_lang AS cl '
. ' ON '
. ' cl.id_category = c.id_category '
. ' AND cl.id_lang = ' . $this->idLang . ' '
. ' AND c.id_shop_default = cl.id_shop '
. ' AND c.id_shop_default = ' . $this->idShop . ' '
. ' LEFT JOIN ' . _DB_PREFIX_ . $prefijo . 'categorias AS s '
. ' ON s.id_category = c.id_category '
. ' WHERE '
. ' c.id_parent=' . $categoria['id_category'];
$categoriasHijos = Db::getInstance()->executeS($sqlHijos);
$num++;
foreach ($categoriasHijos AS $categoriaHijo) {
$checked = "";
$porcentaje = "";
if (!empty($categoriaHijo['checked']) && $categoriaHijo['checked'] != null) {
$checked = ' checked="checked"';
$porcentaje = $categoriaHijo['margen'];
}
$numClase = $num;
$html .= '
';
$this->getHijos($categoriaHijo, $html, $num);
}
$num = $numInterno;
}
/**
*
* @param string $name
* @param string $text
* @param int $defaultValue
* @param string $title
* @param boolean $active
* @param boolean $chosen
* @return $this
*/
public function createSelectCategory($name, $text, $defaultValue, $title = '', $active = true, $chosen = false) {
$categories = Category::getHomeCategories($this->idLang, $active);
$html = '';
$html .= '
';
if ($chosen) {
$html .= '';
}
$this->formulario .= $html;
return $this;
}
private function getHijos($id_parent, $defaultValue, &$html, &$num, $active) {
$numInterno = $num;
$categories = Category::getChildren($id_parent, $this->idLang, $active);
if (!$categories) {
return;
}
$num++;
foreach ($categories AS $category) {
$selected = '';
if ($category['id_category'] == $defaultValue) {
$selected = ' selected="selected"';
}
$html .= '
';
$this->getHijos($category['id_category'], $defaultValue, $html, $num, $active);
}
$num = $numInterno;
return true;
}
/**
* Crea un upload File.
* @param string $name Nombre del boton
* @param string $text Texto informativo
* @param string $title Titulo
* @return $this
*/
public function createFormUploadFile($name, $text, $title = '') {
$html = '';
$html .= '
';
$this->formulario .= $html;
return $this;
}
/**
* Crea una tabla dinamica.
* @param string $name Identificador de la tabla.
* @param string $table Nombre de la tabla en la base de datos.
* @param string[] $campos Nombres de los campos en base de datos.
* @param string[] $camposBusqueda Nombres de los campos en base de datos para las busquedas.
* @param string $url Ruta base.
* @param string $serverfile Archivo de destino en el servidor.
* @param string $contenidoCabecera Los "tr" para la cabecera.
* @param string[] $codigoFormateadores [id del formateador => Contenido de la funcion formateadora, recibe los parametros column y row].
* @param string $codigoEventoLoad Contenido de la funcion del evento load. La tabla viene en la variable grid. Tambien se dispone de e.
* @return $this
*/
function createTableAjax($name, $table, $campos, $camposBusqueda, $url, $serverfile, $contenidoCabecera, $codigoFormateadores = array(), $codigoEventoLoad = '') {
$html = '
';
$this->formulario .= $html;
return $this;
}
private function addExtras($extras) {
$html = '';
if ($extras && is_array($extras)) {
foreach ($extras AS $indice => $valor) {
$html .= $indice . '="' . $valor . '"';
}
}
return $html;
}
public function __toString() {
return $this->renderForm(true);
}
public function createCheckBoxManufacturer($name, $defaultValue) {
$manufacturers = Manufacturer::getManufacturers();
$html = '';
$numElementos = count($manufacturers);
$elementos = ceil($numElementos / 3);
$intercambio = array_flip($defaultValue);
$i = 0;
$html .= '
';
$html .= '
';
$this->formulario .= $html;
return $this;
}
/**
*
* @param int $id_lang
* @param string $name
* @param string $text
* @param array $values
* @param string $title
* @return $this
*/
public function createOptionStatuses($id_lang, $name, $text, $values, $title = '') {
$status = OrderState::getOrderStates($id_lang);
$html .= '
';
$this->formulario .= $html;
return $this;
}
/**
*
* @param string $name
* @param string $text
* @param int $defaultValue
* @param string $title
* @return $this
*/
public function createSelectStatuses($idLang, $name, $text, $defaultValue, $title = '') {
$status = OrderState::getOrderStates($idLang);
$html = '
';
$this->formulario .= $html;
return $this;
}
/**
*
* @param string $name
* @param string $text
* @param int $defaultValue
* @param string $title
* @return $this
*/
public function createSelectFeatures($idLang, $name, $text, $defaultValue, $title = '') {
$features = Feature::getFeatures($idLang);
$html = '
';
$this->formulario .= $html;
return $this;
}
/**
*
* @param string $name
* @param string $text
* @param int $defaultValue
* @param string $title
* @return $this
*/
public function createSelectFeaturesValues($idLang, $name, $text, $defaultValue, $value, $title = '') {
$features = FeatureValueCore::getFeatureValuesWithLang($idLang, $value);
$html .= '
';
$this->formulario .= $html;
return $this;
}
/**
* Genera un select a partir de un array asociativo.
* @param string $name
* @param string $text
* @param array $datos
* @param string $claveId
* @param string $claveValores
* @param mixed $selectedValue
* @param string $title
* @param array $extras
* @return $this
*/
public function createFormSelectFromArray($name, $text, $datos, $claveId, $claveValores, $selectedValue, $title = '', $extras = null) {
$datosFormateados = array();
foreach ($datos as $dato) {
$datosFormateados[$dato[$claveId]] = $dato[$claveValores];
}
return $this->createFormSelect($name, $text, $datosFormateados, $selectedValue, $title, $extras);
}
/**
* Crea un select.
* @param string $name
* @param array $text
* @param string $datos valor => visible
* @param string $defaultValue
* @param string $title
* @param array $extras
* @return $this
*/
function createFormSelectMultiple($name, $text, $datos, $defaultValues, $title = '', $extras = null) {
$this->addJS('jquery.chosen.js');
$this->addCSS('jquery.chosen.css');
$html = '';
$html .= '
';
$html .= '';
$this->formulario .= $html;
return $this;
}
/**
* Genera un select a partir de un array asociativo.
* @param string $name
* @param string $text
* @param array $datos
* @param string $claveId
* @param string $claveValores
* @param mixed $selectedValues
* @param string $title
* @param array $extras
* @return $this
*/
public function createFormSelectMultipleFromArray($name, $text, $datos, $claveId, $claveValores, $selectedValues, $title = '', $extras = null) {
$datosFormateados = array();
foreach ($datos as $dato) {
$datosFormateados[$dato[$claveId]] = $dato[$claveValores];
}
return $this->createFormSelectMultiple($name, $text, $datosFormateados, $selectedValues, $title, $extras);
}
/**
* Crea una tabla para trabajar con un ObjectModel.
* @param string $objectModelName
* @param string[] $columnNames Las traducciones para los nombres de las columnas.
* @param string[] $columnOrder El orden de las columnas.
* @param string[] $hideColumns Columnas que no se desean en la tabla.
* @return $this
*/
public function createFormObjectModelTable($objectModelName, $columnNames = array(), $columnOrder = array(), $hideColumns = array()) {
$campos = $objectModelName::$definition['fields'];
//Eliminamos campos
foreach($hideColumns as $hideColumn) {
unset($campos[$hideColumn]);
}
//Ordenamos
uksort($campos, function($a, $b) use ($columnOrder) {
$posA = array_search($a, $columnOrder);
if($posA === false) {
$posA = PHP_INT_MAX;
}
$posB = array_search($b, $columnOrder);
if($posB === false) {
$posB = PHP_INT_MAX;
}
return $posA - $posB;
});
$idPrimario = $objectModelName::$definition['primary'];
$ids = Db::getInstance()->executeS("SELECT $idPrimario FROM `"._DB_PREFIX_.$objectModelName::$definition['table'].'`');
//Creamos la tabla
$filaVacia = json_encode($this->generateImaxTableRow($objectModelName, 'newRow', $campos));
$this->formulario .= "";
$this->formulario .= '
';
$this->formulario .= '
';
return $this;
}
/**
* Genera una fila de datos para la tabla de ObjectModel.
* @param string $objectModelName
* @param mixed $id
* @param array $fields
* @param ObjectModel $object
* @return string
*/
private function generateImaxTableRow($objectModelName, $id, $fields, $object = null) {
$auxForm = new imaxForm($this->modulo, $this->_path, '', '', '', '', array(), true);
$html = '
';
foreach($fields as $nombreCampo => $infoCampo) {
$html .= '| ';
$tipo = (!empty($infoCampo['type']) ? $infoCampo['type'] : '');
switch($tipo) {
case ObjectModel::TYPE_BOOL:
$auxForm->createFormCheckboxGroup("imaxTableRow[$objectModelName][$id][$nombreCampo]", '', ($object ? $object->$nombreCampo : ''));
break;
default:
$auxForm->createFormTextGroup("imaxTableRow[$objectModelName][$id][$nombreCampo]", ($object ? $object->$nombreCampo : ''), '', '', !empty($infoCampo['lang']));
break;
}
$html .= $auxForm->renderForm(true);
$html .= ' | ';
}
$html .= ' | ';
$html .= '
';
return $html;
}
/**
* Graba automaticamente las tablas de ObjectModel.
* @param array $post Debe contener imaxTableModels -> los ObjectModels cargados y imaxTableRow -> los datos correspondientes.
* @return boolean
*/
public static function saveFormObjectModelTables($post) {
//Primero eliminamos los modelos que no vienen
if(!empty($post['imaxTableModels'])) {
foreach($post['imaxTableModels'] as $objectModelName) {
if(empty($post['imaxTableRow'][$objectModelName])) {
$idPrimario = $objectModelName::$definition['primary'];
$ids = Db::getInstance()->executeS("SELECT $idPrimario FROM `"._DB_PREFIX_.$objectModelName::$definition['table'].'`');
foreach($ids as $id) {
$obj = new $objectModelName($id[$idPrimario]);
$obj->delete();
}
}
}
}
//Resto de operaciones
if(!empty($post['imaxTableRow'])) {
foreach($post['imaxTableRow'] as $objectModelName => $objectModelsData) {
$campos = $objectModelName::$definition['fields'];
$idsExistentes = array();
//Nuevos y existentes
foreach($objectModelsData as $id => $objectModelData) {
$obj = new $objectModelName((int)$id);
foreach($objectModelData as $field => $value) {
$obj->$field = $value;
}
//Checks que no vienen
foreach($campos as $field => $infoCampo) {
if(!isset($objectModelData[$field]) && !empty($infoCampo['type']) && $infoCampo['type'] == ObjectModel::TYPE_BOOL) {
$obj->$field = false;
}
}
$obj->save();
$idsExistentes[] = (int)$obj->id;
}
//Eliminaciones
$idPrimario = $objectModelName::$definition['primary'];
$idsExistentesString = implode(',', array_filter($idsExistentes));
$where = ($idsExistentesString ? "WHERE $idPrimario NOT IN ($idsExistentesString)" : '');
$ids = Db::getInstance()->executeS("SELECT $idPrimario FROM `"._DB_PREFIX_.$objectModelName::$definition['table']."` $where");
foreach($ids as $id) {
$obj = new $objectModelName($id[$idPrimario]);
$obj->delete();
}
}
}
return true;
}
}