Search:
Template:Directories > Template:XsysPrView

Template:XsysPrView

    Table of contents
    No headers
    /* 
    //    Copyright (C) 2010 - www.intertesto.com - www.fitzcarraldo.it
    //    Author: gianluca.sabena@intertesto.com - adrian.policarp@fitzcarraldo.it
    //
    //    This program is free software: you can redistribute it and/or modify
    //    it under the terms of the GNU General Public License  http://www.gnu.org/licenses/gpl-2.0.html
    //    
    // ------ Docs ----------------------
    // This script query and render page properties in a specific path. See XsysPrEdit()
    // ------------------ Config ----------------------------------------
    */
    var extradata = $extradata ?? []; /* list of extradata value to add to listglobal (pagedata, firstparagraph, firstimage,commentsnumber)*/
    var inputs = $fields ?? [ ]; /* list of maps - configure form fields - see XsysPrEdit()*/ 
    var prlist = $prlist ?? ["formdata"]; /* list - list of properties name to extract (Attention: field must be presents in all properties) */
    var getsearch = $getsearch ?? []; /* list of 4 values, the same passed to wiki.getsearch() */
    var pathsearch = $pathsearch ?? []; /*  string|list - page path where looking for properties (if last char is an asterisk * the first subpage level is append) */
    var isrenderhtml =  $isrenderhtml ?? true; /* render field based on type, add some html */
    var output = string.tolower($output ?? "table-simp"); /* div | table-simp | table-ext | map */
    var filtername = $filtername ?? ""; /* filter output based o a fieldname */
    var filtervalue = $filtervalue ?? ""; /* filter this value of filtername */
    var isconservative = $isconservative ?? true; /* bool - if enabled will check if saved data is different from current fields and advise */
    var isviewempty = $isviewempty ?? false;  /* bool - display empty fields */
    var firstSelectValue = "---null-select---"; /* value saved as first option in a html drop down menu */
    var cache_enabled = $cache_enabled ?? false; /* bool - enable or disable cache - if enabled output must be set to map and will return a map cache object instead a list */
    var cache_prname = $cache_prname ?? "cache-"..string.join(prlist); /* string - name of propertyes used to store cache data (default: "cache-" + concatenate prlist) */
    var cache_storepageid = $cache_storepageid ?? page.id; /* id of page used to read/save cache properties data */
    
    var labels = {ita:{
                  msgIsAppendDataDetect:"There is data saved in the previous version of the form:  "
                  },
                  eng:{MsgNoData:"-"}};
    var loc = "ita";
    var debug = false; // mus be exactly true or false
    if (user.name == 'wikiadmin') let debug = true;
    
    /* ------------------- init ----------------------------------------- */
    var ispredit = __request.args['ispredit'];
    var fieldvalues = list.collect(inputs,'field');
    var pl = {}; var errors = ""; var listglobal = []; var mapsingle= {};
    var cache_lastupdate = ""; var cache_queryhash = ""; var cache_stored = ""; var cache_search="";
    var cache_status ="init"; /* init = default update=is sync old=need update */
    if (typeof(pathsearch) == 'string') let pathsearch = [pathsearch];
    if(ispredit!=true) {
    if ( #getsearch > 0 && #pathsearch > 0 ) { 
      let errors ..="<li>Error! You can define SEARCHPAGEPATH OR GETSEARCH not both!</li>";
    } else if ( #pathsearch > 0 && cache_enabled) {
      var q = "type:wiki AND (";
      var pre = "";
      foreach (var s in pathsearch) {
        if (string.startswith(s,'/')) let s = string.remove(s,0,1);
        let q ..= pre.." path:"..s;
        let pre = " OR ";
      }
      let q ..=")";
      let cache_search = wiki.getsearch(q,1,"-date");
    } else if (#getsearch > 0 && cache_enabled) {
      let cache_search = wiki.getsearch(getsearch[0],1,"-date",getsearch[3]);
    }
    if (list.contains(list.collect(inputs,'type'),"int_locator")) locator.init();
    /* - check  cache -- */
    if (#cache_search > 0) {
      let  cache_lastupdate = date.format(cache_search[0].date,'s');
      let cache_queryhash = string.hash(cache_lastupdate..string.join(pathsearch)..string.join(getsearch));
      let cache_stored = json.parse(wiki.getpage(cache_storepageid).properties[cache_prname].text.."");
      if (cache_stored.cache_queryhash == cache_queryhash) {
        let cache_status = "update";
        let listglobal = cache_stored.data;
      } else {
        let cache_status = "old";
      }
    }
    if (#pathsearch > 0 && cache_status != 'update') {
      foreach (var searchpagepath in pathsearch ) {
        var path = "";
        if (string.startswith(searchpagepath,'/')) let searchpagepath = string.remove(searchpagepath,0,1);
        if (string.endswith(searchpagepath,'/')) let path = string.remove(searchpagepath,#searchpagepath-1,1);
        else if (string.endswith(searchpagepath,'/*')) let path = string.remove(searchpagepath,#searchpagepath-2,2);
        else let path = searchpagepath;
        var t = wiki.getpage(path);
        let pl ..= {(t.name):t};
        if (string.endswith(searchpagepath,'/*')) let pl ..= t.subpages;
        else let pl ..= {(t.name):t};
      }
    } else if (#getsearch > 0 ){
      let pl = wiki.getsearch(getsearch[0],getsearch[1],getsearch[2],getsearch[3]);
    }
    if (errors == "" && cache_status!='update') {
    /* ------------------- Extract ----------------------------------------- */
      foreach (var p in pl) {
        foreach (var property in prlist) {
          var properties=[];
          if (#p.properties[property].text > 0){
            let properties = json.parse(p.properties[property].text );
          }
          foreach (var data in properties) {
            if ( p.path > 0) { 
              let listsingle = [];
              let mapsingle = {  };
              /*let mapsingle ..= { 'markericofront': site.uri.."/@api/deki/files/51/=Y_marker.gif"};*/
              let mapsingle ..= { 'pageid' : p.id };
              if (output == 'table-simp' || output == 'table-ext' || output=='list'|| list.contains(extradata,'pagedata') ) {
                let mapsingle ..= { 'pageuri' : p.uri };
                let mapsingle ..= { 'pagetitle' : p.title };
                let mapsingle ..= { 'pagedate' : p.date };
                let mapsingle ..= { 'pagename' : p.name };
                let mapsingle ..= { 'pageauthor' : p.author.name }; 
              }
              if (list.contains(extradata,'firstparagraph')) {
                var t = xml.list(p.xml,"//p[not(@class)]/text()",nil,true);
                var out="";
                foreach(var i in t) {
                  if(#string.trim(i)>0 && #out==0) let out = i;
                }
                let mapsingle ..= { 'firstparagraph' : out};
              }
              if (list.contains(extradata,'firstimage')) {
                var firstimage = string.match(string.tolower(xml.text(p.xml,'/page/content/body[1]',nil,true)),'(/@api/[^.]+(.png|.jpg|.jpeg|.gif))');
                let mapsingle ..= {'firstimage':firstimage};
              }
              if (list.contains(extradata,'commentsnumber')) {
                let mapsingle ..= {'commentsnumber':#p.comments};
              }
              if (isconservative) {
                  var t = {};
                  var keys = map.keys(data);
                  foreach (var key in keys) {
                    if (!(list.contains(fieldvalues,key))) {
                      let t ..={(key):data[key]};
                    }
                  }
                  let mapsingle ..= { 'appenddata' : t };
              }
              foreach ( var input in inputs where (#data[input.field] >0 && data[input.field]!=firstSelectValue ) ) {
                if (input.field == filtername) {
                  var t = false;
                  if (typeof(data[filtername])=='list')  {
                    if (list.contains(data[filtername],string.trim(filtervalue.." "))) 
                      let mapsingle ..={'syscheckfilter':true};
                    else let mapsingle ..={'syscheckfilter':false};
                  } else  {
                    if (data[filtername]..""==string.trim(filtervalue.." ")) 
                      let mapsingle ..={'syscheckfilter':true};
                    else let mapsingle ..={'syscheckfilter':false};
                  }
                }
                if (!isrenderhtml) {
                  let mapsingle ..= { (input.field) : data[input.field] };
                } else {
                var t = "";
                if (input.type == "html_select") {
                  if (typeof input.elements == 'list') { 
                    let t =  data[input.field];
                  }
                  else if (typeof input.elements == 'map') {
                    var i = data[input.field];
                    var j =  input.elements;
                    let t = j[i];
                  }
                }    
                else if(input.type == "html_link"){
                    var v = string.trim(string.tolower(data[input.field]));
                    if(string.startswith(v,"http://") || string.startswith(v,"https://")){
                       let t = web.link(v,v);
                    }else{
                       let t = web.link("http://"..v,v);
                    }
                }
                else if(input.type == "html_mail"){
                    var v = string.trim(string.tolower(data[input.field]));
                    let t = web.link("mailto:"..v,v);
                }
                else if (input.type == "html_textarea") {
                  if (input.newline == true) let t = web.html(string.replace(data[input.field],'\n','<br />'));
                  else let t =  data[input.field];
                }  
                else if (input.type == "mtsid_user" && data[input.field]!= firstSelectValue) {
                  var v = wiki.getuser(data[input.field]+0);
                  if (input.link=='page') let t =  web.link(site.uri..'User:'..v['name'],v['name']);
                  else if (input.link=='changes') let t =  web.link(site.uri..'Special:Contributions?target='..v['name'],v['name']);
                  else  let t =  v['name'];
                }
                else if (input.type == "mtmid_users") {
                  var s = "";
                  var t =  "";
                  foreach ( var i in data[input.field] where i!= firstSelectValue) {
                    var v = wiki.getuser (i+0);
                    if (input.link=='page') let t ..=  s..web.link(site.uri..'User:'..v['name'],v['name']);
                    else if (input.link=='changes') let t ..=  s..web.link(site.uri..'Special:Contributions?target='..v['name'],v['name']);
                    else  let t ..=  v['name'];
                    let s = ", ";
                  }
                }
                else if (input.type == "mtsid_page" && data[input.field]!= firstSelectValue ) {
                  var v = wiki.getpage(data[input.field]+0);
                  if(#v && input.link!=false) let t = web.link(v['uri'],v['title']);
                  else if(#v) let t = v['name'];
                } 
                else if (input.type == "mtmid_pages") {
                  var s = "";
                  foreach ( var i in data[input.field] where i!= firstSelectValue ) {
                    var v = wiki.getpage(i+0);
                    if(#v && input.link!=false) let t ..= s..web.link(v['uri'],v['title']);
                    else if(#v) let t..= v['name'];
                    let s = ", ";
                  }
                }
                else {
                  if (data[input.field]!= firstSelectValue) let t = data[input.field];
                }
                let mapsingle ..= { (input.field) : t };
                }
              }
            }
            let listglobal ..= [mapsingle];
          }
        } 
      } 
    } /* close if errors */
    
    if (#filtername >0 ) let listglobal = list.select(listglobal,"$['syscheckfilter']==true");
    if (errors == "" && output == 'map') {
     /* if we serialize in order to capture the output in the calling page, no javascript code is
     // executed here, so we add a cache object and process it in the calling page */
      if (cache_enabled == true) {
        let listglobal = {data:listglobal, cache_status:cache_status, cache_prname:cache_prname, cache_storepageid:cache_storepageid, cache_lastupdate: cache_lastupdate, cache_queryhash:cache_queryhash};
      }
      string.serialize(listglobal);
    }
    else if (errors == "" && output == 'list') {
      <ul>
         foreach(var row in listglobal) {
           var t = listglobal[__count];
           <li>;web.link(t['pageuri'], string.replace(t['pagename'],"_"," "));</li>;
         }
      </ul>
    }
    else if (errors == "" && output == 'div') {
      <div class="DivFormView">
      foreach (var row in listglobal) {
        var t = listglobal[__count];
        foreach (var i in inputs where !(i.iscreateonly)) {
    /* --- Crit */
          if (i.type == "html_hr") {
            <div class="fHr fRow" >;<hr />;</div>;
          } else if (i.type == "html_label") {
            <div class="fTitle fRow" >;web.html(i.value);</div>;
          } else if (i.type == "int_locator") {
            if(#t['latitude']>0 && #t['longitude']>0) {
              <div class="geo fRow">;
              <span class="fLabel" >i.label;</span>;
              <span class="fField" >locator.plot([{latitude:t['latitude'],longitude:t['longitude'],iscenter:true}],i.optPlot);</span>;
              </div>;
            }
          } else if (#t[i.field] >0 && i.viewshow != false  && isviewempty==false ||isviewempty==true ) {
            var tc="fRow "..i.field.."";
            <div class=(tc)>;
              <span class="fLabel" >i.label;</span>;
              <span class="fField" >;web.html(t[i.field]);</span>;
            </div>;
          }
        }
        if (isconservative && #t['appenddata']>0) {
          <div class="fRow appenddata">;
            labels[loc].msgIsAppendDataDetect;
            foreach (var f in map.keys(t['appenddata'])) { 
              <div>;f..": "..t['appenddata'][f];</div>; 
            }
          </div>;
        }
      }
      </div>;
    } 
    else if (errors == "" && (output == 'table-simp' || output == 'table-ext')) {
      var printfields = [];
      foreach ( var input in inputs where (input.viewshow != false  && isviewempty==false ||isviewempty==true )) {
        let printfields ..=[input];
      }
      <table border='1' cellpadding='4' cellspacing='0'  >
      <tr bgcolor='#dddddd'>
      <td></td>
      if (output == "table-ext") {
        <td>"Autore (1)"</td>
        <td>"Data modifica (2)"</td>
      }
      if (isconservative) {<td>"Old data"</td>}
      foreach (var i in printfields) {
        <td>i.label</td>
      }
      </tr>
      foreach (var row in listglobal) {
        <tr>
        var t = listglobal[__count];
        if (output == "table-simp") {   
          <td bgcolor='#eeeeee'>web.link(t['pageuri'], 'V')</td> 
        } else {   
          <td bgcolor='#eeeeee'>web.link(t['pageuri'], string.replace(t['pagename'],"_"," "))</td>
          <td bgcolor='#eeeeee'>t['pageauthor']</td>
          <td bgcolor='#eeeeee'>date.format(t['pagedate'],"dd/MM/yy - HH:mm")</td>
        }
        if (isconservative) {
          <td>
          if (#t['appenddata']>0) {
            foreach (var f in map.keys(t['appenddata'])) { 
              <div>;f..": "..t['appenddata'][f];</div>; 
            }
          }
          </td>
        }
        foreach (var i in printfields) {
          var temp = t[i.field];
          <td>web.html(temp)</td>
        }
        </tr>
      }
      </table>
    } else {
      "Si sono verificati alcuni errori: "..errors;
    }
    } /* ispredit != true */
    
    
    

    Files (0)

     

    Comments (0)

    You must login to post a comment.