* @license PHP License * @package WB * @subpackage db */ WBClass::load('WBDatasource_FormAttribute'); /** * Populate form element's values from file lists * * Use files in directory to populate values * * @version 1.0.0 * @package WB * @subpackage db */ class WBDatasource_FormAttribute_File extends WBDatasource_FormAttribute { const TYPE_FILE = 'file'; const TYPE_DIR = 'dir'; /** * configuration parameter * @var array */ protected $config = array( 'folder' => 'template/Static', 'recursive' => 1, 'suffix' => '.tmpl', 'type' => 'file' ); /** * table * @var WBDatasource_Table */ protected $table; /** * get attribute's value * * Use dicktionary to fetch list of values * * @param mixed $current actual attribute value * @return array */ protected function getAttributeValue($current) { if (!is_array($current)) { $current = array(); } $dir = $this->config['folder']; if ('/' != $dir[0]) { $dir = WBParam::get('wb/dir/base') . '/' . $dir; } $this->addFolder($current, $dir); usort($current, array($this, 'compareValuesByLabel')); return $current; } /** * add files of folder * * recursively scan folder for matching files * * @param array $current * @param string $dir * @param string $prefix */ private function addFolder(&$current, $dir, $prefix = '') { if (!empty($prefix)) { $prefix .= '/'; } $dh = new DirectoryIterator($dir); foreach ($dh as $i) { $name = $i->getFilename(); // hidden files / folders if ($i->isDot() || '.' == $name[0]) { continue; } // sub folder if ($i->isDir()) { if (self::TYPE_DIR == $this->config['type']) { $name = $prefix . $name; $current[] = array( 'label' => $name, 'value' => $name ); } if ($this->config['recursive']) { $this->addFolder($current, $i->getRealPath(), $prefix . $i->getFilename()); } continue; } // see whether suffix matches if (strlen($this->config['suffix'])) { if (substr($name, -strlen($this->config['suffix'])) != $this->config['suffix']) { continue; } $name = substr($name, 0, -strlen($this->config['suffix'])); } if (self::TYPE_FILE == $this->config['type']) { $name = $prefix . $name; $current[] = array( 'label' => $name, 'value' => $name ); } } } } ?>