Esempio n. 1
0
 public static function saveOrUpdateObjectFromFormData()
 {
     $model_class_name = POSTAccess::getRequiredPostValue(self::FIELD_CLASS_NAME);
     $object_id = POSTAccess::getOptionalPostValue(self::FIELD_OBJECT_ID);
     \OLOG\CheckClassInterfaces::exceptionIfClassNotImplementsInterface($model_class_name, \OLOG\Model\InterfaceSave::class);
     $new_prop_values_arr = [];
     $null_fields_arr = [];
     $reflect = new \ReflectionClass($model_class_name);
     foreach ($reflect->getProperties() as $prop_obj) {
         if (!$prop_obj->isStatic()) {
             // игнорируем статические свойства класса - они относятся не к объекту, а только к классу (http://www.php.net/manual/en/language.oop5.static.php), и в них хранятся настройки ActiveRecord и CRUD
             $prop_name = $prop_obj->getName();
             // сейчас если поля нет в форме - оно не будет изменено в объекте. это позволяет показывать в форме только часть полей, на остальные форма не повлияет
             if (array_key_exists($prop_name, $_POST)) {
                 // Проверка на заполнение обязательных полей делается на уровне СУБД, через нот нулл в таблице
                 $new_prop_values_arr[$prop_name] = $_POST[$prop_name];
             }
             // чтение возможных NULL
             if (array_key_exists($prop_name . "___is_null", $_POST)) {
                 if ($_POST[$prop_name . "___is_null"]) {
                     $null_fields_arr[$prop_name] = 1;
                 }
             }
         }
     }
     $obj = null;
     if ($object_id) {
         $obj = CRUDObjectLoader::createAndLoadObject($model_class_name, $object_id);
     } else {
         $obj = new $model_class_name();
     }
     $obj = CRUDFieldsAccess::setObjectFieldsFromArray($obj, $new_prop_values_arr, $null_fields_arr);
     $obj->save();
     return $obj->getId();
 }
 public function currentUserName()
 {
     $admin_actions_base_classname = AuthConfig::getAdminActionsBaseClassname();
     if (CheckClassInterfaces::classImplementsInterface($admin_actions_base_classname, InterfaceCurrentUserName::class)) {
         return (new $admin_actions_base_classname())->currentUserName();
     }
     return '';
 }
Esempio n. 3
0
 public static function logout()
 {
     $user_session_id = self::getSessionIdFromCookie();
     if ($user_session_id) {
         self::clearUserSession($user_session_id);
     }
     if (AuthConfig::getAfterLogoutCallbackClassName()) {
         \OLOG\CheckClassInterfaces::exceptionIfClassNotImplementsInterface(AuthConfig::getAfterLogoutCallbackClassName(), InterfaceAfterLogoutCallback::class);
         $events_class = AuthConfig::getAfterLogoutCallbackClassName();
         $events_class::afterLogoutCallback();
     }
 }
Esempio n. 4
0
 protected static function swapModelWeightOperation()
 {
     $model_class_name = POSTAccess::getRequiredPostValue('_class_name');
     // TODO: constant for field name
     \OLOG\CheckClassInterfaces::exceptionIfClassNotImplementsInterface($model_class_name, \OLOG\Model\InterfaceWeight::class);
     $model_id = POSTAccess::getRequiredPostValue('_id');
     // TODO: constant for field name
     $context_fields_names_str = POSTAccess::getRequiredPostValue(CRUDTableWidgetWeight::FORMFIELD_CONTEXT_FIELDS_NAME);
     $context_fields_names_arr = explode(',', $context_fields_names_str);
     $context_arr = [];
     foreach ($context_fields_names_arr as $context_field_name) {
         $context_arr[$context_field_name] = NullablePostFields::optionalFieldValue($context_field_name);
     }
     /** @var InterfaceWeight $obj */
     $obj = CRUDObjectLoader::createAndLoadObject($model_class_name, $model_id);
     $obj->swapWeights($context_arr);
     \OLOG\Redirects::redirectToSelf();
 }
Esempio n. 5
0
 public function afterSave()
 {
     $this->removeFromFactoryCache();
     if (AuthConfig::getUserAfterSaveCallbackClassName()) {
         \OLOG\CheckClassInterfaces::exceptionIfClassNotImplementsInterface(AuthConfig::getUserAfterSaveCallbackClassName(), InterfaceUserAfterSaveCallback::class);
         $events_class = AuthConfig::getUserAfterSaveCallbackClassName();
         $events_class::userAfterSaveCallback($this);
     }
 }
 /**
  * Возвращает одну страницу списка объектов указанного класса.
  * Сортировка: TODO.
  * Фильтры: массив $context_arr.
  * Как определяется страница: см. Pager.
  * @param $model_class_name Имя класса модели
  * @param $context_arr array Массив пар "имя поля" - "значение поля"
  * @return array Массив идентикаторов объектов.
  */
 public static function getObjIdsArrForClassName($table_index_on_page, $model_class_name, $filters_arr, $order_by = '')
 {
     \OLOG\CheckClassInterfaces::exceptionIfClassNotImplementsInterface($model_class_name, \OLOG\Model\InterfaceLoad::class);
     $page_size = Pager::getPageSize($table_index_on_page);
     $start = Pager::getPageOffset($table_index_on_page);
     $db_table_name = $model_class_name::DB_TABLE_NAME;
     $db_id = $model_class_name::DB_ID;
     $db_id_field_name = CRUDFieldsAccess::getIdFieldName($model_class_name);
     $query_param_values_arr = array();
     $where = ' 1 = 1 ';
     foreach ($filters_arr as $filter_obj) {
         /*
         if ($filter_obj instanceof InterfaceCRUDTableFilter) {
         
             $column_name = $filter_obj->getFieldName();
             $operation_code = $filter_obj->getOperationCode();
             $value = $filter_obj->getValue();
         
             $column_name = preg_replace("/[^a-zA-Z0-9_]+/", "", $column_name);
         
             switch ($operation_code) {
                 case CRUDTableFilter::FILTER_EQUAL:
                     $where .= ' and ' . $column_name . ' = ? ';
                     $query_param_values_arr[] = $value;
                     break;
         
                 case CRUDTableFilter::FILTER_IS_NULL:
                     $where .= ' and ' . $column_name . ' is null ';
                     break;
         
                 case CRUDTableFilter::FILTER_LIKE:
                     $where .= ' and ' . $column_name . ' like ? ';
                     $query_param_values_arr[] = '%' . $value . '%';
                     break;
         
                 case CRUDTableFilter::FILTER_IN:
                     if (count($value)) {
                         $in_placeholders_arr = [];
         
                         foreach ($value as $in_single_value) {
                             $in_placeholders_arr[] = '?';
                             $query_param_values_arr[] = $in_single_value;
                         }
         
                         $where .= ' and ' . $column_name . ' in (' . implode(', ', $in_placeholders_arr) . ') ';
                     }
                     break;
         
                 default:
                     throw new \Exception('unknown filter code');
             }
         } else
         */
         if ($filter_obj instanceof InterfaceCRUDTableFilter2) {
             list($filter_sql_condition, $filter_placeholder_values_arr) = $filter_obj->sqlConditionAndPlaceholderValue();
             if ($filter_sql_condition != '') {
                 $where .= ' and ' . $filter_sql_condition;
             }
             $query_param_values_arr = array_merge($query_param_values_arr, $filter_placeholder_values_arr);
         } elseif ($filter_obj instanceof InterfaceCRUDTableFilterInvisible) {
             list($filter_sql_condition, $filter_placeholder_values_arr) = $filter_obj->sqlConditionAndPlaceholderValue();
             if ($filter_sql_condition != '') {
                 $where .= ' and ' . $filter_sql_condition;
             }
             $query_param_values_arr = array_merge($query_param_values_arr, $filter_placeholder_values_arr);
         } else {
             throw new \Exception('filter doesnt implement InterfaceCRUDTableFilter nor InterfaceCRUDTableFilter2');
         }
     }
     if ($order_by == '') {
         $order_by = $db_id_field_name;
     }
     $obj_ids_arr = \OLOG\DB\DBWrapper::readColumn($db_id, "select " . $db_id_field_name . " from " . $db_table_name . ' where ' . $where . ' order by ' . $order_by . ' limit ' . intval($page_size) . ' offset ' . intval($start), $query_param_values_arr);
     return $obj_ids_arr;
 }
Esempio n. 7
0
 public static function createAndLoadObject($model_class_name, $obj_id)
 {
     \OLOG\CheckClassInterfaces::exceptionIfClassNotImplementsInterface($model_class_name, \OLOG\Model\InterfaceFactory::class);
     return $model_class_name::factory($obj_id);
 }
Esempio n. 8
0
 public function testClassImplementsInterface()
 {
     $this->assertTrue(\OLOG\CheckClassInterfaces::classImplementsInterface(\Tests\TestObject::class, \Tests\TestInterface::class));
     $this->assertFalse(\OLOG\CheckClassInterfaces::classImplementsInterface(\Tests\TestObject::class, \Tests\TestInterface2::class));
 }