Forums / General / Object Relations / Comment32121

Author Topic

Roberto Kirschbaum

Monday 29 August 2005 6:12:19 am

Comment32121

Hello,

There really seems to be nothing wrong with the fetch function. But I agree that there must be a bug as to why don't the relations show in the admin GUI?

For the records and for those who have the same needs as I do, here's what I've done so far:

I have an override template for the AUTHORS class, which shows the Author's Name, Birth and Death dates, his Biography, and then Links to his available books. Books are in another class, "book", and here's what I did in order to show the books, alphabetically ordered:

        {def $sAutorId=$node.object.id}

        {* GETTING THIS AUTHOR'S BOOKS *}
        {* Note: Authors [Enhanced Object relation] (id:227) *}
        {def $books=fetch( content,
                           reverse_related_objects,
                           hash( object_id, $node.contentobject_id,
                                 attribute_identifier, 227
                                )
                          )
        }


        {* BUILDING AN ARRAY WITH OBJECT_IDs AND BOOKS' TITLES *}
        {def $japassou=0}
        {def $name='?'}
        {def $nodeid=0}
        {foreach $books as $thisbook}
          {set $name=$thisbook.name}
          {set $nodeid=$thisbook.id}
          {if eq($japassou,0)}
            {set $japassou=1}
            {* 1ST TIME: def ARRAY $lista *}
            {def $lista=array($nodeid,$name)}
          {else}
            {* N-th TIME: SET/APPEND TO ARRAY $lista *}
            {set $lista=$lista|append($nodeid,$name)}
          {/if}
        {/foreach}
        {undef $books, $thisbook, $name, $nodeid, $japassou}

        {* ORDERING OBJECTS/TITLES ARRAY BY TITLE*}
        {* "sort" operator returns this kind of array: *}
        {* Array ( [0]=> 6 [1] => 112 [2] => Casa Velha [3] => 111 [4] => Memórias Póstumas de Brás Cubas [5] => 130 [6] => Sempre não ) *}
        {* i.e., an array(objectid, title, objectid, title...) titles already alphabetically ordered, first cell= sizeof array *}
        {set lista = $lista|sort}

        {* SWEEPING THE ARRAY AND BUILDING THE HREF LINKS *}
        {* Note: $lista.0 contains the size of the array *}
        {def $sizeoflista=$lista.0}
        {def $i=1}
        {def $j=1}
        {def $titulo=''}
        {while lt($i,$sizeoflista)}
          {* WE NEED THE OBJECT'S NODE_ID, NOT ITS OBJECT_IDs, SO... LET'S GET IT *}
          {def $object=fetch( 'content', 'object', hash( 'object_id', $lista[$i] ) )}
          {def $nodeid=$object.main_node_id}
          {undef $object}
          {set $j=inc($i)}
          {set $titulo=$lista[$j]|wash}
           <a href={concat('/content/view/full/',$nodeid)|ezurl}>{$titulo}</a><BR />
          {undef $object, $nodeid, $titulo}
          {set $i=sum($i,2)}
        {/while}

        {undef}

And here is the sort function, which I put in index.php:

function my_sort($array) {
  $i=0;
  while ($i<sizeof($array)) {
    $j=$i+1;
    $node=$array[$i];
    $nome=$array[$j];
    $newarray["$node"]=$nome;
    $i=$i+2;
  }
  asort($newarray);
  reset($newarray);
  $lista="";
  while (list ($key, $val) = each ($newarray) ) {
    if ($lista<>"") {
      $lista.="ÿ";
    }
    $lista.=$key."ÿ".$val;
  }
  $newarray=explode("ÿ",$lista);

  array_unshift($newarray, sizeof($newarray)); // puts sizeof $newarray into it's 1st cell

  return $newarray;
}

I hope this helps. If anyone could do all this in a more ellegant way, please share!

Roberto