Example #1
0
function OrionDB_Create($requestedResource)
{
    /// Function to create a record of a specific type in the database - to search for an old one.
    /// If it's to create new records, it should send back the record containing both the new ID and the old _guid.
    /// If it's to search, it should send back an array of matching SC GUIDs.
    // find out if you have a search or not...
    if ($_POST['search_string']) {
        // you have a search...
        // build the query, then run it
        $tmpInfo = new OrionDB_QueryInfo();
        // iterate through $_POST
        $tmpInfo->tablename = $requestedResource;
        $tmpInfo->fieldnamelist = 'guid';
        $searchColumnsAry = explode(',', $_POST['search_columns']);
        foreach ($searchColumnsAry as $columnName) {
            // give the search string wildcard ears:
            $tmpInfo->conditions[$columnName] = '%' . $_POST['search_string'] . '%';
        }
        $tmpQueryObject = new OrionDB_Query();
        $query = $tmpQueryObject->createSelectQuery($tmpInfo);
        global $ORIONDB_DB;
        $outgoingArray = $ORIONDB_DB->runquery($requestedResource, $query);
        // ready? respond!
        $testing = array();
        foreach ($outgoingArray as $returnedRecord) {
            $testing[] = $returnedRecord['guid'];
        }
        echo json_encode($testing);
    } else {
        // you don't have a search...
        // all records to create are in the $_POST
        $incomingRecordsToCreate = json_decode($_POST['records']);
        // if malformed JSON, it'd better die here :)
        if ($incomingRecordsToCreate) {
            // $incomingRecordsToCreate is an array so iterate
            // but first get ourselves an empty OrionDB_Collection object to send data back
            $outgoingRecords = array();
            // create working object
            $workingObject = eval("return new " . $requestedResource . "_class;");
            foreach ($incomingRecordsToCreate as $key => $value) {
                // we need to save the id so SC will know what record to update
                // it is sent along in both the id property as the _guid property
                // so remove both from the object we pass along, but keep 'em here
                $SC_guid = $value->_guid;
                unset($value->_guid);
                unset($value->id);
                // next create a new record
                $workingObject->create($value);
                // now put back the SC temp guid
                $workingObject->_guid = $SC_guid;
                // put the record in the collection
                $outgoingRecords[] = clone $workingObject;
            }
            // ready? send back the new record(s)
            echo json_encode($outgoingRecords);
            //echo json_encode($workingObject);
        }
    }
}
Example #2
0
 function __construct($info = null)
 {
     /// function to construct the collection object
     /// \param[in] $info An object containing at least the property tablename
     /// $info can also contain an object called conditions
     /// The conditions object recognises the following properties
     /// - ids : return only ids in this comma separated string
     /// - order : sort order
     /// - fieldnames : add a condition based on a specific field name. If the field name is not
     ///                a property of the object, it is ignored
     // we want to be able to call this function without parameters, so we cannot
     // check at the entrance. So, let's make a check here: if $info is not of the correct type,
     // make an empty collection object
     global $ORIONDB_DB;
     if ($info instanceof OrionDB_QueryInfo) {
         // first find out whether the table name exists. We can find out by asking the DB plugin
         // get basic information from the object
         $tableNameExists = property_exists($info, 'tablename');
         $conditionsFieldExists = property_exists($info, 'conditions');
         $tablename = $info->tablename;
         $table_exists = $ORIONDB_DB->table_exists($tablename);
         if ($tableNameExists && $table_exists) {
             // even when $info->fieldnamelist is set, override it to only get all ids for this table
             $info->fieldnamelist = "id";
             //print_r($info);
             $tmpQueryObject = new OrionDB_Query();
             $query = $tmpQueryObject->createSelectQuery($info);
             //echo $query;
             $queryresult = $ORIONDB_DB->runquery($tablename, $query);
             $numrows = count($queryresult);
             if ($numrows > 0) {
                 for ($index = 0; $index < $numrows; $index++) {
                     $currentrecord = $queryresult[$index];
                     $currentid = $currentrecord['id'];
                     $newobject = eval("return new " . $tablename . "_class;");
                     // modify init of 'OrionDB_Object' to allow second, column limiting argument ('+' delineated string)
                     if (array_key_exists('returncolumns', $info->conditions)) {
                         $newobject->init($currentid, $info->conditions['returncolumns']);
                     } else {
                         $newobject->init($currentid);
                     }
                     $this->records[] = $newobject;
                     // add the new object to the record array
                     $this->ids[] = $currentid;
                     // idem for the id
                 }
             }
         } else {
             // this part is executed when the loaded class is not an instance of OrionDB_Object
             // remove the properties to show an empty object
             unset($this->records);
             unset($this->ids);
         }
     }
     // end if $info instanceof
 }
Example #3
0
 function init_by_query(OrionDB_QueryInfo $info)
 {
     /// Function to init an object by QueryInfo object. Used by the ORIONDB authentication module
     /// to get the passwords etc. So, it does not filter out the filtered fields set in the config
     global $ORIONDB_DB;
     $tmpQueryObject = new OrionDB_Query();
     $query = $tmpQueryObject->createSelectQuery($info);
     $tmprecord = $ORIONDB_DB->getrecordbyquery($this->_tablename, $query);
     if ($tmprecord) {
         foreach ($tmprecord as $key => $value) {
             if (is_string($key)) {
                 // for some strange reason foreach runs every item twice, first with string association and
                 // second with the index number itself. Only the association is of any interest here.
                 $codetoeval = "\$this->{$key} = '{$value}';";
                 //logmessage($codetoeval);
                 eval($codetoeval);
             }
         }
         return true;
     } else {
         return false;
     }
 }