Beispiel #1
0
 /**
  * Correct the (file)name of a driver
  * 
  * @param string    $name    The name of the driver
  * @param string    $type    The type of the driver
  * @return mixed             Either true or a PEAR_Error object
  * @access private
  */
 function _correctDriverName($name, $type)
 {
     $driverNameMap = array('DataSource' => array('DBDataObject' => 'DataObject', 'XLS' => 'Excel'), 'Renderer' => array('ConsoleTable' => 'Console', 'Excel' => 'XLS'));
     // replace underscores (e.g. HTML_Table driver has filename HTMLTable.php)
     $name = str_replace('_', '', $name);
     // does the file exist?
     if (Structures_DataGrid::fileExists("Structures/DataGrid/{$type}/{$name}.php")) {
         return $name;
     }
     // check, whether a name mapping exists (e.g. from 'Excel' to 'XLS')
     if (isset($driverNameMap[$type][$name])) {
         return $driverNameMap[$type][$name];
     }
     // we could not find a valid driver name => return an error
     $error = PEAR::raiseError("Unknown {$type} driver. Please specify an " . 'existing driver.');
     return $error;
 }
Beispiel #2
0
 /**
  * Smarty custom function "getPaging"
  *
  * This is only meant to be called from a smarty template, using the
  * expression: {getPaging <options>}
  *
  * <options> are any Pager::factory() options
  *
  * @param array  $params Options passed from the Smarty template
  * @param object $smarty Smarty object
  * @return string Paging HTML links
  * @access private
  */
 function _smartyGetPaging($params, &$smarty)
 {
     // Load and get output from the Pager rendering driver
     $driver =& Structures_DataGrid::loadDriver('Structures_DataGrid_Renderer_Pager');
     // Propagate the selfPath option. Do not override user params
     if (!isset($params['path']) && !isset($params['filename'])) {
         $params['path'] = dirname($this->_options['selfPath']);
         $params['fileName'] = basename($this->_options['selfPath']);
         $params['fixFileName'] = false;
     }
     $driver->setupAs($this, $params);
     $driver->build(array(), 0);
     return $driver->getOutput();
 }
Beispiel #3
0
 /**
  * Обработчик действия: Экспорт каталога.
  */
 function Export()
 {
     @set_time_limit(0);
     mk_dir("files/" . DOMAIN . "/tmp");
     clearDir("files/" . DOMAIN . "/tmp");
     require_once "Structures/DataGrid.php";
     require_once "Structures/DataGrid/DataSource/Array.php";
     require_once "Structures/DataGrid/Renderer/CSV.php";
     $categories = array();
     $fields = array();
     $all = array();
     A::$DB->query("SELECT * FROM " . SECTION . "_cols ORDER BY sort");
     $i = 0;
     while ($row = A::$DB->fetchRow()) {
         if ($row['type'] == 'select' || $row['type'] == 'mselect') {
             if ($row['idvar'] = A::$DB->getOne("SELECT property FROM " . DOMAIN . "_fields WHERE item='" . SECTION . "' AND field=?", $row['field'])) {
                 $row['vars'] = loadList($row['idvar']);
                 foreach ($row['vars'] as $j => $name) {
                     if (is_array($name) && isset($name['name'])) {
                         $row['vars'][$j] = $name['name'];
                     }
                 }
             }
         }
         $row['id'] = $i++;
         $all[$row['field']] = $row;
         if (preg_match("/^category([0-9]{1})\$/i", $row['field'], $matches)) {
             $row['level'] = $matches[1];
             $categories[$row['field']] = $row;
         } else {
             $fields[$row['field']] = $row;
         }
     }
     A::$DB->free();
     $renderer = new Structures_DataGrid_Renderer_CSV();
     $renderer->setOptions(array('delimiter' => ';', 'enclosure' => '"', 'saveToFile' => true, 'useQuotes' => true, 'filename' => "files/" . DOMAIN . "/tmp/" . DOMAIN . "_" . getName(SECTION) . ".csv"));
     $renderer->init();
     $datasource = new Structures_DataGrid_DataSource_Array();
     $datagrid = new Structures_DataGrid();
     $datagrid->bindDataSource($datasource);
     $datagrid->attachRenderer(&$renderer);
     $i = 0;
     $header = array();
     foreach ($all as $field => $frow) {
         $header[$i++] = array('field' => $field, 'label' => mb_convert_encoding($frow['caption'], "Windows-1251", "UTF-8"));
     }
     $renderer->buildHeader($header);
     $cats = array();
     $this->getCategories($cats);
     $i = 0;
     foreach ($cats as $id => $category) {
         A::$DB->query("SELECT * FROM " . SECTION . "_catalog WHERE idcat={$id} ORDER BY name");
         while ($row = A::$DB->fetchRow()) {
             $crow = array();
             for ($j = 0; $j < count($all); $j++) {
                 $crow[$j] = "";
             }
             foreach ($categories as $field => $frow) {
                 if (isset($category['parents'][$frow['level']])) {
                     $crow[$frow['id']] = mb_convert_encoding($category['parents'][$frow['level']], "Windows-1251", "UTF-8");
                 }
             }
             foreach ($fields as $field => $frow) {
                 switch ($frow['type']) {
                     default:
                         $crow[$frow['id']] = isset($row[$field]) ? mb_convert_encoding($row[$field], "Windows-1251", "UTF-8") : '';
                         break;
                     case 'select':
                         $crow[$frow['id']] = !empty($frow['vars'][$row[$field]]) ? mb_convert_encoding($frow['vars'][$row[$field]], "Windows-1251", "UTF-8") : "";
                         break;
                     case 'mselect':
                         $row[$field] = explode(',', $row[$field]);
                         foreach ($row[$field] as $j => $value) {
                             $row[$field][$j] = !empty($frow['vars'][(int) $value]) ? mb_convert_encoding($frow['vars'][(int) $value], "Windows-1251", "UTF-8") : "";
                         }
                         $crow[$frow['id']] = implode(', ', $row[$field]);
                         break;
                     case 'float':
                         $crow[$frow['id']] = round($row[$field], 2);
                         break;
                     case 'image':
                         if (preg_match("/^idimg([0-9]+)\$/i", $field, $mathes)) {
                             $sort = $mathes[1];
                             $images = A::$DB->getCol("SELECT path FROM " . DOMAIN . "_images WHERE idsec=" . SECTION_ID . " AND iditem=" . $row['id'] . " ORDER BY sort");
                             $crow[$frow['id']] = isset($images[$sort]) ? basename($images[$sort]) : "";
                         }
                         break;
                     case 'file':
                         if (preg_match("/^idfile([0-9]+)\$/i", $field, $mathes)) {
                             $sort = $mathes[1];
                             $files = A::$DB->getCol("SELECT path FROM " . DOMAIN . "_files WHERE idsec=" . SECTION_ID . " AND iditem=" . $row['id'] . " ORDER BY sort");
                             $crow[$frow['id']] = isset($files[$sort]) ? basename($files[$sort]) : "";
                         }
                         break;
                 }
             }
             if (isset($fields['mprice']) && isset($fields['price'])) {
                 $mprices = !empty($row['mprices']) ? unserialize($row['mprices']) : array();
                 foreach ($mprices as $mp) {
                     $crow[$fields['mprice']['id']] = mb_convert_encoding($mp['name'], "Windows-1251", "UTF-8");
                     $crow[$fields['price']['id']] = (double) $mp['price'];
                     $renderer->buildRow($i++, $crow);
                 }
                 if (empty($mprices)) {
                     $renderer->buildRow($i++, $crow);
                 }
             } else {
                 $renderer->buildRow($i++, $crow);
             }
         }
         A::$DB->free();
     }
     $renderer->render();
     $renderer->finalize();
     if (filesize($file = "files/" . DOMAIN . "/tmp/" . DOMAIN . "_" . getName(SECTION) . '.csv') > 1024 * 2000) {
         return outArchive("files/" . DOMAIN . "/tmp/" . DOMAIN . "_" . getName(SECTION) . ".tar.gz", $file);
     } else {
         require_once 'HTTP/Download.php';
         $params = array('file' => $file, 'contenttype' => 'text/csv', 'contentdisposition' => array(HTTP_DOWNLOAD_ATTACHMENT, basename($file)));
         HTTP_Download::staticSend($params, false);
     }
 }
Beispiel #4
0
 function &prepare(&$do, $frontend = true, $pager = true)
 {
     $unitFormatters = array();
     if (is_array($do->fb_fieldLabels)) {
         foreach ($do->fb_fieldLabels as $k => &$v) {
             if (is_array($v)) {
                 if (key_exists('unit', $v)) {
                     $unitFormatters[$k] = $v['unit'];
                 }
                 $v = $v[0];
             }
         }
     }
     if ($frontend && $pager) {
         $perPage = $this->getControllerOption('recordsPerPage', $do->tableName());
         $perPage = $perPage === true ? 10 : $perPage;
     } else {
         $perPage = null;
     }
     $pk = MyFB::_getPrimaryKey($do);
     $AllFields = array_keys($do->table());
     $links = $do->links();
     $links = is_array($links) ? $links : array();
     $LinkFields = array_keys($links);
     $noLinkFields = array_diff($AllFields, $LinkFields);
     $fields = $this->getControllerOption('fields', $do->tableName());
     if (!is_array($fields)) {
         $fields = array_diff($AllFields, array($pk));
     }
     $usedFields = array_merge($noLinkFields, array_intersect($fields, $LinkFields));
     while ($offset = array_search($pk, $usedFields)) {
         unset($usedFields[$offset]);
     }
     if ($o = $do->getPlugin('ownership')) {
         Log::info('table filtrée par owner');
         if ($o->userIsInAdminMode()) {
             Log::info('ajout du champ owner');
             $fields = array_merge(array($do->ownerShipField), $fields);
             $usedFields = array_merge(array($do->ownerShipField), $usedFields);
             $do->fb_fieldLabels[$do->ownerShipField] = 'Gérant';
         }
     }
     $do->fb_fieldsToRender = $usedFields;
     $fb =& MyFB::create($do);
     $do->selectAdd();
     if (!in_array($pk, $usedFields)) {
         $do->selectAdd($pk . ',' . implode(',', $usedFields));
     } else {
         $do->selectAdd(implode(',', $usedFields));
     }
     $fb->populateOptions();
     $specialElements = $fb->_getSpecialElementNames();
     $dg = new Structures_DataGrid($perPage);
     $dts = new Structures_DataGrid_DataSource_MyDataObj();
     $dts->bind($do, array('generate_columns' => false, 'fields' => $usedFields));
     $dg->bindDataSource($dts);
     if ($frontend) {
         if ($this->getControllerOption('edit', $do->tableName()) || $this->getControllerOption('view', $do->tableName())) {
             $dg->addColumn(new Structures_DataGrid_Column(null, null, null, array('width' => '50px'), null, array('M_Office_ShowTable', 'getEditLink'), array('database' => $do->database(), 'table' => $do->tableName(), 'pk' => $pk)));
         }
         if ($this->_controller->hasActions) {
             $dg->addColumn(new Structures_DataGrid_Column('', null, null, array('width' => '20px'), null, array('M_Office_ShowTable', 'getSelectedEntry'), array('pk' => $pk)));
         }
         $formatters = $this->getControllerOption('formatters', $do->tableName());
     }
     if (!is_array($formatters)) {
         $formatters = array();
     }
     if (isset($do->fb_linkOrderFields) && is_array($do->fb_linkOrderFields)) {
         $dg->setDefaultSort($do->fb_linkOrderFields);
     }
     $eltTypes = $do->table();
     $dateRenderer = $this->getControllerOption('dateRenderer');
     foreach ($fields as $field) {
         if (!isset($specialElements[$field])) {
             if (key_exists($field, $unitFormatters)) {
                 $dg->addColumn(new Structures_DataGrid_Column($fb->getFieldLabel($field), $field, in_array($field, $AllFields) ? $field : null, null, null, key_exists($field, $formatters) ? $formatters[$field]['callback'] : array('M_Office_ShowTable', 'getFieldWithUnit'), key_exists($field, $formatters) ? $formatters[$field]['args'] : array('field' => $field, 'unit' => $unitFormatters[$field])));
             } elseif ($eltTypes[$field] & DB_DATAOBJECT_DATE && !empty($dateRenderer) && $dateRenderer !== true) {
                 $dg->addColumn(new Structures_DataGrid_Column($fb->getFieldLabel($field), $field, in_array($field, $AllFields) ? $field : null, null, null, key_exists($field, $formatters) ? $formatters[$field]['callback'] : $dateRenderer, key_exists($field, $formatters) ? $formatters[$field]['args'] : array('field' => $field)));
             } else {
                 $dg->addColumn(new Structures_DataGrid_Column($fb->getFieldLabel($field), $field, in_array($field, $AllFields) ? $field : null, null, null, key_exists($field, $formatters) ? $formatters[$field]['callback'] : null, key_exists($field, $formatters) ? $formatters[$field]['args'] : null));
             }
         }
     }
     if (isset($_REQUEST['orderBy'])) {
         $dg->sortRecordSet($_REQUEST['orderBy'], $_REQUEST['direction']);
     }
     $renderer =& new Structures_DataGrid_Renderer_HTMLTable();
     $renderer->setTableAttribute('width', '100%');
     $renderer->setTableAttribute('cellspacing', '0');
     $renderer->setTableAttribute('cellpadding', '0');
     $renderer->setTableAttribute('class', 'datagrid');
     $renderer->setTableOddRowAttributes(array('class' => 'oddRow'));
     $renderer->setTableEvenRowAttributes(array('class' => 'evenRow'));
     $renderer->sortIconASC = '&uarr;';
     $renderer->sortIconDESC = '&darr;';
     $renderer->setOption('selfPath', ROOT_ADMIN_URL . ROOT_ADMIN_SCRIPT);
     $dg->attachRenderer($renderer);
     $this->_view =& $dg;
 }
Beispiel #5
0
		/**
		 *
		 * The Constructor
		 *
		 * Classname is specific to Structures_Datagrid_Propel
		 *
		 * The other parameters are needed to construct the parent Structures_DataGrid Class.
		 *
		 * @param      string className
		 * @param      string limit
		 * @param      string render
		 *
		 */
		public function __construct($className = null, $limit = null, $render = DATAGRID_RENDER_HTML_TABLE)
		{

				include_once $className.'.php';
				include_once $className.'Peer'.'.php';

				$this->setClassName($className);
				$this->setPeerName($className.'Peer'); // Is this always true ?
				parent::Structures_DataGrid($limit,null,$render);

				// set the default column policy
				$this->setColumnMode(STRUCTURES_DATAGRID_PROPEL_ALL_COLUMNS);
				$this->criteria = new Criteria();
		}
Beispiel #6
0
 function Import()
 {
     require_once "Structures/DataGrid.php";
     require_once "Structures/DataGrid/DataSource/Excel.php";
     require_once "Structures/DataGrid/DataSource/CSV.php";
     require_once 'Image/Transform.php';
     A::$REGFILES = getSectionByModule('pages');
     mk_dir("files/" . DOMAIN . "/tmp");
     clearDir("files/" . DOMAIN . "/tmp");
     if (isset($_FILES['file']['tmp_name']) && file_exists($_FILES['file']['tmp_name'])) {
         $path_parts = pathinfo($_FILES['file']['name']);
         $ext = preg_replace("/[^a-z0-9]+/i", "", mb_strtolower($path_parts['extension']));
         if ($ext == 'xls' || $ext == 'csv') {
             if ($ext == 'csv') {
                 $sourcefile = $_FILES['file']['tmp_name'];
                 $content = @file_get_contents($sourcefile);
                 if ($content && !mb_check_encoding($content, 'UTF-8')) {
                     file_put_contents($sourcefile, mb_convert_encoding($content, 'UTF-8', 'Windows-1251'));
                 }
             } else {
                 $sourcefile = $_FILES['file']['tmp_name'];
             }
             if ($ext == 'xls') {
                 $datasource = new Structures_DataGrid_DataSource_Excel();
                 $datasource->bind($sourcefile);
             } elseif ($ext == 'csv') {
                 $datasource = new Structures_DataGrid_DataSource_CSV();
                 $datasource->bind($sourcefile, array('delimiter' => ';', 'enclosure' => '"'));
             } else {
                 return false;
             }
             $datagrid = new Structures_DataGrid();
             $datagrid->bindDataSource($datasource);
             if (isset($_REQUEST['clear'])) {
                 A::$DB->execute("TRUNCATE " . STRUCTURE);
             }
             $sort = A::$DB->getOne("SELECT MAX(sort) FROM " . STRUCTURE) + 1;
             $list = array();
             foreach ($datagrid->recordSet as $row) {
                 if (empty($row)) {
                     continue;
                 }
                 if ($ext == 'xls') {
                     $trow = array();
                     foreach ($row as $j => $value) {
                         if (!empty($value)) {
                             $trow[$j - 1] = $value;
                         }
                     }
                     $row = $trow;
                 }
                 $data = array();
                 if (!empty($row[0])) {
                     $data['name_' . LANG] = trim($row[0]);
                 } else {
                     continue;
                 }
                 $j = 1;
                 A::$DB->query("SELECT * FROM " . DOMAIN . "_fields WHERE item='" . STRUCTURE . "' ORDER BY sort");
                 while ($frow = A::$DB->fetchRow()) {
                     switch ($frow['type']) {
                         default:
                             $data[$frow['field']] = !empty($row[$j]) ? trim($row[$j]) : "";
                             break;
                         case 'int':
                             $data[$frow['field']] = !empty($row[$j]) ? (int) $row[$j] : 0;
                             break;
                         case 'float':
                             $data[$frow['field']] = !empty($row[$j]) ? (double) $row[$j] : 0;
                             break;
                         case 'select':
                             if (!empty($row[$j])) {
                                 if (!isset($list[$frow['property']])) {
                                     $list[$frow['property']] = loadList($frow['property']);
                                 }
                                 $row[$j] = trim($row[$j]);
                                 $key = array_search($row[$j], $list[$frow['property']]);
                                 if (empty($key) && !empty($row[$j])) {
                                     $key = addToList($frow['property'], $row[$j]);
                                     $list[$frow['property']][$key] = $row[$j];
                                 }
                                 if (!empty($key)) {
                                     $data[$frow['field']] = $key;
                                 }
                             }
                             break;
                         case 'mselect':
                             if (!empty($row[$j])) {
                                 if (!isset($list[$frow['property']])) {
                                     $list[$frow['property']] = loadList($frow['property']);
                                 }
                                 $row[$j] = explode(',', $row[$j]);
                                 $data[$frow['field']] = array();
                                 foreach ($row[$j] as $value) {
                                     $value = trim($value);
                                     $key = array_search($value, $list[$frow['property']]);
                                     if (empty($key) && !empty($value)) {
                                         $key = addToList($frow['idvar'], $value);
                                         $list[$frow['property']][$key] = $value;
                                     }
                                     if (!empty($key)) {
                                         $data[$frow['field']][] = sprintf("%04d", $key);
                                     }
                                 }
                                 $data[$frow['field']] = implode(",", $data[$frow['field']]);
                             }
                             break;
                         case 'bool':
                             $data[$frow['field']] = !empty($row[$j]) && $row[$j] != 'N' ? "Y" : "N";
                             break;
                         case 'image':
                             $row[$j] = preg_replace("/[^a-zA-Zа-яА-Я0-9-_.]/iu", "", $row[$j]);
                             if (is_file($path = "ifiles/" . $row[$j])) {
                                 $data[$frow['field']] = RegisterImage($path, $data['name_' . LANG]);
                             }
                             break;
                         case 'file':
                             $row[$j] = preg_replace("/[^a-zA-Zа-яА-Я0-9-_.]/iu", "", $row[$j]);
                             if (is_file($path = "ifiles/" . $row[$j])) {
                                 $data[$frow['field']] = RegisterFile($path, $data['name_' . LANG]);
                             }
                             break;
                     }
                     $j++;
                 }
                 A::$DB->free();
                 $data['sort'] = $sort++;
                 A::$DB->Insert(STRUCTURE, $data);
             }
             return true;
         }
     }
     return false;
 }