123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- <?php
- if (!class_exists("plug_shortcodes")) {
- class plug_shortcodes {
- function plug_shortcodes($ns) {
- $this->title = __("Misc Shortcodes","dagsopt");
- $this->pluginname = $ns->pluginname;
- $this->file = $ns->file;
- $this->ns = $ns;
- }
- function start() {
- add_filter('the_content', array(&$this,'CSV_Parse'));
- add_filter('the_content', array(&$this,'CSV_Parse2'));
- add_shortcode( 'acc-page', array(&$this,'getpage') );
- }
- function getpage( $atts ) {
- extract( shortcode_atts( array(
- 'id' => 0
- ), $atts ) );
- $s = "";
- if($id!==0){
- $page = get_page( $id );
- $s .= '<div class="hbox embed-page page-id-'.$id.'" >';
- $s .= '<div class="arrow">'.$page->post_title.'</div>';
- $s .= '<div class="box">';
- $s .= apply_filters('the_content',$page->post_content);
- $s .= '</div>';
- $s .= '</div>';
- }
- return $s;
- }
-
- function CSV_Parse2($content) {
- $content = preg_replace_callback("/\<a href=\'(.*).csv\'(.*)\>(.*)\<\/a\>/i", array(&$this,"CSV_Render2"), $content);
- return $content;
- }
-
- function CSV_Render2($matches) {
- //0 full 1 filename 2 attributes 3 title
-
- $filen = explode("/",$matches[1]);
- $class = array_pop($filen);
- $filen[] = $class;
- $filen = implode("/",$filen).".csv";
- $arguments = array();
- $arguments['filename'] = $filen;
- $arguments['class'] = "tablesorter ".$class;
- $arguments['id'] = "".$class;
- $arguments['title'] = "".$matches[3];
- return $this->CSV_Render_($arguments);
- }
-
- function CSV_Render($matches) {
- $matches[1] = str_replace(array('”','″'), '', $matches[1]);
- preg_match_all('/(\w*)=(.*?)" /i', $matches[1], $attributes);
- $arguments = array();
- foreach ( (array) $attributes[1] as $key => $value ) {
- $arguments[$value] = str_replace('"', '', $attributes[2][$key]);
- }
- if(!isset($arguments['filename']) || !is_file(ABSPATH.$arguments['filename'])){
- return "csv error: filename ".ABSPATH.$arguments['filename']." not readable";
- }
- return $this->CSV_Render_($arguments);
- }
- function CSV_Parse($content) {
- $content = preg_replace_callback("/\[\[csv ([^]]*)\/\]\]/i", array(&$this,"CSV_Render"), $content);
- return $content;
- }
-
- function CSV_Render_($arguments) {
- if(!isset($arguments['filename']) || !is_file(ABSPATH.$arguments['filename'])){
- return "csv error: filename ".ABSPATH.$arguments['filename']." not readable";
- }
- $maxread = isset($arguments['read']) ? $arguments['read'] : (1024*1024*5);
- $delimiter = isset($arguments['delimiter']) ? $arguments['delimiter'] : ',';
- $quotechar = isset($arguments['quotechar']) ? $arguments['quotechar'] : '"';
- $headers = isset($arguments['headers']) ? $arguments['headers'] : false;
- $fields = isset($arguments['fields']) ? explode(",",$arguments['fields']) : false;
- $tid = isset($arguments['id']) ? $arguments['id'] : 'mytable';
- $class = isset($arguments['class']) ? $arguments['class'] : 'tablesorter';
- $tables = array();
- $rows = array();
- $tmp = "";
- $tmpheaders = array();
- $handle = fopen(ABSPATH.$arguments['filename'], "r");
-
-
-
-
- while (($data = fgetcsv($handle, $maxread, $delimiter,$quotechar)) !== FALSE) {
-
- if(substr($data[0],0,1) == "!"){
- $tables[] = array($tmp,$tmpheaders,$rows);
- $rows = array();
- $tmp = substr($data[0],1);
- if($fields){
- $tmpheaders = array();
- $num = count($fields);
- for ($c=0; $c < $num; $c++) {
- $tmpheaders[] = $data[$fields[$c]];
- }
- }else{
- array_shift($data);
- $tmpheaders = $data;
- //print_r($tmpheaders);
- //die();
- }
-
- }else{
- if($fields){
- $num = count($fields);
- $obj = array();
- for ($c=0; $c < $num; $c++) {
- $obj[] = $data[$fields[$c]];
- }
- $rows[] = $obj;
- }else{
- $num = count($data);
- $diff = count($tmpheaders) < $num ? $num - count($tmpheaders) : 0;
- $obj = array();
- for ($c=0; $c < $num; $c++) {
- $obj[] = $data[$c+$diff];
- }
- $rows[] = $obj;
- }
- }
- }
- if($headers){
- $tmpheaders = explode(",",$headers);
- }else{
- if(count($tmpheaders) == 0){
- $tmpheaders = array_shift($rows);
- }
- }
-
- $tables[] = array($tmp,$tmpheaders,$rows);
- if(count($tables)>1){
- array_shift($tables);
- }
- fclose($handle);
- $sa = "";
- $index = 0;
- $menu = "";
- foreach($tables as $table){
- $title = $table[0];
- $tt = explode("\n",$title);
- if(count($tt)>1){
- $title = array_shift($tt);
- }
- $menu .= '<li><a href="#'.$title.'">'.$title.'</a></li>';
- }
- $sa .= '<ul id="'.$tid.'_menu" class="csvelement anchormenu">'.$menu.'</ul>';
-
- foreach($tables as $table){
- $index ++;
-
- $title = $table[0];
- $heads = $table[1];
- $rows = $table[2];
-
- $hhe = "";
- $tt = explode("\n",$title);
- if(count($tt)>1){
- $title = array_shift($tt);
- $hhe .= '<h3>'.$title.'</a></h3>';
- $hhe .= '<p>'.implode("<br>",$tt).'</p>';
- }else{
- $hhe .= '<h3>'.$title.'</h3>';
- }
- $sa .= '<a name="'.$title.'" class="anchor"> </a>';
-
- $sa .= '<div id="'.$tid.'_'.$index.'" class="csvelement '.$tid.'">';
- $sa .= $hhe;
- $sa .= '<table class="'.$class.'">';
- $sa .= '<thead><tr>';
- $cols = count($heads);
- for($y=0;$y<$cols;$y++){
- $sa .= '<th class="col'.$y.'">';
- $sa .= $heads[$y];
- $sa .= '</th>';
- }
- $sa .= '</tr></thead><tbody>';
- for($i=0,$n=count($rows);$i<$n;$i++){
- $row = $rows[$i];
- if(strlen(join("",$row))==0){
- }else{
- $sa .= '<tr>';
- for($y=0;$y<$cols;$y++){
- $sa .= '<td class="col'.$y.'">';
- $tt = explode("\n",$row[$y]);
- if(count($tt)>1){
- $sa .= '<strong>'.array_shift($tt).'</strong><br>';
- }
- $sa .= implode("<br>",$tt);
- $sa .= '</td>';
- }
- $sa .= '</tr>';
- }
- }
- $sa .= '</tbody></table><div class="totoplink"><a href="#TOP">Top</a></div></div>';
- }
-
- /*file_put_contents($cachefn,$sa);*/
-
- return $sa;
-
-
- }
-
-
-
-
-
-
-
- function help(){
- ?>
- [csv filename="" read="" delimiter="," quotechar='"' headers=false fields=false id="mytable" class="tablesorter"] <br>
- < a href='test.csv' >Title < /a >
-
-
- <?php
- }
-
-
- function admin_line(){
- ?>
- <?php
- }
- }
- global $plug_shortcodes;
- $plug_shortcodes = new plug_shortcodes($this);
- $this->dagsopt['plug_shortcodes'] = $plug_shortcodes;
-
-
-
-
-
-
-
-
-
- }
-
-
|