Module HowTo - File upload




1. Defining upload columns

If you want to support account creation via file upload you have to define columns in the CSV file.
Each column has an non-translated identifier, a description, help entry and several other values.

The upload columns are specified with get_uploadColumns() or meta['upload_columns'].

Example:

The ieee802Device module has only one attribute and therefore one column: the MAC address.

    /**
    * Returns meta data that is interpreted by parent class
    *
    * @return array array with meta data
    */
    function get_metaData() {
        $return = array();
        // manages host accounts
        $return["account_types"] = array("host");
        // upload fields
        $return['upload_columns'] = array(
            array(
                'name' => 'ieee802Device_mac',
                'description' => _('MAC address'),
                'help' => 'mac',
                'example' => '00:01:02:DE:EF:18'
            )
        );
        return $return;
    }


2. Building the accounts

When the user has uploaded the CSV file the modules have to transform the input data to LDAP accounts.

This is done with build_uploadAccounts(). The function gets the input data and a list of LDAP accounts as parameter.

Example:

The ieee802Device module has only one LDAP attribute - 'macAddress' - and the 'ieee802Device' objectClass which is added to all accounts.

    /**
    * In this function the LDAP account is built up.
    *
    * @param array $rawAccounts list of hash arrays (name => value) from user input
    * @param array $partialAccounts list of hash arrays (name => value) which are later added to LDAP
    * @param array $ids list of IDs for column position (e.g. "posixAccount_uid" => 5)
    * @param array $selectedModules list of selected account modules
    * @return array list of error messages if any
    */
    function build_uploadAccounts($rawAccounts, $ids, &$partialAccounts, $selectedModules) {
        $messages = array();
        for ($i = 0; $i < sizeof($rawAccounts); $i++) {
            // add object class
            if (!in_array("ieee802Device", $partialAccounts[$i]['objectClass'])) $partialAccounts[$i]['objectClass'][] = "ieee802Device";
            // add MACs
            if ($rawAccounts[$i][$ids['ieee802Device_mac']] != "") {
                $macs = explode(',', $rawAccounts[$i][$ids['ieee802Device_mac']]);
                // check format
                for ($m = 0; $m < sizeof($macs); $m++) {
                    if (get_preg($macs[$m], 'macAddress')) {
                        $partialAccounts[$i]['macAddress'][] = $macs[$m];
                    }
                    else {
                        $errMsg = $this->messages['mac'][1];
                        array_push($errMsg, array($i));
                        $messages[] = $errMsg;
                    }
                }
            }
        }
        return $messages;
    }