Auxiliary snippets: ItemDropList

In management lists for items using menuindex or a numerical rank field, the ItemDropList snippet generates selection lists to be used with Add Item and Move Item buttons.


ItemDropList

<?php
/*

/**************** ItemDropList ****************************
* Create a drop-down selection list offering 1 to n or 1 to n+1
* for folder containing n items
* for use with list management pages' Move and Add buttons
********************************************************/


/* parameters:
    &action: add or move

    &sourceOnly: produce only the first list (item ids as values) (for moves
    outside the current screen)
    default: 0;

    &parent: ID of container document (this or &table required)

    &table: mySQL table to be counted (this or &parent required)

    &orderBy: standard mySql parameter; default 'rank'

    &showUnpublished: default 0;

    &name: name attribute for numerical selection list
    (destination in "from: to" lists) (required);

    &srcName: name attribute for ID of item to move. Default 'docId'.
    (Required if you have several move lists in one form)

    &extra: extend list of options by this many (usually 1 for Add, 0 for Move)
    default: 0;

    &default: preselect first or last item ('first', 'last')
    default: first;

   output: XHTML selection list as a string
*/

$output = NULL;
$nl = "\n";

$action = (isset($action)) ? $action : 'add';
$srcName = (isset($srcName)) ? $srcName : 'docId';
$sourceOnly = (isset($sourceOnly)) ? $sourceOnly : 0;
$extra = (isset($extra)) ? $extra : 0;
$where = (isset($where)) ? str_replace('_EQ_', '=', $where) : NULL;
$orderBy = (isset($orderBy)) ? $orderBy : 'rank';

$childIds = array();

if (!empty($table)) {
    $table = $modx->db->config['table_prefix'] . $table;
    $finalIndex = $extra + $modx->db->getValue(
        $modx->db->select("COUNT(*)", $table, $where)
        );
    $members = $modx->db->select('id',$table, $where, $orderBy);
    while ($child = $modx->db->getRow($members)) {
        $childIds[] = $child['id'];
    }
}
elseif (!empty($parent) && !empty($name)) {
// Get child IDs list from DB. MODx getChildIds() can be out of date list
    $table = $modx->getFullTableName("site_content");
    $children = $modx->db->select('id',$table,'parent='.$parent, $name.' ASC');
    while ($child = $modx->db->getRow($children)) {
        $childIds[] = $child['id'];
    }
    $finalIndex = $modx->db->getRecordCount($children) + $extra;
}

else {
    $action = 'exit';
}

switch ($action) {
case 'add':
    $output .= '</select>'.$nl;
    $output .= '<select class="itempos" name="'.$name.'" title="Add item" tabindex="1">'.$nl;
    for ($i=1;$i<=$finalIndex;$i++) {
        $output .= '<option value="'.$i.'">'.$i.'</option>'.$nl;
    }
    $output .= '</select>'.$nl;
    break;

case 'move':
    $output .= '<select class="itempos" name="' . $srcName . '" title="Move item" tabindex="1">'.$nl;
    $i = 1;
    foreach ($childIds as $childId) {
        $output .= '<option value="'.$childId.'">'.$i++.'</option>'.$nl;
    }
    $output .= '</select>'.$nl;

    if ($sourceOnly != 1) {
        $output .= ' to ';

        $output .= '<select class="itempos" name="'.$name.'" title="Move item" tabindex="1">'.$nl;
        for ($i=1;$i<=($finalIndex - $extra);$i++) {
            $output .= '<option value="'.$i.'">'.$i.'</option>'.$nl;
        }
        $output .= '</select>'.$nl;
    }
}

return $output;
?>