/** * Reads the ordered/reversed column map for certain model * *<code> * print_r($metaData->readColumnMap(new Robots())); *</code> * * @param Phalcon\Mvc\ModelInterface $model * @return array */ PHP_METHOD(Phalcon_Mvc_Model_MetaData, readColumnMap){ zval *model, *key_name, *column_map = NULL, *null_value; zval *data; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 0, &model); PHALCON_VERIFY_INTERFACE_EX(model, phalcon_mvc_modelinterface_ce, phalcon_mvc_model_exception_ce, 1); if (!PHALCON_GLOBAL(orm).column_renaming) { RETURN_MM(); } PHALCON_INIT_VAR(key_name); phalcon_get_class(key_name, model, 1 TSRMLS_CC); PHALCON_OBS_VAR(column_map); phalcon_read_property_this(&column_map, this_ptr, SL("_columnMap"), PH_NOISY TSRMLS_CC); if (!phalcon_array_isset(column_map, key_name)) { null_value = PHALCON_GLOBAL(z_null); PHALCON_CALL_METHOD(NULL, this_ptr, "_initialize", model, null_value, null_value, null_value); PHALCON_OBS_NVAR(column_map); phalcon_read_property_this(&column_map, this_ptr, SL("_columnMap"), PH_NOISY TSRMLS_CC); } PHALCON_OBS_VAR(data); phalcon_array_fetch(&data, column_map, key_name, PH_NOISY); RETURN_CTOR(data); }
/** * Generates SQL to add an index to a table * * @param string $tableName * @param string $schemaName * @param Phalcon\Db\IndexInterface $index * @return string */ PHP_METHOD(Phalcon_Db_Dialect_Sqlite, addIndex){ zval *table_name, *schema_name, *index, *sql = NULL, *columns = NULL; zval *quoted_column_list = NULL, *name = NULL; zval *index_type = NULL; PHALCON_MM_GROW(); phalcon_fetch_params(1, 3, 0, &table_name, &schema_name, &index); PHALCON_VERIFY_INTERFACE_EX(index, phalcon_db_indexinterface_ce, phalcon_db_exception_ce, 1); PHALCON_CALL_METHOD(&name, index, "getname"); PHALCON_CALL_METHOD(&index_type, index, "gettype"); PHALCON_INIT_VAR(sql); if (zend_is_true(schema_name)) { if (index_type && Z_TYPE_P(index_type) == IS_STRING && Z_STRLEN_P(index_type) > 0) { PHALCON_CONCAT_SVSVSVSVS(sql, "CREATE ", index_type, " INDEX \"", schema_name, "\".\"", name, "\" ON \"", table_name, "\" ("); } else { PHALCON_CONCAT_SVSVSVS(sql, "CREATE INDEX \"", schema_name, "\".\"", name, "\" ON \"", table_name, "\" ("); } } else if (index_type && Z_TYPE_P(index_type) == IS_STRING && Z_STRLEN_P(index_type) > 0) { PHALCON_CONCAT_SVSVSVS(sql, "CREATE ", index_type, " INDEX \"", name, "\" ON \"", table_name, "\" ("); } else { PHALCON_CONCAT_SVSVS(sql, "CREATE INDEX \"", name, "\" ON \"", table_name, "\" ("); } PHALCON_CALL_METHOD(&columns, index, "getcolumns"); PHALCON_CALL_METHOD("ed_column_list, getThis(), "getcolumnlist", columns); PHALCON_SCONCAT_VS(sql, quoted_column_list, ")"); RETURN_CTOR(sql); }
/** * Gets the column name in MySQL * * @param Phalcon\Db\ColumnInterface $column * @return string */ PHP_METHOD(Phalcon_Db_Dialect_Mysql, getColumnDefinition){ zval *column, size = {}, column_type = {}, column_sql = {}, type_values = {}, slash = {}, *value, value_cslashes = {}, is_unsigned = {}, scale = {}, name = {}; int c, i = 0; phalcon_fetch_params(0, 1, 0, &column); PHALCON_VERIFY_INTERFACE_EX(column, phalcon_db_columninterface_ce, phalcon_db_exception_ce, 0); PHALCON_CALL_METHODW(&size, column, "getsize"); PHALCON_CALL_METHODW(&column_type, column, "gettype"); if (Z_TYPE(column_type) == IS_STRING) { PHALCON_CPY_WRT(&column_sql, &column_type); PHALCON_CALL_METHODW(&type_values, column, "gettypevalues"); if (PHALCON_IS_NOT_EMPTY(&type_values)) { ZVAL_STRING(&slash, "\""); if (Z_TYPE(type_values) == IS_ARRAY) { c = phalcon_fast_count_int(&type_values); phalcon_concat_self_str(&column_sql, SL("(")); ZEND_HASH_FOREACH_VAL(Z_ARRVAL(type_values), value) { i++; PHALCON_CALL_FUNCTIONW(&value_cslashes, "addcslashes", value, &slash); if (i < c) { PHALCON_SCONCAT_SVS(&column_sql, "\"", &value_cslashes, "\", "); } else { PHALCON_SCONCAT_SVS(&column_sql, "\"", &value_cslashes, "\""); } } ZEND_HASH_FOREACH_END();
/** * Sets the DependencyInjector container * * @param Phalcon\DiInterface $dependencyInjector */ PHP_METHOD(Phalcon_Mvc_Model_Criteria, setDI){ zval *dependency_injector; phalcon_fetch_params(0, 1, 0, &dependency_injector); PHALCON_VERIFY_INTERFACE_EX(dependency_injector, phalcon_diinterface_ce, phalcon_mvc_model_exception_ce, 0); phalcon_update_property_array_string(this_ptr, SL("_params"), SS("di"), dependency_injector TSRMLS_CC); }
/** * Sets the DependencyInjector container * * @param Phalcon\DiInterface $dependencyInjector */ PHP_METHOD(Phalcon_Mvc_Collection_Manager, setDI){ zval *dependency_injector; phalcon_fetch_params(0, 1, 0, &dependency_injector); PHALCON_VERIFY_INTERFACE_EX(dependency_injector, phalcon_diinterface_ce, phalcon_mvc_collection_exception_ce, 0); phalcon_update_property_this(this_ptr, SL("_dependencyInjector"), dependency_injector TSRMLS_CC); }
/** * Sets transaction manager related to the transaction * * @param Phalcon\Mvc\Model\Transaction\ManagerInterface $manager */ PHP_METHOD(Phalcon_Mvc_Model_Transaction, setTransactionManager){ zval *manager; phalcon_fetch_params(0, 1, 0, &manager); PHALCON_VERIFY_INTERFACE_EX(manager, phalcon_mvc_model_transaction_managerinterface_ce, phalcon_mvc_model_transaction_exception_ce, 0); phalcon_update_property_zval(getThis(), SL("_manager"), manager); }
/** * Set a default dependency injection container to be obtained into static methods * * @param Phalcon\DiInterface $dependencyInjector */ PHP_METHOD(Phalcon_DI, setDefault){ zval *dependency_injector; phalcon_fetch_params(0, 1, 0, &dependency_injector); PHALCON_VERIFY_INTERFACE_EX(dependency_injector, phalcon_diinterface_ce, phalcon_di_exception_ce, 0); phalcon_update_static_property_ce(phalcon_di_ce, SL("_default"), dependency_injector); }
/** * Sets a custom events manager * * @param Phalcon\Events\ManagerInterface $eventsManager */ PHP_METHOD(Phalcon_DI, setEventsManager){ zval *events_manager; phalcon_fetch_params(0, 1, 0, &events_manager); PHALCON_VERIFY_INTERFACE_EX(events_manager, phalcon_events_managerinterface_ce, phalcon_di_exception_ce, 0); phalcon_update_property_this(getThis(), SL("_eventsManager"), events_manager); }
/** * Phalcon\Paginator\Adapter\Sql * * @param array $config */ PHP_METHOD(Phalcon_Paginator_Adapter_Sql, __construct){ zval *config, db = {}, sql = {}, total_sql = {}, bind = {}, limit = {}, page = {}; long int i_limit; phalcon_fetch_params(0, 1, 0, &config); if (!phalcon_array_isset_fetch_str(&db, config, SL("db"))) { PHALCON_THROW_EXCEPTION_STRW(phalcon_paginator_exception_ce, "Parameter 'db' is required"); return; } if (!phalcon_array_isset_fetch_str(&sql, config, SL("sql"))) { PHALCON_THROW_EXCEPTION_STRW(phalcon_paginator_exception_ce, "Parameter 'sql' is required"); return; } if (!phalcon_array_isset_fetch_str(&total_sql, config, SL("total_sql"))) { PHALCON_THROW_EXCEPTION_STRW(phalcon_paginator_exception_ce, "Parameter 'sql' is required"); return; } if (phalcon_array_isset_fetch_str(&bind, config, SL("bind"))) { if (Z_TYPE_P(&bind) != IS_ARRAY) { phalcon_update_property_empty_array(getThis(), SL("_bind")); } else { phalcon_update_property_this(getThis(), SL("_bind"), &bind); } } else { phalcon_update_property_empty_array(getThis(), SL("_bind")); } PHALCON_VERIFY_INTERFACE_EX(&db, phalcon_db_adapterinterface_ce, phalcon_paginator_exception_ce, 0); phalcon_update_property_this(getThis(), SL("_db"), &db); phalcon_update_property_this(getThis(), SL("_sql"), &sql); phalcon_update_property_this(getThis(), SL("_total_sql"), &total_sql); if (!phalcon_array_isset_fetch_str(&limit, config, SL("limit"))) { PHALCON_THROW_EXCEPTION_STRW(phalcon_paginator_exception_ce, "Parameter 'limit' is required"); return; } i_limit = phalcon_get_intval(&limit); if (i_limit < 1) { PHALCON_THROW_EXCEPTION_STRW(phalcon_paginator_exception_ce, "'limit' should be positive"); return; } phalcon_update_property_this(getThis(), SL("_limitRows"), &limit); if (phalcon_array_isset_fetch_str(&page, config, SL("page"))) { phalcon_update_property_this(getThis(), SL("_page"), &page); } }
/** * Phalcon\Mvc\JsonRpc * * @param Phalcon\DI $dependencyInjector */ PHP_METHOD(Phalcon_Mvc_JsonRpc, __construct){ zval *dependency_injector = NULL; phalcon_fetch_params(0, 0, 1, &dependency_injector); if (dependency_injector && Z_TYPE_P(dependency_injector) == IS_OBJECT) { PHALCON_VERIFY_INTERFACE_EX(dependency_injector, phalcon_diinterface_ce, phalcon_mvc_jsonrpc_exception_ce, 0); phalcon_update_property_this(getThis(), SL("_dependencyInjector"), dependency_injector); } }
/** * Phalcon\Mvc\Application * * @param Phalcon\DI $dependencyInjector */ PHP_METHOD(Phalcon_Mvc_Application, __construct){ zval *dependency_injector = NULL; phalcon_fetch_params(0, 0, 1, &dependency_injector); if (dependency_injector && Z_TYPE_P(dependency_injector) == IS_OBJECT) { PHALCON_VERIFY_INTERFACE_EX(dependency_injector, phalcon_diinterface_ce, phalcon_mvc_application_exception_ce, 0); phalcon_update_property_this(this_ptr, SL("_dependencyInjector"), dependency_injector TSRMLS_CC); } }
/** * Set query builder object * * @param Phalcon\Db\AdapterInterface $db * * @return Phalcon\Paginator\Adapter\Sql $this Fluent interface */ PHP_METHOD(Phalcon_Paginator_Adapter_Sql, setDb){ zval *db; phalcon_fetch_params(0, 1, 0, &db); PHALCON_VERIFY_INTERFACE_EX(db, phalcon_db_adapterinterface_ce, phalcon_paginator_exception_ce, 0); phalcon_update_property_this(getThis(), SL("_db"), db); RETURN_THISW(); }
PHP_METHOD(Phalcon_JsonRpc_Client, __construct){ zval *httpclient; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 0, &httpclient); PHALCON_VERIFY_INTERFACE_EX(httpclient, phalcon_http_client_adapterinterface_ce, phalcon_jsonrpc_client_exception_ce, 1); phalcon_update_property_this(this_ptr, SL("_httpclient"), httpclient TSRMLS_CC); PHALCON_MM_RESTORE(); }
/** * Set query builder object * * @param Phalcon\Db\AdapterInterface $db * * @return Phalcon\Paginator\Adapter\Sql $this Fluent interface */ PHP_METHOD(Phalcon_Paginator_Adapter_Sql, setDb){ zval *dbname, db = {}; phalcon_fetch_params(0, 1, 0, &dbname); if (Z_TYPE_P(dbname) != IS_OBJECT) { PHALCON_CALL_METHODW(&db, getThis(), "getresolveservice", dbname); } else { PHALCON_CPY_WRT(&db, dbname); } PHALCON_VERIFY_INTERFACE_EX(&db, phalcon_db_adapterinterface_ce, phalcon_paginator_exception_ce, 0); phalcon_update_property_zval(getThis(), SL("_db"), &db); RETURN_THISW(); }
/** * Sets a custom events manager for a specific model * * @param Phalcon\Mvc\CollectionInterface $model * @param Phalcon\Events\ManagerInterface $eventsManager */ PHP_METHOD(Phalcon_Mvc_Collection_Manager, setCustomEventsManager){ zval *model, *events_manager, *class_name; PHALCON_MM_GROW(); phalcon_fetch_params(1, 2, 0, &model, &events_manager); PHALCON_VERIFY_INTERFACE_EX(model, phalcon_mvc_collectioninterface_ce, phalcon_mvc_collection_exception_ce, 1); PHALCON_VERIFY_INTERFACE_OR_NULL_EX(events_manager, phalcon_events_managerinterface_ce, phalcon_mvc_collection_exception_ce, 1); PHALCON_INIT_VAR(class_name); phalcon_get_class(class_name, model, 1 TSRMLS_CC); phalcon_update_property_array(this_ptr, SL("_customEventsManager"), class_name, events_manager TSRMLS_CC); PHALCON_MM_RESTORE(); }
/** * Reads meta-data for certain model using a MODEL_* constant * *<code> * print_r($metaData->writeColumnMapIndex(new Robots(), MetaData::MODELS_REVERSE_COLUMN_MAP, array('leName' => 'name'))); *</code> * * @param Phalcon\Mvc\ModelInterface $model * @param int $index * @return array */ PHP_METHOD(Phalcon_Mvc_Model_MetaData, readMetaDataIndex){ zval **model, **index, *table = NULL, *schema = NULL, *class_name; zval *key, *meta_data = NULL, *meta_data_index, *attributes; phalcon_fetch_params_ex(2, 0, &model, &index); PHALCON_VERIFY_INTERFACE_EX(*model, phalcon_mvc_modelinterface_ce, phalcon_mvc_model_exception_ce, 0); PHALCON_ENSURE_IS_LONG(index); PHALCON_MM_GROW(); PHALCON_CALL_METHOD(&table, *model, "getsource"); PHALCON_CALL_METHOD(&schema, *model, "getschema"); PHALCON_INIT_VAR(class_name); phalcon_get_class(class_name, *model, 1 TSRMLS_CC); /** * Unique key for meta-data is created using class-name-schema-table */ PHALCON_INIT_VAR(key); PHALCON_CONCAT_VSVV(key, class_name, "-", schema, table); PHALCON_OBS_VAR(meta_data); phalcon_read_property_this(&meta_data, this_ptr, SL("_metaData"), PH_NOISY TSRMLS_CC); if (!phalcon_array_isset(meta_data, key)) { PHALCON_CALL_METHOD(NULL, this_ptr, "_initialize", *model, key, table, schema); PHALCON_OBS_NVAR(meta_data); phalcon_read_property_this(&meta_data, this_ptr, SL("_metaData"), PH_NOISY TSRMLS_CC); } PHALCON_OBS_VAR(meta_data_index); phalcon_array_fetch(&meta_data_index, meta_data, key, PH_NOISY); PHALCON_OBS_VAR(attributes); phalcon_array_fetch(&attributes, meta_data_index, *index, PH_NOISY); RETURN_CTOR(attributes); }
/** * Generates SQL to add a column to a table * * @param string $tableName * @param string $schemaName * @param Phalcon\Db\ColumnInterface $column * @return string */ PHP_METHOD(Phalcon_Db_Dialect_Sqlite, addColumn){ zval *table_name, *schema_name, *column, *sql = NULL, *name = NULL; zval *column_definition = NULL, *is_not_null = NULL, *is_autoincrement = NULL; PHALCON_MM_GROW(); phalcon_fetch_params(1, 3, 0, &table_name, &schema_name, &column); PHALCON_VERIFY_INTERFACE_EX(column, phalcon_db_columninterface_ce, phalcon_db_exception_ce, 1); PHALCON_INIT_VAR(sql); if (zend_is_true(schema_name)) { PHALCON_CONCAT_SVSVS(sql, "ALTER TABLE \"", schema_name, "\".\"", table_name, "\" ADD COLUMN "); } else { PHALCON_CONCAT_SVS(sql, "ALTER TABLE \"", table_name, "\" ADD COLUMN "); } PHALCON_CALL_METHOD(&name, column, "getname"); PHALCON_CALL_METHOD(&column_definition, getThis(), "getcolumndefinition", column); PHALCON_SCONCAT_SVSV(sql, "\"", name, "\" ", column_definition); PHALCON_CALL_METHOD(&is_not_null, column, "isnotnull"); if (zend_is_true(is_not_null)) { phalcon_concat_self_str(sql, SL(" NOT NULL")); } PHALCON_CALL_METHOD(&is_autoincrement, column, "isautoincrement"); /* * See http://www.sqlite.org/syntaxdiagrams.html#column-constraint */ if (zend_is_true(is_autoincrement)) { phalcon_concat_self_str(sql, SL(" PRIMARY KEY AUTOINCREMENT")); } RETURN_CTOR(sql); }
/** * Sets the DependencyInjector container * * @param Phalcon\DiInterface $dependencyInjector */ PHP_METHOD(Phalcon_Mvc_Micro, setDI){ zval *dependency_injector, *service, *exists = NULL; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 0, &dependency_injector); PHALCON_VERIFY_INTERFACE_EX(dependency_injector, phalcon_diinterface_ce, phalcon_mvc_micro_exception_ce, 1); /** * We automatically set ourselves as application service */ PHALCON_INIT_VAR(service); ZVAL_STRING(service, "application"); PHALCON_CALL_METHOD(&exists, dependency_injector, "has", service); if (!zend_is_true(exists)) { PHALCON_CALL_METHOD(NULL, dependency_injector, "set", service, getThis()); } phalcon_update_property_this(getThis(), SL("_dependencyInjector"), dependency_injector); PHALCON_MM_RESTORE(); }
/** * Returns the cookie's value * * @param string|array $filters * @param string $defaultValue * @return mixed */ PHP_METHOD(Phalcon_Http_Cookie, getValue){ zval *filters = NULL, *default_value = NULL, *restored, *dependency_injector = NULL; zval *readed, *name, *_COOKIE, *value = NULL, *encryption; zval *service = NULL, *crypt = NULL, *decrypted_value = NULL, *filter = NULL; PHALCON_MM_GROW(); phalcon_fetch_params(0, 0, 2, &filters, &default_value); if (!filters) { filters = &PHALCON_GLOBAL(z_null); } if (!default_value) { default_value = &PHALCON_GLOBAL(z_null); } restored = phalcon_read_property(getThis(), SL("_restored"), PH_NOISY); if (!zend_is_true(restored)) { PHALCON_CALL_METHOD(NULL, getThis(), "restore"); } PHALCON_INIT_VAR(dependency_injector); readed = phalcon_read_property(getThis(), SL("_readed"), PH_NOISY); if (PHALCON_IS_FALSE(readed)) { name = phalcon_read_property(getThis(), SL("_name"), PH_NOISY); _COOKIE = phalcon_get_global(SL("_COOKIE")); if (phalcon_array_isset_fetch(&value, _COOKIE, name)) { encryption = phalcon_read_property(getThis(), SL("_useEncryption"), PH_NOISY); if (zend_is_true(encryption)) { dependency_injector = phalcon_read_property(getThis(), SL("_dependencyInjector"), PH_NOISY); if (Z_TYPE_P(dependency_injector) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_http_cookie_exception_ce, "A dependency injection object is required to access the 'filter' service"); return; } PHALCON_INIT_VAR(service); ZVAL_STRING(service, "crypt"); PHALCON_CALL_METHOD(&crypt, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(crypt, phalcon_cryptinterface_ce); /** * Decrypt the value also decoding it with base64 */ PHALCON_CALL_METHOD(&decrypted_value, crypt, "decryptbase64", value); } else { PHALCON_CPY_WRT(decrypted_value, value); } /** * Update the decrypted value */ phalcon_update_property_this(getThis(), SL("_value"), decrypted_value); if (Z_TYPE_P(filters) != IS_NULL) { filter = phalcon_read_property(getThis(), SL("_filter"), PH_NOISY); if (Z_TYPE_P(filter) != IS_OBJECT) { if (Z_TYPE_P(dependency_injector) == IS_NULL) { dependency_injector = phalcon_read_property(getThis(), SL("_dependencyInjector"), PH_NOISY); PHALCON_VERIFY_INTERFACE_EX(dependency_injector, phalcon_diinterface_ce, phalcon_http_cookie_exception_ce, 1); } PHALCON_INIT_NVAR(service); ZVAL_STR(service, IS(filter)); PHALCON_CALL_METHOD(&filter, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(filter, phalcon_filterinterface_ce); phalcon_update_property_this(getThis(), SL("_filter"), filter); } PHALCON_RETURN_CALL_METHOD(filter, "sanitize", decrypted_value, filters); RETURN_MM(); } /** * Return the value without filtering */ RETURN_CTOR(decrypted_value); } RETURN_CTOR(default_value); } value = phalcon_read_property(getThis(), SL("_value"), PH_NOISY); RETURN_CTOR(value); }
/** * Builds a Phalcon\Mvc\Model\Criteria based on an input array like $_POST * * @param Phalcon\DiInterface $dependencyInjector * @param string $modelName * @param array $data * @return Phalcon\Mvc\Model\Criteria */ PHP_METHOD(Phalcon_Mvc_Model_Criteria, fromInput){ zval *dependency_injector, *model_name, *data; zval *conditions, *service, *meta_data = NULL, *model; zval *data_types = NULL, *bind, *value = NULL, *field = NULL, *type, *condition = NULL; zval *value_pattern = NULL, *join_conditions; zval *column_map = NULL; HashTable *ah0; HashPosition hp0; zval **hd; zend_class_entry *ce0; PHALCON_MM_GROW(); phalcon_fetch_params(1, 3, 0, &dependency_injector, &model_name, &data); if (Z_TYPE_P(data) != IS_ARRAY) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Input data must be an Array"); return; } if (Z_TYPE_P(dependency_injector) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "A dependency injector container is required to obtain the ORM services"); return; } object_init_ex(return_value, phalcon_mvc_model_criteria_ce); if (zend_hash_num_elements(Z_ARRVAL_P(data))) { PHALCON_INIT_VAR(service); PHALCON_ZVAL_MAYBE_INTERNED_STRING(service, phalcon_interned_modelsMetadata); PHALCON_CALL_METHOD(&meta_data, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(meta_data, phalcon_mvc_model_metadatainterface_ce); ce0 = phalcon_fetch_class(model_name TSRMLS_CC); PHALCON_INIT_VAR(model); object_init_ex(model, ce0); if (phalcon_has_constructor(model TSRMLS_CC)) { PHALCON_CALL_METHOD(NULL, model, "__construct"); } PHALCON_VERIFY_INTERFACE_EX(model, phalcon_mvc_modelinterface_ce, phalcon_mvc_model_exception_ce, 1); if (PHALCON_GLOBAL(orm).column_renaming) { PHALCON_CALL_METHOD(&column_map, meta_data, "getreversecolumnmap", model); if (Z_TYPE_P(column_map) != IS_ARRAY) { PHALCON_INIT_NVAR(column_map); } } else { column_map = PHALCON_GLOBAL(z_null); } PHALCON_CALL_METHOD(&data_types, meta_data, "getdatatypes", model); PHALCON_INIT_VAR(bind); array_init(bind); PHALCON_INIT_VAR(conditions); array_init(conditions); /** * We look for attributes in the array passed as data */ phalcon_is_iterable(data, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { zval *real_field; PHALCON_GET_HKEY(field, ah0, hp0); PHALCON_GET_HVALUE(value); if (Z_TYPE_P(column_map) != IS_ARRAY || !phalcon_array_isset_fetch(&real_field, column_map, field)) { real_field = field; } if (phalcon_array_isset_fetch(&type, data_types, real_field)) { if (Z_TYPE_P(value) != IS_NULL && !PHALCON_IS_STRING(value, "")) { if (PHALCON_IS_LONG(type, 2)) { /** * For varchar types we use LIKE operator */ PHALCON_INIT_NVAR(condition); PHALCON_CONCAT_VSVS(condition, field, " LIKE :", field, ":"); PHALCON_INIT_NVAR(value_pattern); PHALCON_CONCAT_SVS(value_pattern, "%", value, "%"); phalcon_array_update_zval(&bind, field, value_pattern, PH_COPY); } else { /** * For the rest of data types we use a plain = operator */ PHALCON_INIT_NVAR(condition); PHALCON_CONCAT_VSVS(condition, field, "=:", field, ":"); phalcon_array_update_zval(&bind, field, value, PH_COPY); } phalcon_array_append(&conditions, condition, 0); } } zend_hash_move_forward_ex(ah0, &hp0); } if (zend_hash_num_elements(Z_ARRVAL_P(conditions))) { PHALCON_INIT_VAR(join_conditions); phalcon_fast_join_str(join_conditions, SL(" AND "), conditions TSRMLS_CC); PHALCON_CALL_METHOD(NULL, return_value, "where", join_conditions, bind); } } PHALCON_CALL_METHOD(NULL, return_value, "setmodelname", model_name); RETURN_MM(); }
/** * Add a watermark to an image with a specified opacity. Alpha transparency * will be preserved. * * @param Phalcon\Image\Adapter $watermark watermark Image instance * @param int $offset_x offset from the left, If less than 0 offset from the right, If true right the x offset * @param int $offset_y offset from the top, If less than 0 offset from the bottom, If true bottom the Y offset * @param int $opacity opacity of watermark: 1-100 * @return Phalcon\Image\AdapterInterface */ PHP_METHOD(Phalcon_Image_Adapter, watermark) { zval **watermark, **ofs_x = NULL, **ofs_y = NULL, **op = NULL; zval *offset_x, *offset_y, *opacity; zval *image_width, *image_height, *watermark_width, *watermark_height; long tmp_image_width, tmp_image_height, tmp_watermark_width, tmp_watermark_height, tmp_offset_x, tmp_offset_y; phalcon_fetch_params_ex(1, 3, &watermark, &ofs_x, &ofs_y, &op); PHALCON_VERIFY_INTERFACE_EX(*watermark, phalcon_image_adapterinterface_ce, phalcon_image_exception_ce, 0); PHALCON_MM_GROW(); image_width = phalcon_fetch_nproperty_this(this_ptr, SL("_width"), PH_NOISY TSRMLS_CC); image_height = phalcon_fetch_nproperty_this(this_ptr, SL("_height"), PH_NOISY TSRMLS_CC); watermark_width = phalcon_fetch_nproperty_this(*watermark, SL("_width"), PH_NOISY TSRMLS_CC); watermark_height = phalcon_fetch_nproperty_this(*watermark, SL("_height"), PH_NOISY TSRMLS_CC); tmp_image_width = phalcon_get_intval(image_width); tmp_image_height = phalcon_get_intval(image_height); tmp_watermark_width = phalcon_get_intval(watermark_width); tmp_watermark_height = phalcon_get_intval(watermark_height); if (!ofs_x) { tmp_offset_x = (int)(((tmp_image_width - tmp_watermark_width) / 2) + 0.5); } else if (Z_TYPE_PP(ofs_x) == IS_LONG) { tmp_offset_x = Z_LVAL_PP(ofs_x); if (tmp_offset_x < 0) { tmp_offset_x = (int)(tmp_image_width - tmp_watermark_width + tmp_offset_x + 0.5); } } else if (zend_is_true(*ofs_x)) { tmp_offset_x = (int)(tmp_image_width - tmp_watermark_width); } else { tmp_offset_x = (int)(((tmp_image_width - tmp_watermark_width) / 2) + 0.5); } PHALCON_INIT_VAR(offset_x); ZVAL_LONG(offset_x, tmp_offset_x); if (!ofs_y) { tmp_offset_y = (int)(((tmp_image_height - tmp_watermark_height) / 2) + 0.5); } else if (Z_TYPE_PP(ofs_y) == IS_LONG) { tmp_offset_y = Z_LVAL_PP(ofs_y); if (tmp_offset_y < 0) { tmp_offset_y = (int)(tmp_image_height - tmp_watermark_height + tmp_offset_y + 0.5); } } else if (zend_is_true(*ofs_y)) { tmp_offset_y = (int)(tmp_image_height - tmp_watermark_height); } else { tmp_offset_y = (int)(((tmp_image_height - tmp_watermark_height) / 2) + 0.5); } PHALCON_INIT_VAR(offset_y); ZVAL_LONG(offset_y, tmp_offset_y); PHALCON_INIT_VAR(opacity); if (!op) { ZVAL_LONG(opacity, 100); } else { PHALCON_ENSURE_IS_LONG(op); if (Z_LVAL_PP(op) < 1) { ZVAL_LONG(opacity, 1); } else if (Z_LVAL_PP(op) > 100) { ZVAL_LONG(opacity, 100); } else { ZVAL_LONG(opacity, Z_LVAL_PP(op)); } } PHALCON_CALL_METHOD(NULL, this_ptr, "_watermark", *watermark, offset_x, offset_y, opacity); RETURN_THIS(); }
/** * Writes meta-data for certain model using a MODEL_* constant * *<code> * print_r($metaData->writeColumnMapIndex(new Robots(), MetaData::MODELS_REVERSE_COLUMN_MAP, array('leName' => 'name'))); *</code> * * @param Phalcon\Mvc\ModelInterface $model * @param int $index * @param mixed $data */ PHP_METHOD(Phalcon_Mvc_Model_MetaData, writeMetaDataIndex){ zval *model, *index, *data, *replace, *table = NULL, *schema = NULL, *class_name; zval *key, *meta_data = NULL, *arr, *value; HashTable *ah2; HashPosition hp2; zval **hd; PHALCON_MM_GROW(); phalcon_fetch_params(1, 4, 0, &model, &index, &data, &replace); PHALCON_VERIFY_INTERFACE_EX(model, phalcon_mvc_modelinterface_ce, phalcon_mvc_model_exception_ce, 1); if (Z_TYPE_P(index) != IS_LONG) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Index must be a valid integer constant"); return; } if (Z_TYPE_P(data) != IS_ARRAY && Z_TYPE_P(data) != IS_STRING && Z_TYPE_P(data) != IS_BOOL) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Invalid data for index"); return; } PHALCON_CALL_METHOD(&table, model, "getsource"); PHALCON_CALL_METHOD(&schema, model, "getschema"); PHALCON_INIT_VAR(class_name); phalcon_get_class(class_name, model, 1 TSRMLS_CC); /** * Unique key for meta-data is created using class-name-schema-table */ PHALCON_INIT_VAR(key); PHALCON_CONCAT_VSVV(key, class_name, "-", schema, table); PHALCON_OBS_VAR(meta_data); phalcon_read_property_this(&meta_data, this_ptr, SL("_metaData"), PH_NOISY TSRMLS_CC); if (!phalcon_array_isset(meta_data, key)) { PHALCON_CALL_METHOD(NULL, this_ptr, "_initialize", model, key, table, schema); PHALCON_OBS_NVAR(meta_data); phalcon_read_property_this(&meta_data, this_ptr, SL("_metaData"), PH_NOISY TSRMLS_CC); } else if (!zend_is_true(replace)) { PHALCON_OBS_VAR(arr); phalcon_array_fetch(&arr, meta_data, key, PH_NOISY); PHALCON_OBS_VAR(value); phalcon_array_fetch(&value, arr, index, PH_NOISY); PHALCON_SEPARATE_PARAM(data); phalcon_is_iterable(value, &ah2, &hp2, 0, 0); while (zend_hash_get_current_data_ex(ah2, (void**) &hd, &hp2) == SUCCESS) { zval key2 = phalcon_get_current_key_w(ah2, &hp2); if (!phalcon_array_isset(data, &key2)) { phalcon_array_update_zval(&data, &key2, *hd, PH_COPY | PH_SEPARATE); } zend_hash_move_forward_ex(ah2, &hp2); } } phalcon_array_update_multi_2(&meta_data, key, index, data, 0); phalcon_update_property_this(this_ptr, SL("_metaData"), meta_data TSRMLS_CC); PHALCON_MM_RESTORE(); }
/** * Handles a MVC request * * @param string $uri * @return Phalcon\Http\ResponseInterface */ PHP_METHOD(Phalcon_Mvc_JsonRpc, handle){ zval *uri = NULL, *dependency_injector, *events_manager; zval *status = NULL, *service = NULL, *request = NULL, *response = NULL; zval *json = NULL, *data = NULL, *jsonrpc_message, *jsonrpc_error, *jsonrpc_result = NULL; zval *jsonrpc_method, *jsonrpc_params, *jsonrpc_id; zval *url = NULL, *router = NULL, *module_name = NULL; zval *module_object = NULL, *modules; zval *module, *class_name = NULL, *module_params; zval *namespace_name = NULL; zval *controller_name = NULL, *action_name = NULL, *params = NULL, *exact = NULL; zval *dispatcher = NULL, *controller = NULL, *returned_response = NULL; zval *path; PHALCON_MM_GROW(); dependency_injector = phalcon_read_property(getThis(), SL("_dependencyInjector"), PH_NOISY); if (Z_TYPE_P(dependency_injector) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_jsonrpc_exception_ce, "A dependency injection object is required to access internal services"); return; } events_manager = phalcon_read_property(getThis(), SL("_eventsManager"), PH_NOISY); if (Z_TYPE_P(events_manager) != IS_OBJECT) { events_manager = NULL; } else { PHALCON_VERIFY_INTERFACE_EX(events_manager, phalcon_events_managerinterface_ce, phalcon_mvc_jsonrpc_exception_ce, 1); } /* Call boot event, this allows the developer to perform initialization actions */ if (FAILURE == phalcon_mvc_jsonrpc_fire_event(events_manager, "jsonrpc:boot", getThis(), NULL)) { RETURN_MM_FALSE; } /* Deserializer Json */ PHALCON_INIT_NVAR(service); ZVAL_STR(service, IS(request)); PHALCON_CALL_METHOD(&request, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(request, phalcon_http_requestinterface_ce); PHALCON_CALL_METHOD(&json, request, "getrawbody"); PHALCON_CALL_FUNCTION(&data, "json_decode", json, &PHALCON_GLOBAL(z_true)); PHALCON_INIT_NVAR(service); ZVAL_STR(service, IS(response)); PHALCON_CALL_METHOD(&response, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(response, phalcon_http_responseinterface_ce); PHALCON_INIT_VAR(jsonrpc_message); array_init(jsonrpc_message); PHALCON_INIT_VAR(jsonrpc_error); array_init(jsonrpc_error); if (PHALCON_IS_EMPTY(data)) { phalcon_array_update_str_long(jsonrpc_error, SL("code"), __LINE__, 0); phalcon_array_update_str_str(jsonrpc_error, SL("message"), SL("Parse error"), PH_COPY); } else if (Z_TYPE_P(data) != IS_ARRAY) { phalcon_array_update_str_long(jsonrpc_error, SL("code"), __LINE__, 0); phalcon_array_update_str_str(jsonrpc_error, SL("message"), SL("Parse error"), PH_COPY); } else if (!phalcon_array_isset_str(data, SL("jsonrpc"))) { phalcon_array_update_str_long(jsonrpc_error, SL("code"), __LINE__, 0); phalcon_array_update_str_str(jsonrpc_error, SL("message"), SL("Invalid Request"), PH_COPY); } else if (!phalcon_array_isset_str(data, SL("method"))) { phalcon_array_update_str_long(jsonrpc_error, SL("code"), __LINE__, 0); phalcon_array_update_str_str(jsonrpc_error, SL("message"), SL("Invalid Request"), PH_COPY); } else { PHALCON_OBS_VAR(jsonrpc_method); phalcon_array_fetch_str(&jsonrpc_method, data, SL("method"), PH_NOISY); if (phalcon_array_isset_str(data, SL("params"))) { PHALCON_OBS_VAR(jsonrpc_params); phalcon_array_fetch_str(&jsonrpc_params, data, SL("params"), PH_NOISY); } else { PHALCON_INIT_VAR(jsonrpc_params); array_init(jsonrpc_params); } PHALCON_INIT_NVAR(service); ZVAL_STR(service, IS(url)); PHALCON_CALL_METHOD(&url, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(url, phalcon_mvc_urlinterface_ce); PHALCON_CALL_METHOD(&uri, url, "get", jsonrpc_method); PHALCON_INIT_NVAR(service); ZVAL_STR(service, IS(router)); PHALCON_CALL_METHOD(&router, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(router, phalcon_mvc_routerinterface_ce); /* Handle the URI pattern (if any) */ PHALCON_CALL_METHOD(NULL, router, "handle", uri); /* Load module config */ PHALCON_CALL_METHOD(&module_name, router, "getmodulename"); /* Load module config */ PHALCON_CALL_METHOD(&module_name, router, "getmodulename"); /* If the router doesn't return a valid module we use the default module */ if (!zend_is_true(module_name)) { module_name = phalcon_read_property(getThis(), SL("_defaultModule"), PH_NOISY); } /** * Process the module definition */ if (zend_is_true(module_name)) { if (FAILURE == phalcon_mvc_jsonrpc_fire_event(events_manager, "jsonrpc:beforeStartModule", getThis(), module_name)) { RETURN_MM_FALSE; } /** * Check if the module passed by the router is registered in the modules container */ modules = phalcon_read_property(getThis(), SL("_modules"), PH_NOISY); if (!phalcon_array_isset_fetch(&module, modules, module_name)) { convert_to_string(module_name); zend_throw_exception_ex(phalcon_mvc_jsonrpc_exception_ce, 0, "Module %s is not registered in the jsonrpc container", Z_STRVAL_P(module_name)); RETURN_MM(); } /** * A module definition must be an array or an object */ if (Z_TYPE_P(module) != IS_ARRAY && Z_TYPE_P(module) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_jsonrpc_exception_ce, "Invalid module definition"); return; } /* An array module definition contains a path to a module definition class */ if (Z_TYPE_P(module) == IS_ARRAY) { /* Class name used to load the module definition */ if (phalcon_array_isset_str(module, SL("className"))) { PHALCON_OBS_VAR(class_name); phalcon_array_fetch_str(&class_name, module, SL("className"), PH_NOISY); } else { PHALCON_INIT_NVAR(class_name); ZVAL_STRING(class_name, "Module"); } /* If the developer has specified a path, try to include the file */ if (phalcon_array_isset_str(module, SL("path"))) { PHALCON_OBS_VAR(path); phalcon_array_fetch_str(&path, module, SL("path"), PH_NOISY); convert_to_string_ex(path); if (Z_TYPE_P(class_name) != IS_STRING || phalcon_class_exists(class_name, 0) == NULL) { if (phalcon_file_exists(path) == SUCCESS) { RETURN_MM_ON_FAILURE(phalcon_require(Z_STRVAL_P(path))); } else { zend_throw_exception_ex(phalcon_mvc_jsonrpc_exception_ce, 0, "Module definition path '%s' does not exist", Z_STRVAL_P(path)); RETURN_MM(); } } } PHALCON_CALL_METHOD(&module_object, dependency_injector, "get", class_name); /** * 'registerAutoloaders' and 'registerServices' are automatically called */ PHALCON_CALL_METHOD(NULL, module_object, "registerautoloaders", dependency_injector); PHALCON_CALL_METHOD(NULL, module_object, "registerservices", dependency_injector); } else if (Z_TYPE_P(module) == IS_OBJECT && instanceof_function(Z_OBJCE_P(module), zend_ce_closure)) { /* A module definition object, can be a Closure instance */ PHALCON_INIT_VAR(module_params); array_init_size(module_params, 1); phalcon_array_append(module_params, dependency_injector, PH_COPY); PHALCON_CALL_USER_FUNC_ARRAY(&status, module, module_params); } else { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_jsonrpc_exception_ce, "Invalid module definition"); return; } /* Calling afterStartModule event */ if (events_manager) { if (!module_object) { module_object = &PHALCON_GLOBAL(z_null); } phalcon_update_property_this(getThis(), SL("_moduleObject"), module_object); if (FAILURE == phalcon_mvc_jsonrpc_fire_event(events_manager, "jsonrpc:afterStartModule", getThis(), module_name)) { RETURN_MM_FALSE; } } } /* We get the parameters from the router and assign them to the dispatcher */ PHALCON_CALL_METHOD(&module_name, router, "getmodulename"); PHALCON_CALL_METHOD(&namespace_name, router, "getnamespacename"); PHALCON_CALL_METHOD(&controller_name, router, "getcontrollername"); PHALCON_CALL_METHOD(&action_name, router, "getactionname"); PHALCON_CALL_METHOD(¶ms, router, "getparams"); PHALCON_CALL_METHOD(&exact, router, "isexactcontrollername"); PHALCON_INIT_NVAR(service); ZVAL_STR(service, IS(dispatcher)); PHALCON_CALL_METHOD(&dispatcher, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(dispatcher, phalcon_dispatcherinterface_ce); /* Assign the values passed from the router */ PHALCON_CALL_METHOD(NULL, dispatcher, "setmodulename", module_name); PHALCON_CALL_METHOD(NULL, dispatcher, "setnamespacename", namespace_name); PHALCON_CALL_METHOD(NULL, dispatcher, "setcontrollername", controller_name, exact); PHALCON_CALL_METHOD(NULL, dispatcher, "setactionname", action_name); PHALCON_CALL_METHOD(NULL, dispatcher, "setparams", jsonrpc_params); /* Calling beforeHandleRequest */ RETURN_MM_ON_FAILURE(phalcon_mvc_jsonrpc_fire_event(events_manager, "jsonrpc:beforeHandleRequest", getThis(), dispatcher)); /* The dispatcher must return an object */ PHALCON_CALL_METHOD(&controller, dispatcher, "dispatch"); PHALCON_INIT_VAR(returned_response); /* Get the latest value returned by an action */ PHALCON_CALL_METHOD(&jsonrpc_result, dispatcher, "getreturnedvalue"); } /* Calling afterHandleRequest */ if (FAILURE == phalcon_mvc_jsonrpc_fire_event(events_manager, "jsonrpc:afterHandleRequest", getThis(), controller) && EG(exception)) { RETURN_MM(); } phalcon_array_update_str_str(jsonrpc_message, SL("jsonrpc"), SL("2.0"), PH_COPY); if (PHALCON_IS_NOT_EMPTY(jsonrpc_error)) { phalcon_array_update_str(jsonrpc_message, SL("error"), jsonrpc_error, PH_COPY); } if (jsonrpc_result != NULL) { phalcon_array_update_str(jsonrpc_message, SL("result"), jsonrpc_result, PH_COPY); } if (phalcon_array_isset_str_fetch(&jsonrpc_id, data, SL("id"))) { phalcon_array_update_str(jsonrpc_message, SL("id"), jsonrpc_id, PH_COPY); } else { phalcon_array_update_str(jsonrpc_message, SL("id"), &PHALCON_GLOBAL(z_null), PH_COPY); } PHALCON_CALL_METHOD(NULL, response, "setjsoncontent", jsonrpc_message); /* Calling beforeSendResponse */ if (FAILURE == phalcon_mvc_jsonrpc_fire_event(events_manager, "jsonrpc:beforeSendResponse", getThis(), response) && EG(exception)) { RETURN_MM(); } /* Headers are automatically sent */ PHALCON_CALL_METHOD(NULL, response, "sendheaders"); /* Cookies are automatically sent */ PHALCON_CALL_METHOD(NULL, response, "sendcookies"); /* Return the response */ RETURN_CCTOR(response); }