/* reset/default variables */
$id = 31; /* resource ID of MixReviewAggregator document */
$issueCounter = 0;
$articleCounter = 0;
$reviewCounter = 0;
if ($issueCount == null) {$issueCount = 10000;}

/* formatting: begin list */
$list = '<ul>';

/* select the current document for get() queries */
/* NB. redundant, but functional: $id = $modx->resource->get('id'); */
$resource = $modx->getObject('modResource', $id);

/* poll the MixReviewAggregator document for its first-generation children
   (ie. the SOS issue documents) */
$SOSIssueDocIDs = $modx->getChildIds($id, 1); /* NB. returns array!! */

/* selects each SOS Issue document for get() queries in turn,
   then polls it for its first-generation children
  (ie. the Track documents), the fills data accordingly */
foreach ($SOSIssueDocIDs as $key1 => $value1)
{
  $trackSOSLinkSuffixCounter = 0; /* reset variable */
  $resource = $modx->getObject('modResource', $value1);
  $articleCounter += 1;

/* only shows SOSIssue documents that are published */
  if ($resource->get('published') == 1)
  {

/* outputs formatted SOSIssue document data */
    if ($issueCounter < $issueCount)
    {
      $list .= '<li class="liMixReviewSOSIssue"><strong><a href="';
      $list .= $resource->getTVValue(6); /* TV: mixRev_SOSIssueLink */
      $list .= '">';
      $list .= $resource->getTVValue(3); /* TV: mixRev_SOSIssue */
      $list .= '</a></strong><ul>';
    }
    $TrackDocIDs = $modx->getChildIds($value1, 1);
    foreach ($TrackDocIDs as $key2 => $value2)
    {
      $reviewCounter += 1;
      $trackSOSLinkSuffixCounter += 1;
      $resource = $modx->getObject('modResource', $value2);
      if ($issueCounter < $issueCount)
      {

/* outputs formatted Track document data */
        $list .= '<li class="liMixReviewTrack">';
        $specialTrack = $resource->getTVValue(22);
        if ($specialTrack == 'Classic')
/* only adds 'Classic Mix' if nec */
        {
          $list .= '<span class="spanMixRevAggClassic"><strong>Classic Mix:</strong></span> ';
        }
        $list .= $resource->getTVValue(4); /* TV:mixRev_TrackArtist */
        $trackFeaturing = $resource->getTVValue(8);
        if ($trackFeaturing != null) /* only adds featured artists if nec */
        {
          $list .= ' (featuring ';
          $list .= $trackFeaturing; /* TV:mixRev_TrackComposer */
          $list .= ')';
        }
        $list .= ': ';
        $trackComposer = $resource->getTVValue(7);
        if ($trackComposer != null) /* only adds composer if nec */
        {
          $list .= $trackComposer; /* TV:mixRev_TrackComposer */
          $list .= ' ';
        }
        $list .= "'" . '<a href="';
        $altLink = $resource->getTVValue(24);
        if ($altLink == null)
/* only adds SOSAltLink if it's there */
        {
          $list .= $resource->getTVValue(6); /* TV:mixRev_SOSIssueLink */
          $list .= '#para';
          $list .= $trackSOSLinkSuffixCounter;
        }
        else
        {
          $list .= $resource->getTVValue(24); /* TV:mixRev_SOSAltLink */
        }
        $list .= '">';
        $list .= $resource->get('pagetitle');
        $list .= '</a>' . "'";
        if ($specialTrack == 'BonusQA')
/* only adds '(Bonus Q&A)' if nec */
        {
          $list .= ' <i>(Bonus Q&amp;A)</i>';
        }
        $list .= '</li>';
      }
    }
    if ($issueCounter < $issueCount)
    {
      $list .= '</ul>';
    }

/* increments $issueCounter */
    $issueCounter += 1;

  }
}


/* output assembly */
$output .= '<p><strong>' . "'" . 'The Mix Review' . "'";
$output .= ' chart productions analysed (' . $reviewCounter . ' critiques):</strong></p>';
$output .= $list;

if ($issueCount < $articleCounter)
  {
  $output .= '<li><a href="[[~428]]#MixReview">Click here to browse all ' . "'" . 'The Mix Review' . "'" . ' articles...</a></li>';
  }
else
  {
  $output .= '<li><strong>Introductory article:</strong> <a href="http://www.soundonsound.com/sos/apr11/articles/listen-and-learn.htm">Listen &amp; Learn</a>.</li>';
  $output .= '<li><strong>Further information and discussion on-line at the </strong> <a href="http://www.soundonsound.com/mixreview">SOS Forum</a>.</li>';
  $output .= '<li>(For a huge library of home-studio mix critiques, complete with audio examples and raw multitracks, check out the <a href="[[~350]]">Great ' . "'" . 'Blood To Bone' . "'" . ' Mixoff</a> and <a href="[[~615]]">Cambridge-MT Patrons Diesel13 ' . "'" . 'Colour Me Red' . "'" . ' Mix Contest</a> pages';
  }

$output .= '</ul>';


return $output;