Ejemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function renderAllFields($mode, $filterFields = array(), $readOnlyFields = array())
 {
     self::$logger->debug('>>renderAllFields(fields=[' . var_export($fields, true) . '])');
     $json = json_encode(array_diff($this->BO->toArray(), $filterFields));
     self::$logger->debug('<<renderAllFields [JSON]');
     return $json;
 }
Ejemplo n.º 2
0
 /**
  * Returns the raw PDF data stream.
  *
  * @return string
  *
  * @since 2.0
  */
 public function getPDFData()
 {
     $PDFDownloadName = str_replace(' ', '-', $this->BO->get('title') . '.pdf');
     // first load the file
     $handle = fopen($this->PDFFilename, 'r');
     $data = fread($handle, filesize($this->PDFFilename));
     fclose($handle);
     return $data;
 }
Ejemplo n.º 3
0
 /**
  * Saves the HTML generated by Markdown to the cache directory.
  *
  * @throws Alpha\Exception\AlphaException
  *
  * @since 1.0
  */
 private function cache()
 {
     // check to ensure that the article is not transient before caching it
     if (!$this->BO->isTransient() && $this->filename != '') {
         $fp = fopen($this->filename, 'w');
         if (!$fp) {
             throw new AlphaException('Failed to open the cache file for writing, directory permissions my not be set correctly!');
         } else {
             flock($fp, 2);
             // locks the file for writting
             fwrite($fp, $this->content);
             flock($fp, 3);
             // unlocks the file
             fclose($fp);
             //closes the file
         }
     }
 }
Ejemplo n.º 4
0
 /**
  * Returns the output as an Excel spreadsheet.
  *
  * @param bool $renderHeaders Set to false to supress headers in the spreadsheet (defaults to true).
  *
  * @return string
  *
  * @since 1.0
  */
 public function render($renderHeaders = true)
 {
     self::$logger->debug('>>render()');
     //define separator (tabbed character)
     $sep = "\t";
     $output = '';
     // get the class attributes
     $reflection = new \ReflectionClass(get_class($this->BO));
     $properties = $reflection->getProperties();
     // print headers
     if ($renderHeaders) {
         $output .= $this->BO->getDataLabel('OID') . $sep;
         foreach ($properties as $propObj) {
             $propName = $propObj->name;
             if (!in_array($propName, $this->BO->getTransientAttributes()) && !in_array($propName, $this->BO->getDefaultAttributes())) {
                 $output .= $this->BO->getDataLabel($propName) . $sep;
             }
         }
         $output .= "\n";
     }
     // print values
     $output .= $this->BO->getOID() . $sep;
     foreach ($properties as $propObj) {
         $propName = $propObj->name;
         $prop = $this->BO->getPropObject($propName);
         if (!in_array($propName, $this->BO->getTransientAttributes()) && !in_array($propName, $this->BO->getDefaultAttributes())) {
             if (get_class($prop) == 'DEnum') {
                 $output .= $prop->getDisplayValue() . $sep;
             } elseif (get_class($prop) == 'Relation') {
                 $output .= $prop->getRelatedClassDisplayFieldValue() . $sep;
             } else {
                 $output .= preg_replace("/[\n\r]/", '', $prop->getValue()) . $sep;
             }
         }
     }
     $output .= "\n";
     self::$logger->debug('<<render');
     return $output;
 }
Ejemplo n.º 5
0
 /**
  * Cast a BO to another type of BO.  A new BO will be returned with the same OID and
  * version_num as the old BO, so this is NOT a true cast but is a copy.  All attribute
  * values will be copied accross.
  *
  * @param string                   $targetClassName The fully-qualified name of the target BO class.
  * @param Alpha\Model\ActiveRecord $originalBO      The original business object.
  *
  * @return Alpha\Model\ActiveRecord The new business object resulting from the cast.
  *
  * @since 1.0
  */
 public function cast($targetClassName, $originalBO)
 {
     $BO = new $targetClassName();
     $BO->setOID($originalBO->getOID());
     $BO->setVersion($originalBO->getVersion());
     // get the class attributes
     $originalBOreflection = new ReflectionClass(get_class($originalBO));
     $originalBOproperties = $originalBOreflection->getProperties();
     $newBOreflection = new ReflectionClass($targetClassName);
     $newBOproperties = $newBOreflection->getProperties();
     // copy the property values from the old BO to the new BO
     if (count($originalBOproperties) < count($newBOproperties)) {
         // the original BO is smaller, so loop over its properties
         foreach ($originalBOproperties as $propObj) {
             $propName = $propObj->name;
             if (!in_array($propName, $this->transientAttributes)) {
                 $BO->set($propName, $originalBO->get($propName));
             }
         }
     } else {
         // the new BO is smaller, so loop over its properties
         foreach ($newBOproperties as $propObj) {
             $propName = $propObj->name;
             if (!in_array($propName, $this->transientAttributes)) {
                 $BO->set($propName, $originalBO->get($propName));
             }
         }
     }
     return $BO;
 }
Ejemplo n.º 6
0
 /**
  * Dynamically binds all of the attributes for the current BO to the supplied prepared statement
  * parameters.  If arrays of attribute names and values are provided, only those will be bound to
  * the supplied statement.
  *
  * @param mysqli_stmt $stmt The SQL statement to bind to.
  * @param array Optional array of BO attributes.
  * @param array Optional array of BO values.
  *
  * @return mysqli_stmt
  *
  * @since 1.1
  */
 private function bindParams($stmt, $attributes = array(), $values = array())
 {
     self::$logger->debug('>>bindParams(stmt=[' . var_export($stmt, true) . '])');
     $bindingsTypes = '';
     $params = array();
     // here we are only binding the supplied attributes
     if (count($attributes) > 0 && count($attributes) == count($values)) {
         $count = count($values);
         for ($i = 0; $i < $count; ++$i) {
             if (Validator::isInteger($values[$i])) {
                 $bindingsTypes .= 'i';
             } else {
                 $bindingsTypes .= 's';
             }
             array_push($params, $values[$i]);
         }
         if ($this->BO->isTableOverloaded()) {
             if (isset($this->classname)) {
                 $bindingsTypes .= 's';
                 array_push($params, $this->classname);
             } else {
                 $bindingsTypes .= 's';
                 array_push($params, get_class($this->BO));
             }
         }
     } else {
         // bind all attributes on the business object
         // get the class attributes
         $reflection = new ReflectionClass(get_class($this->BO));
         $properties = $reflection->getProperties();
         foreach ($properties as $propObj) {
             $propName = $propObj->name;
             if (!in_array($propName, $this->BO->getTransientAttributes())) {
                 // Skip the OID, database auto number takes care of this.
                 if ($propName != 'OID' && $propName != 'version_num') {
                     if ($this->BO->getPropObject($propName) instanceof Integer) {
                         $bindingsTypes .= 'i';
                     } else {
                         $bindingsTypes .= 's';
                     }
                     array_push($params, $this->BO->get($propName));
                 }
                 if ($propName == 'version_num') {
                     $temp = $this->BO->getVersionNumber()->getValue();
                     $this->BO->set('version_num', $temp + 1);
                     $bindingsTypes .= 'i';
                     array_push($params, $this->BO->getVersionNumber()->getValue());
                 }
             }
         }
         if ($this->BO->isTableOverloaded()) {
             if (isset($this->classname)) {
                 $bindingsTypes .= 's';
                 array_push($params, $this->classname);
             } else {
                 $bindingsTypes .= 's';
                 array_push($params, get_class($this->BO));
             }
         }
         // the OID may be on the WHERE clause for UPDATEs and DELETEs
         if (!$this->BO->isTransient()) {
             $bindingsTypes .= 'i';
             array_push($params, $this->BO->getOID());
         }
     }
     self::$logger->debug('bindingsTypes=[' . $bindingsTypes . '], count: [' . mb_strlen($bindingsTypes) . ']');
     self::$logger->debug('params [' . var_export($params, true) . ']');
     if ($params != null) {
         $bind_names[] = $bindingsTypes;
         $count = count($params);
         for ($i = 0; $i < $count; ++$i) {
             $bind_name = 'bind' . $i;
             ${$bind_name} = $params[$i];
             $bind_names[] =& ${$bind_name};
         }
         call_user_func_array(array($stmt, 'bind_param'), $bind_names);
     }
     self::$logger->debug('<<bindParams [' . var_export($stmt, true) . ']');
     return $stmt;
 }
Ejemplo n.º 7
0
 /**
  * (non-PHPdoc).
  *
  * @see Alpha\Model\ActiveRecordProviderInterface::isTableOverloaded()
  */
 public function isTableOverloaded()
 {
     self::$logger->debug('>>isTableOverloaded()');
     $reflection = new ReflectionClass($this->BO);
     $classname = $reflection->getShortName();
     $tablename = ucfirst($this->BO->getTableName());
     // use reflection to check to see if we are dealing with a persistent type (e.g. DEnum) which are never overloaded
     $implementedInterfaces = $reflection->getInterfaces();
     foreach ($implementedInterfaces as $interface) {
         if ($interface->name == 'Alpha\\Model\\Type\\TypeInterface') {
             self::$logger->debug('<<isTableOverloaded [false]');
             return false;
         }
     }
     if ($classname != $tablename) {
         // loop over all BOs to see if there is one using the same table as this BO
         $BOclasses = ActiveRecord::getBOClassNames();
         foreach ($BOclasses as $BOclassName) {
             $reflection = new ReflectionClass($BOclassName);
             $classname = $reflection->getShortName();
             if ($tablename == $classname) {
                 self::$logger->debug('<<isTableOverloaded [true]');
                 return true;
             }
         }
         throw new BadTableNameException('The table name [' . $tablename . '] for the class [' . $classname . '] is invalid as it does not match a BO definition in the system!');
         self::$logger->debug('<<isTableOverloaded [false]');
         return false;
     } else {
         // check to see if there is already a "classname" column in the database for this BO
         $sqlQuery = 'PRAGMA table_info(' . $this->BO->getTableName() . ')';
         $result = self::getConnection()->query($sqlQuery);
         $this->BO->setLastQuery($sqlQuery);
         if (!$result) {
             self::$logger->warn('Error during pragma table info lookup [' . self::getLastDatabaseError() . ']');
         } else {
             while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
                 if ('classname' == $row['name']) {
                     self::$logger->debug('<<isTableOverloaded [true]');
                     return true;
                 }
             }
         }
         self::$logger->debug('<<isTableOverloaded [false]');
         return false;
     }
 }