Exemplo n.º 1
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;
 }
Exemplo n.º 2
0
 /**
  * (non-PHPdoc).
  *
  * @see Alpha\Model\ActiveRecordProviderInterface::setEnumOptions()
  * @since 1.1
  */
 public function setEnumOptions()
 {
     self::$logger->debug('>>setEnumOptions()');
     // get the class attributes
     $reflection = new ReflectionClass(get_class($this->BO));
     $properties = $reflection->getProperties();
     // flag for any database errors
     $dbError = false;
     foreach ($properties as $propObj) {
         $propName = $propObj->name;
         if (!in_array($propName, $this->BO->getDefaultAttributes()) && !in_array($propName, $this->BO->getTransientAttributes())) {
             $propClass = get_class($this->BO->getPropObject($propName));
             if ($propClass == 'Enum') {
                 $sqlQuery = 'SHOW COLUMNS FROM ' . $this->BO->getTableName() . " LIKE '{$propName}'";
                 $this->BO->setLastQuery($sqlQuery);
                 $result = self::getConnection()->query($sqlQuery);
                 if ($result) {
                     $row = $result->fetch_array(MYSQLI_NUM);
                     $options = explode("','", preg_replace("/(enum|set)\\('(.+?)'\\)/", '\\2', $row[1]));
                     $this->BO->getPropObject($propName)->setOptions($options);
                 } else {
                     $dbError = true;
                     break;
                 }
             }
         }
     }
     if (!$dbError) {
         if (method_exists($this, 'after_setEnumOptions_callback')) {
             $this->after_setEnumOptions_callback();
         }
     } else {
         throw new AlphaException('Failed to load enum options correctly for object instance of class [' . get_class($this) . ']');
     }
     self::$logger->debug('<<setEnumOptions');
 }
 /**
  * (non-PHPdoc).
  *
  * @see Alpha\Model\ActiveRecordProviderInterface::addProperty()
  */
 public function addProperty($propName)
 {
     self::$logger->debug('>>addProperty(propName=[' . $propName . '])');
     $sqlQuery = 'ALTER TABLE ' . $this->BO->getTableName() . ' ADD ';
     if ($this->isTableOverloaded() && $propName == 'classname') {
         $sqlQuery .= 'classname TEXT(100)';
     } else {
         if (!in_array($propName, $this->BO->getDefaultAttributes()) && !in_array($propName, $this->BO->getTransientAttributes())) {
             $reflection = new ReflectionClass($this->BO->getPropObject($propName));
             $propClass = $reflection->getShortName();
             switch (mb_strtoupper($propClass)) {
                 case 'INTEGER':
                     // special properties for RelationLookup OIDs
                     if ($this->BO instanceof RelationLookup && ($propName == 'leftID' || $propName == 'rightID')) {
                         $sqlQuery .= "{$propName} INTEGER(" . $this->BO->getPropObject($propName)->getSize() . ') NOT NULL,';
                     } else {
                         $sqlQuery .= "{$propName} INTEGER(" . $this->BO->getPropObject($propName)->getSize() . '),';
                     }
                     break;
                 case 'DOUBLE':
                     $sqlQuery .= "{$propName} REAL(" . $this->BO->getPropObject($propName)->getSize(true) . '),';
                     break;
                 case 'STRING':
                     $sqlQuery .= "{$propName} TEXT(" . $this->BO->getPropObject($propName)->getSize() . '),';
                     break;
                 case 'TEXT':
                     $sqlQuery .= "{$propName} TEXT,";
                     break;
                 case 'BOOLEAN':
                     $sqlQuery .= "{$propName} INTEGER(1) DEFAULT '0',";
                     break;
                 case 'DATE':
                     $sqlQuery .= "{$propName} TEXT,";
                     break;
                 case 'TIMESTAMP':
                     $sqlQuery .= "{$propName} TEXT,";
                     break;
                 case 'ENUM':
                     $sqlQuery .= "{$propName} TEXT,";
                     break;
                 case 'DENUM':
                     $tmp = new DEnum(get_class($this->BO) . '::' . $propName);
                     $sqlQuery .= "{$propName} INTEGER(11),";
                     break;
                 case 'RELATION':
                     $sqlQuery .= "{$propName} INTEGER(11),";
                     break;
                 default:
                     $sqlQuery .= '';
                     break;
             }
         }
     }
     $this->BO->setLastQuery($sqlQuery);
     if (!($result = self::getConnection()->query($sqlQuery))) {
         throw new AlphaException('Failed to add the new attribute [' . $propName . '] to the table [' . $this->BO->getTableName() . '], query is [' . $this->BO->getLastQuery() . ']');
         self::$logger->debug('<<addProperty');
     } else {
         self::$logger->info('Successfully added the [' . $propName . '] column onto the [' . $this->BO->getTableName() . '] table for the class [' . get_class($this->BO) . ']');
     }
     if ($this->BO->getMaintainHistory()) {
         $sqlQuery = str_replace($this->BO->getTableName(), $this->BO->getTableName() . '_history', $sqlQuery);
         if (!($result = self::getConnection()->query($sqlQuery))) {
             throw new AlphaException('Failed to add the new attribute [' . $propName . '] to the table [' . $this->BO->getTableName() . '_history], query is [' . $this->BO->getLastQuery() . ']');
             self::$logger->debug('<<addProperty');
         } else {
             self::$logger->info('Successfully added the [' . $propName . '] column onto the [' . $this->BO->getTableName() . '_history] table for the class [' . get_class($this->BO) . ']');
         }
     }
     self::$logger->debug('<<addProperty');
 }