I get errors when I install this. Anyone know what's wrong?

idea

Automatically create a table which gathers together information in several pages.

how to use

To include a matrix in a page, you use the Matrix macro. The first argument is the name of a page and the others are a list of field names. e.g.

[[Matrix ContactDetails email tel]]

It will then look at all the pages which are linked to from ContactDetails, and in each of them will look for lines of the form:

email: something and

tel: something

If it finds them it will include the 'somethings' in the table under the appropriate headings. The first column includes links to the source pages.

You can easily create extra fields and if your table becomes too wide, just create a new page showing a subset of the fields. (e.g. PhoneNumbers).

A second version of the macro, called MatrixText, takes the same arguments but displays tab-delimited text, which you can paste into text editors and then import into databases, etc

how to install

Save the following code in matrix.php in a directory of your choice. I simply put it in the parse directory.

<?php

// This macro takes a page, looks for all the links to other pages in it
// and produces a table with one row per linked page
// The columns in the table are specified as the remaining args and are
// filled in from name: value lines in the pages.
function view_macro_matrix($args)

{
  global $pagestore, $LkTbl;
  $text='';
  $bits = explode(" ",$args);
  $srcpage = array_shift($bits);
  $q1 = $pagestore->dbh->query("SELECT link FROM $LkTbl WHERE page='$srcpage'");
  $text .= "<table class=\"matrix\">\n";
  $text .= html_table_row_start();
  $text .= html_table_cell_start() . "page" . html_table_cell_end();
  foreach ($bits as $k) {

      $text .= html_table_cell_start() . $k . html_table_cell_end();
  }
  $text .= html_table_row_end();

  while ($result = $pagestore->dbh->result($q1)) {
    $pg = $pagestore->page($result[0]);
    $pg->read();
    if(!$pg->exists) {
      $visited_count--;
    } else {
      $text .= html_table_row_start();
      $text .= html_table_cell_start();
      $text .= html_ref($result[0], $result[0]) . " ";
      $text .= '<a href="' . editURL($result[0]) . '">(edit)</a>'; 
      $text .= html_table_cell_end();
      foreach ($bits as $k) {
	$text .= html_table_cell_start();
        $exp = '/(?i)(' . $k . ') *: *(.*)\n/';
        if(preg_match($exp, $pg->text, $matches)) {
          $keyval = $matches[2];
          $text .= $keyval;
        } else {
          $text .= "&nbsp;";
        }
	$text .= html_table_cell_end();
      }

      $text .= html_table_row_end();
    }
  }
  $text .= html_table_end();

  return $text;
}

// This is essentially the same as view_macro_matrix
// but the output is tab-delimited text which can be pasted
// into other programs

function view_macro_matrix_text($args) {
  global $pagestore, $LkTbl;
  $text='';
  $bits = explode(" ",$args);
  $srcpage = array_shift($bits);
  $q1 = $pagestore->dbh->query("SELECT link FROM $LkTbl WHERE page='$srcpage'");
  $text .= "<form action=\"\">\n";
  $text .= "<textarea rows=\"20\" cols=\"85\">\n";
  $text .= "page";
  foreach ($bits as $k) {
      $text .= "\t" . $k;
  }
  $text .= "\n";

  while ($result = $pagestore->dbh->result($q1)) {
    $pg = $pagestore->page($result[0]);
    $pg->read();
    if(!$pg->exists) {
      $visited_count--;
    } else {
      $text .= $result[0];

      foreach ($bits as $k) {
	$text .= "\t";

        $exp = '/(?i)(' . $k . ') *: *(.*)\n/';
        if(preg_match($exp, $pg->text, $matches)) {
          $keyval = $matches[2];
          $text .= $keyval;
        } else {
          $text .= "";
        }
      }
      $text .= "\n";
    }

  }
  // this line is formatted this way simply so it 
  // can be pasted into a Tavi editing window
  // without confusing things!
  $text .= "<" ."/textarea><"."/form>";

  return $text;
}
?>

You need to add the following lines to the $ViewMacroEngine array. Either edit the file lib/defaults.php or copy the $ViewMacroEngine? definition to config.php and edit it there:

                      'Matrix'        => 'view_macro_matrix',
                     'MatrixText'    => 'view_macro_matrix_text',

Add your file to parse/macros.php:

 require('matrix.php');

(You may need to specify a directory as well if you put matrix.php somewhere else.)

QuentinStaffordFraser