/** * Read the resource as CSV data * * @param uri * The resource uri * @param args default null * If not null, will process the csv using mustache template * @param fields default null * If not null, will take this as the fields, if not, will use the first line * @param flat default true * If true, will flattern all the fields * @param enclosure default " * The enclosure of the CSV * @param escape default \ * The escape character of the CSV */ public function read($uri, $args = null, $fields = null, $flat = true, $delimiter = ",", $enclosure = '"', $escape = "\\") { $r = new Resource($uri); $c = $r->contents(); if ($c) { if ($args) { $c = \Clips\out("string://" . $c, $args, false); } $ret = array(); foreach (explode("\n", $c) as $line) { $s = trim($line); if ($s) { $data = str_getcsv($s); if ($fields) { $d = array(); for ($i = 0; $i < count($fields); $i++) { $d[$fields[$i]] = $data[$i]; } $ret[] = $d; } else { if ($flat) { $fields = array_map(function ($item) { return \Clips\to_flat($item); }, $data); } else { $fields = $data; } } } } return $ret; } return array(); }
public function filter_after($chain, $controller, $method, $args, $request, $controller_ret) { $scsses = \Clips\context('scss'); if ($scsses) { $cache = $this->filecache->cacheDir(); $forward_method = \Clips\context('forward_method'); if ($forward_method) { $method = $forward_method; } $full_name = \Clips\to_flat(get_class($controller)) . '_' . $method; $uri = \Clips\path_join(\Clips\path_join($cache, 'css'), $full_name); $cache_filename = \Clips\path_join($cache, 'css', \Clips\to_flat(get_class($controller) . '_' . $method) . '.css'); if (file_exists($cache_filename) && !\Clips\config('debug_sass')) { \Clips\add_css(\Clips\static_url($uri)); return; } if (\Clips\config('debug_sass')) { $this->sass->source_map_file = $cache_filename . '.map'; $this->sass->source_comments = true; $this->sass->source_map_embed = true; $this->sass->source_map_contents = true; } // Add the sass_dir into include pathes $result = $this->sass->compile($scsses); if ($result) { $this->filecache->save(\Clips\to_flat(get_class($controller) . '_' . $method) . '.css', $result, \Clips\path_join($cache, 'css'), true); \Clips\add_css(\Clips\static_url($uri)); } } }
public function init() { $class = explode('\\', get_class($this)); $class = array_pop($class); // The last one $class = \Clips\to_flat(str_replace('Migration', '', $class)); $this->tool =& \Clips\get_clips_tool(); $this->template = $this->tool->library('MigrationTool'); $this->config = Yaml::parse(\Clips\content_relative('schemas/' . $class . '.yml', $this)); }
public function init() { $this->tool->widget('ListView'); if (isset($this->value)) { if (!is_array($this->value)) { $this->value = array($this->value); } } else { $this->value = array(\Clips\default_form_name()); } // Initialize the ListView in javacript foreach ($this->getConfig() as $name => $config) { if ($config) { // Setting the default values for the datatable configuration if (!isset($config->ajax)) { $controller_class = \Clips\context('controller_seg'); $method = \Clips\context('controller_method'); $uri = \Clips\context('uri'); if (strpos($uri, $method) !== false) { $d = explode($method, $uri); $config->ajax = \Clips\site_url(\Clips\path_join($d[0], $name)); } else { if (strpos($uri, $controller_class) !== false) { $config->ajax = \Clips\site_url(\Clips\path_join($uri, $name)); } else { $config->ajax = \Clips\site_url(\Clips\path_join($uri, $controller_class, $name)); } } } $config->processing = true; $config->serverSide = true; foreach ($config->columns as $col) { if (isset($col->data)) { // Must smooth the data $col->data = \Clips\smooth($col->data); } if (isset($col->refer)) { $col->refer = \Clips\smooth($col->refer); } if (isset($col->action)) { // If has action, use action render $col->render = 'datatable_action_column'; } } // Adding the initialize script to jquery init \Clips\context('jquery_init', '$("ul[name=' . \Clips\to_flat($name) . ']").listview(' . json_encode($config) . ')', true); } } }
public function init() { $name = get_class($this); // Remove the prefixes $name = explode('\\', $name); $name = $name[count($name) - 1]; if (!isset($this->table)) { $this->table = \Clips\to_flat(str_replace('Model', '', $name)) . 's'; // If no table is set for this model, just guess for its table } if (!isset($this->name)) { // If there is no name from the annotation, will use the default table name as the name $this->name = $name; } // Check for models config first $models = \Clips\config('models'); if ($models) { foreach ($models as $mc) { $config = \Clips\get_default($mc, $this->name, null); if ($config) { // If found the model's configuration, try using it to find datasource $ds = \Clips\get_default($config, 'datasource'); if ($ds) { $datasource = $ds; break; } } else { // If not found for model itself, try the common one $ds = \Clips\get_default($mc, 'datasource'); if ($ds) { $datasource = $ds; break; } } } } $ds = $this->tool->library('DataSource'); // Load the datasource library if (!isset($datasource)) { // There is still no datasource information, let's try using first one of the datasource $this->db = $ds->first(); } else { $this->db = $ds->get($datasource); } if (!isset($this->db)) { throw new \Exception('Cant\'t find any datasource for this model.'); } }
public function init() { $tool = $this->tool; $config = $tool->config; $name = get_class($this); // Remove the prefixes $name = explode('\\', $name); $name = $name[count($name) - 1]; if (!isset($this->table)) { $this->table = \Clips\to_flat(str_replace('Model', '', $name)) . 's'; // If no table is set for this model, just guess for its table } // Check for models config first if ($config->models) { foreach ($config->models as $mc) { if (isset($mc->{$name})) { if (isset($mc->{$name}->datasource)) { $datasource = $mc->{$name}->datasource; break; } } if (isset($mc->datasource)) { // Let's try the overall configuration $datasource = $mc->datasource; break; } } } $ds = $tool->library('DataSource'); // Load the datasource library if (!isset($datasource)) { // There is still no datasource information, let's try using first one of the datasource $this->db = $ds->first(); } else { $this->db = $ds->get($datasource); } if (isset($this->db)) { parent::__construct($this->db->type); if (isset($this->db->table_prefix)) { $this->table_prefix = $this->db->table_prefix; } return; } throw new \Exception('Cant\'t find any datasource for this model.'); }
public function init() { $this->tool->widget('DataTable'); if (isset($this->value)) { if (!is_array($this->value)) { $this->value = array($this->value); } } else { $this->value = array(\Clips\default_form_name()); } // Initialize the datatable in javacript foreach ($this->getConfig() as $name => $config) { if ($config) { // Enforce security by default for DataTable $config->security = $this->securityengine; $config->name = $name; // Setting the default values for the datatable configuration if (!isset($config->ajax)) { $controller_class = \Clips\context('controller_seg'); $method = \Clips\context('controller_method'); $uri = \Clips\context('uri'); if (strpos($uri, $method) !== false) { $d = explode($method, $uri); $config->ajax = \Clips\site_url(\Clips\path_join($d[0], $name)); } else { if (strpos($uri, $controller_class) !== false) { $config->ajax = \Clips\site_url(\Clips\path_join($uri, $name)); } else { $config->ajax = \Clips\site_url(\Clips\path_join($uri, $controller_class, $name)); } } } $config->processing = true; $config->serverSide = true; // The bundle configuration in anntation has the highest priority $bundle = \Clips\get_default($this, 'bundle', null); if ($bundle === null) { // Try configuration's bundle settings $bundle = \Clips\get_default($config, 'bundle', null); if ($bundle === null) { // We can't find the bundle in annotation, try to find it using default name $bundle_name = 'pagination/' . $name; $bundle = $this->tool->bundle($bundle_name); if ($bundle->isEmpty()) { // All default bundle can't be found, try current controller's bundle $bundle = \Clips\context('current_bundle'); if ($bundle === null) { $bundle = ''; } } } } $bundle = \Clips\bundle($bundle); // Update the columns configuration to the filtered columns configuration $config->columns = $config->columns(); $replaces = array(); foreach ($config->columns as $col) { if (isset($col->title)) { $col->title = $bundle->message($col->title); } if (isset($col->data)) { // Must smooth the data if (strpos($col->data, " as ")) { $d = explode(' as ', $col->data); if ($d) { $col->data = trim($d[1]); } } $col->data = \Clips\smooth($col->data); } if (isset($col->refer)) { $col->refer = \Clips\smooth($col->refer); } if (isset($col->render)) { if (array_search($col->render, $replaces) === false) { $replaces[] = $col->render; } } if (isset($col->action)) { // If has action, use action render $col->render = 'datatable_action_column'; if (array_search($col->render, $replaces) === false) { $replaces[] = $col->render; } } } // Clean the where data stored in the session $controller = \Clips\context('controller'); if ($controller) { $controller->request->session($name, null); } $json = $config->toJson(); foreach ($replaces as $r) { $json = str_replace('"' . $r . '"', $r, $json); } // Adding the initialize script to jquery init \Clips\context('jquery_init', 'if(window[\'DatatableSettings\'] == undefined ) { DatatableSettings = {}; } DatatableSettings["' . $name . '"] = ' . $json . ';$("table[name=' . \Clips\to_flat($name) . ']").DataTable(DatatableSettings["' . $name . '"]);', true); } } }
public function testToFlat() { $this->assertEquals(Clips\to_flat('ABC'), 'a_b_c'); $this->assertEquals(Clips\to_flat('AaBbCc'), 'aa_bb_cc'); $this->assertEquals(Clips\to_flat('D/E/F/AaBbCc'), 'd_e_f_aa_bb_cc'); }