Exemplo n.º 1
0
 /**
  * Selector for a module's tables or entities.
  *
  * This method is Backward Compatible with all Core versions back to 1.2.x
  * It scans for tables in `tables.php` as well as locating Doctrine 1 tables
  * or Doctrine 2 entities in either the 1.3.0 type directories or 1.4.0++ type
  *
  * @param string  $modname       Module name.
  * @param string  $name          Select field name.
  * @param string  $selectedValue Selected value.
  * @param string  $defaultValue  Value for "default" option.
  * @param string  $defaultText   Text for "default" option.
  * @param boolean $submit        Submit on choose.
  * @param string  $remove        Remove string from table name.
  * @param boolean $disabled      Add Disabled attribute to select.
  * @param integer $nStripChars   Strip the first n characters.
  * @param integer $multipleSize  Size for multiple selects.
  *
  * @return string The rendered output.
  */
 public static function getSelector_ModuleTables($modname, $name, $selectedValue = '', $defaultValue = 0, $defaultText = '', $submit = false, $remove = '', $disabled = false, $nStripChars = 0, $multipleSize = 1)
 {
     if (!$modname) {
         throw new \Exception(__f('Invalid %1$s passed to %2$s.', array('modname', 'HtmlUtil::getSelector_ModuleTables')));
     }
     // old style 'tables.php' modules (Core 1.2.x--)
     $tables = ModUtil::dbInfoLoad($modname, '', true);
     $data = array();
     if (is_array($tables) && $tables) {
         foreach ($tables as $k => $v) {
             if (strpos($k, '_column') === false && strpos($k, '_db_extra_enable') === false && strpos($k, '_primary_key_column') === false) {
                 $checkColumns = $k . '_column';
                 if (!isset($tables[$checkColumns])) {
                     continue;
                 }
             }
             if (strpos($k, '_column') === false && strpos($k, '_db_extra_enable') === false && strpos($k, '_primary_key_column') === false) {
                 if (strpos($k, 'z_') === 0) {
                     $k = substr($k, 4);
                 }
                 if ($remove) {
                     $k2 = str_replace($remove, '', $k);
                 } else {
                     $k2 = $k;
                 }
                 if ($nStripChars) {
                     $k2 = ucfirst(substr($k2, $nStripChars));
                 }
                 // Use $k2 for display also (instead of showing the internal table name)
                 $data[$k2] = $k2;
             }
         }
     }
     if (!empty($data)) {
         return self::getSelector_Generic($name, $data, $selectedValue, $defaultValue, $defaultText, null, null, $submit, $disabled, $multipleSize);
     }
     // Doctrine 1 models (Core 1.3.0 - 1.3.5)
     DoctrineUtil::loadModels($modname);
     $records = Doctrine::getLoadedModels();
     $data = array();
     foreach ($records as $recordClass) {
         // remove records from other modules
         if (substr($recordClass, 0, strlen($modname)) != $modname) {
             continue;
         }
         // get table name of remove table prefix
         $tableNameRaw = Doctrine::getTable($recordClass)->getTableName();
         sscanf($tableNameRaw, Doctrine_Manager::getInstance()->getAttribute(Doctrine::ATTR_TBLNAME_FORMAT), $tableName);
         if ($remove) {
             $tableName = str_replace($remove, '', $tableName);
         }
         if ($nStripChars) {
             $tableName = ucfirst(substr($tableName, $nStripChars));
         }
         $data[$tableName] = $tableName;
     }
     if (!empty($data)) {
         return self::getSelector_Generic($name, $data, $selectedValue, $defaultValue, $defaultText, null, null, $submit, $disabled, $multipleSize);
     }
     // Doctrine 2 entities (Core 1.3.0++)
     // Core-2.0 spec
     $module = ModUtil::getModule($modname);
     if (null !== $module && !class_exists($module->getVersionClass())) {
         // this check just confirming a Core-2.0 spec bundle - remove in 2.0.0
         $capabilities = $module->getMetaData()->getCapabilities();
         if (isset($capabilities['categorizable'])) {
             $data = array();
             foreach ($capabilities['categorizable'] as $fullyQualifiedEntityName) {
                 $nameParts = explode('\\', $fullyQualifiedEntityName);
                 $entityName = array_pop($nameParts);
                 $data[$entityName] = $entityName;
             }
             $selectedValue = count($data) == 1 ? $entityName : $defaultValue;
             return self::getSelector_Generic($name, $data, $selectedValue, $defaultValue, $defaultText, null, null, $submit, $disabled, $multipleSize);
         }
     }
     // (Core-1.3 spec)
     $modinfo = ModUtil::getInfo(ModUtil::getIdFromName($modname));
     $modpath = $modinfo['type'] == ModUtil::TYPE_SYSTEM ? 'system' : 'modules';
     $osdir = DataUtil::formatForOS($modinfo['directory']);
     $entityDirs = array("{$modpath}/{$osdir}/Entity/", "{$modpath}/{$osdir}/lib/{$osdir}/Entity/");
     $entities = array();
     foreach ($entityDirs as $entityDir) {
         if (file_exists($entityDir)) {
             $files = scandir($entityDir);
             foreach ($files as $file) {
                 if ($file != '.' && $file != '..' && substr($file, -4) === '.php') {
                     $entities[] = $file;
                 }
             }
         }
     }
     $data = array();
     foreach ($entities as $entity) {
         $possibleClassNames = array($modname . '_Entity_' . substr($entity, 0, strlen($entity) - 4));
         if ($module) {
             $possibleClassNames[] = $module->getNamespace() . '\\Entity\\' . substr($entity, 0, strlen($entity) - 4);
             // Core 1.4.0++
         }
         foreach ($possibleClassNames as $class) {
             if (class_exists($class)) {
                 $entityName = substr($entity, 0, strlen($entity) - 4);
                 if ($remove) {
                     $entityName = str_replace($remove, '', $entityName);
                 }
                 if ($nStripChars) {
                     $entityName = ucfirst(substr($entityName, $nStripChars));
                 }
                 $data[$entityName] = $entityName;
             }
         }
     }
     return self::getSelector_Generic($name, $data, $selectedValue, $defaultValue, $defaultText, null, null, $submit, $disabled, $multipleSize);
 }
Exemplo n.º 2
0
 /**
  * Selector for a module's tables.
  *
  * @param string  $modname       Module name.
  * @param string  $name          Select field name.
  * @param string  $selectedValue Selected value.
  * @param string  $defaultValue  Value for "default" option.
  * @param string  $defaultText   Text for "default" option.
  * @param boolean $submit        Submit on choose.
  * @param string  $remove        Remove string from table name.
  * @param boolean $disabled      Add Disabled attribute to select.
  * @param integer $nStripChars   Strip the first n characters.
  * @param integer $multipleSize  Size for multiple selects.
  *
  * @return string The rendered output.
  */
 public static function getSelector_ModuleTables($modname, $name, $selectedValue = '', $defaultValue = 0, $defaultText = '', $submit = false, $remove = '', $disabled = false, $nStripChars = 0, $multipleSize = 1)
 {
     if (!$modname) {
         return z_exit(__f('Invalid %1$s passed to %2$s.', array('modname', 'HtmlUtil::getSelector_ModuleTables')));
     }
     $tables = ModUtil::dbInfoLoad($modname, '', true);
     $data = array();
     if (is_array($tables) && $tables) {
         foreach ($tables as $k => $v) {
             if (strpos($k, '_column') === false && strpos($k, '_db_extra_enable') === false && strpos($k, '_primary_key_column') === false) {
                 $checkColumns = $k . '_column';
                 if (!isset($tables[$checkColumns])) {
                     continue;
                 }
             }
             if (strpos($k, '_column') === false && strpos($k, '_db_extra_enable') === false && strpos($k, '_primary_key_column') === false) {
                 if (strpos($k, 'z_') === 0) {
                     $k = substr($k, 4);
                 }
                 if ($remove) {
                     $k2 = str_replace($remove, '', $k);
                 } else {
                     $k2 = $k;
                 }
                 if ($nStripChars) {
                     $k2 = ucfirst(substr($k2, $nStripChars));
                 }
                 // Use $k2 for display also (instead of showing the internal table name)
                 $data[$k2] = $k2;
             }
         }
     }
     // Doctrine models
     DoctrineUtil::loadModels($modname);
     $records = Doctrine::getLoadedModels();
     foreach ($records as $recordClass) {
         // remove records from other modules
         if (substr($recordClass, 0, strlen($modname)) != $modname) {
             continue;
         }
         // get table name of remove table prefix
         $tableNameRaw = Doctrine::getTable($recordClass)->getTableName();
         sscanf($tableNameRaw, Doctrine_Manager::getInstance()->getAttribute(Doctrine::ATTR_TBLNAME_FORMAT), $tableName);
         if ($remove) {
             $tableName = str_replace($remove, '', $tableName);
         }
         if ($nStripChars) {
             $tableName = ucfirst(substr($tableName, $nStripChars));
         }
         $data[$tableName] = $tableName;
     }
     // Doctrine2 models
     $modinfo = ModUtil::getInfo(ModUtil::getIdFromName($modname));
     $modpath = $modinfo['type'] == ModUtil::TYPE_SYSTEM ? 'system' : 'modules';
     $osdir = DataUtil::formatForOS($modinfo['directory']);
     $entityDir = "{$modpath}/{$osdir}/lib/{$osdir}/Entity/";
     $entities = array();
     if (file_exists($entityDir)) {
         $entities = scandir($entityDir);
     }
     foreach ($entities as $entity) {
         if (!($entity[0] != '.' && substr($entity, -4) === '.php')) {
             continue;
         }
         $class = $modname . '_Entity_' . substr($entity, 0, strlen($entity) - 4);
         if (class_exists($class) && !in_array('Doctrine_Record', class_parents($class))) {
             $tableName = substr($entity, 0, strlen($entity) - 4);
             if ($remove) {
                 $tableName = str_replace($remove, '', $tableName);
             }
             if ($nStripChars) {
                 $tableName = ucfirst(substr($tableName, $nStripChars));
             }
             $data[$tableName] = $tableName;
         }
     }
     return self::getSelector_Generic($name, $data, $selectedValue, $defaultValue, $defaultText, null, null, $submit, $disabled, $multipleSize);
 }