public function actionAdd()
 {
     $addOnId = $this->_input->filterSingle('addon_id', XenForo_Input::STRING);
     if (!$addOnId) {
         $addOnModel = $this->_getAddOnModel();
         $viewParams = array('addOnOptions' => $addOnModel->getAddOnOptionsListIfAvailable());
         return $this->responseView('ThemeHouse_Controllers_ViewAdmin_ControllerAdmin_Add_ChooseAddOn', 'th_controller_admin_choose_addon_controllers', $viewParams);
     }
     $dataWriter = $this->_input->filterSingle('datawriter', XenForo_Input::STRING);
     if (!$dataWriter) {
         $dataWriters = array();
         $rootPath = XenForo_Autoloader::getInstance()->getRootDir();
         $dataWriterPath = $rootPath . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $addOnId) . DIRECTORY_SEPARATOR . 'DataWriter';
         if (!file_exists($dataWriterPath)) {
             return $this->responseError(new XenForo_Phrase('th_no_datawriters_in_this_addon_controller'));
         }
         $directory = new RecursiveDirectoryIterator($dataWriterPath);
         $iterator = new RecursiveIteratorIterator($directory);
         $regex = new RegexIterator($iterator, '/^.+\\.php$/i', RecursiveRegexIterator::GET_MATCH);
         foreach ($regex as $fileinfo) {
             $classPath = str_replace($rootPath, '', $fileinfo[0]);
             $classPath = pathinfo($classPath, PATHINFO_DIRNAME) . DIRECTORY_SEPARATOR . pathinfo($classPath, PATHINFO_FILENAME);
             $dirs = explode(DIRECTORY_SEPARATOR, $classPath);
             $dirs = array_filter($dirs);
             $className = implode('_', $dirs);
             $dataWriters[$className] = $className;
         }
         $viewParams = array('dataWriters' => $dataWriters, 'addOnSelected' => $addOnId);
         return $this->responseView('ThemeHouse_Controllers_ViewAdmin_ControllerAdmin_Add_ChooseDataWriter', 'th_controller_admin_choose_dw_controllers', $viewParams);
     }
     $dataWriterClassName = $dataWriter;
     if (substr($dataWriterClassName, 0, strlen($addOnId . '_DataWriter_')) != $addOnId . '_DataWriter_') {
         return $this->responseNoPermission();
     }
     $class = $addOnId . '_ControllerAdmin_' . substr($dataWriterClassName, strlen($addOnId . '_DataWriter_'));
     $name = substr(strrchr($dataWriter, '_'), 1);
     $dataWriter = XenForo_DataWriter::create($dataWriter);
     if (!$dataWriter || !$dataWriter instanceof XenForo_DataWriter) {
         return $this->responseNoPermission();
     }
     $fields = $dataWriter->getFields();
     $selectedFields = array();
     $titleField = '';
     foreach ($fields as $table => $tableFields) {
         foreach ($tableFields as $fieldId => $field) {
             $titleField = $fieldId;
             if (empty($field['autoIncrement'])) {
                 $selectedFields[$table][$fieldId] = 1;
                 break 2;
             }
         }
     }
     $dataWriterReflectionClass = new ThemeHouse_Reflection_Class(get_class($dataWriter));
     $getModelMethod = '_get' . $name . 'Model';
     /* @var $reflectionMethod ThemeHouse_Reflection_Method */
     $dataWriterReflectionMethod = $dataWriterReflectionClass->getMethod($getModelMethod, 'ThemeHouse_Reflection_Method');
     $modelClassName = $dataWriterReflectionMethod->getReturnTag();
     $model = XenForo_Model::create($modelClassName);
     if (!$model || !$model instanceof XenForo_Model) {
         return $this->responseNoPermission();
     }
     $modelReflectionClass = new ThemeHouse_Controllers_Reflection_Class_Model(get_class($model));
     $pluralName = $modelReflectionClass->getPluralName();
     $routePrefix = ThemeHouse_Controllers_Helper_RoutePrefix::camelCaseToHyphenCase($pluralName);
     $method = $modelReflectionClass->findGetAllMethod($pluralName, false);
     $viewParams = array('dataWriter' => $dataWriterClassName, 'model' => $modelClassName, 'addOnSelected' => $addOnId, 'method' => $method, 'routePrefix' => $routePrefix, 'fields' => $fields, 'selectedFields' => $selectedFields, 'titleField' => $titleField, 'class' => $class);
     return $this->responseView('ThemeHouse_Controllers_ViewAdmin_ControllerAdmin_Add', 'th_controller_admin_add_controllers', $viewParams);
 }
 protected function _createFunctionGetOrError(array $options)
 {
     $name = $options['name'];
     $lcFirstName = lcfirst($name);
     $function = $this->createFunction('_get' . $name . 'OrError');
     $function->setPhpDoc(array('', '@return array'));
     $primaryKeyId = $options['primary_key_id'];
     $camelCasePrimaryKeyId = ThemeHouse_Reflection_Helper_Template::snakeCaseToCamelCase($primaryKeyId);
     $lcFirstPrimaryKeyId = lcfirst($camelCasePrimaryKeyId);
     $function->setSignature(array('$' . $lcFirstPrimaryKeyId));
     $snakeCase = ThemeHouse_Reflection_Helper_Template::camelCaseToSnakeCase($options['name']);
     $addOnId = $options['addon_id'];
     $notFoundPhrase = ThemeHouse_Reflection_Helper_Phrase::getPhraseName($snakeCase . '_not_found', $addOnId);
     $model = XenForo_Model::create($options['model']);
     $modelReflectionClass = new ThemeHouse_Controllers_Reflection_Class_Model(get_class($model));
     if ($modelReflectionClass->hasMethod('prepare' . $name)) {
         $body = array('$' . $lcFirstName . 'Model = $this->_get' . $options['name'] . 'Model();', '', 'return $' . $lcFirstName . 'Model->prepare' . $options['name'] . '(', "\t" . '$this->getRecordOrError($' . $lcFirstPrimaryKeyId . ', $' . $lcFirstName . 'Model, \'get' . $options['name'] . 'ById\',', "\t\t" . '\'' . $notFoundPhrase . '\'));');
     } else {
         $body = array('return $this->getRecordOrError($' . $lcFirstPrimaryKeyId . ', $this->_get' . $options['name'] . 'Model(), \'get' . $options['name'] . 'ById\',', "\t" . '\'' . $notFoundPhrase . '\');');
     }
     $function->setBody($body);
 }