/** * Registers a job to the AT service. * * @param int $start Timestamp after which the job is run * @param string $component The name of the component which should run the job * @param string $method The method in interface class to call to run the job * @param array $args Arguments array for the method * @return boolean Indicating success/failure in registering the job */ public static function register($start, $component, $method, $args) { $entry = new midcom_services_at_entry_dba(); $entry->start = $start; $entry->component = $component; $entry->method = $method; $entry->arguments = $args; return $entry->create(); }
/** * Function to process the notify date in the passed formdata of the datamanger * creates/edits/deletes the corresponding at_entry if needed * * @param array $formdata The Formdata of the datamanager containing the notify_date * @param org_openpsa_sales_salesproject_deliverable_dba $deliverable The current deliverable */ public function process_notify_date($formdata, org_openpsa_sales_salesproject_deliverable_dba $deliverable) { //check if there is already an at_entry $mc_entry = org_openpsa_relatedto_dba::new_collector('toGuid', $deliverable->guid); $mc_entry->add_constraint('fromClass', '=', 'midcom_services_at_entry_dba'); $mc_entry->add_constraint('toClass', '=', 'org_openpsa_sales_salesproject_deliverable_dba'); $mc_entry->add_constraint('toExtra', '=', 'notify_at_entry'); $entry_keys = $mc_entry->get_values('fromGuid'); //check date if (!$formdata['notify']->is_empty()) { $notification_entry = null; if (count($entry_keys) == 0) { $notification_entry = new midcom_services_at_entry_dba(); $notification_entry->create(); //relatedto from notifcation to deliverable org_openpsa_relatedto_plugin::create($notification_entry, 'midcom.services.at', $deliverable, 'org.openpsa.sales', false, array('toExtra' => 'notify_at_entry')); } else { //get guid of at_entry foreach ($entry_keys as $key => $entry) { //check if related at_entry exists try { $notification_entry = new midcom_services_at_entry_dba($entry); } catch (midcom_error $e) { //relatedto links to a non-existing at_entry - so create a new one an link to it $notification_entry = new midcom_services_at_entry_dba(); $notification_entry->create(); $relatedto = new org_openpsa_relatedto_dba($key); $relatedto->fromGuid = $notification_entry->guid; $relatedto->update(); } break; } } $notification_entry->start = $formdata['notify']->value->format('U'); $notification_entry->method = 'new_notification_message'; $notification_entry->component = 'org.openpsa.sales'; $notification_entry->arguments = array('deliverable' => $deliverable->guid); $notification_entry->update(); } else { //void date - so delete existing at_entrys for this notify_date foreach ($entry_keys as $key => $empty) { try { $notification_entry = new midcom_services_at_entry_dba($mc_entry->get_subkey($key, 'fromGuid')); //check if related at_entry exists & delete it $notification_entry->delete(); } catch (midcom_error $e) { $e->log(); } } } }
/** * Manually trigger a subscription cycle run. */ private function _run_cycle() { if (empty($_POST['at_entry'])) { throw new midcom_error('No AT entry specified'); } $entry = new midcom_services_at_entry_dba($_POST['at_entry']); $deliverable = new org_openpsa_sales_salesproject_deliverable_dba($entry->arguments['deliverable']); $scheduler = new org_openpsa_invoices_scheduler($deliverable); if (!$scheduler->run_cycle($entry->arguments['cycle'])) { throw new midcom_error('Failed to run cycle, see debug log for details'); } if (!$entry->delete()) { throw new midcom_error('Could not delete AT entry: ' . midcom_connection::get_error_string()); } }
/** * Loads all entries that need to be processed and processes them. */ public function _on_execute() { $qb = midcom_services_at_entry_dba::new_query_builder(); // (to be) start(ed) AND last touched over two days ago $qb->add_constraint('start', '<=', time() - 3600 * 24 * 2); $qb->begin_group('OR'); $qb->add_constraint('host', '=', midcom_connection::get('host')); $qb->add_constraint('host', '=', 0); $qb->end_group(); $qb->add_constraint('metadata.revised', '<=', date('Y-m-d H:i:s', time() - 3600 * 24 * 2)); $qb->add_constraint('status', '>=', midcom_services_at_entry_dba::RUNNING); midcom::get('auth')->request_sudo('midcom.services.at'); $qbret = $qb->execute(); if (empty($qbret)) { debug_add('Got empty resultset, exiting'); midcom::get('auth')->drop_sudo(); return; } foreach ($qbret as $entry) { debug_add("Deleting dangling entry #{$entry->id}\n", MIDCOM_LOG_INFO); debug_print_r("Entry #{$entry->id} dump: ", $entry); $entry->delete(); } midcom::get('auth')->drop_sudo(); }
public function get_qb($field = null, $direction = 'ASC') { $qb = midcom_services_at_entry_dba::new_query_builder(); $qb->add_constraint('method', '=', 'new_subscription_cycle'); $qb->add_constraint('component', '=', 'org.openpsa.sales'); $qb->add_constraint('status', '=', midcom_services_at_entry_dba::SCHEDULED); if (!is_null($field)) { $qb->add_order($field, $direction); } return $qb; }
public function testCRUD() { $args = array('arg1' => 'test', 'arg2' => 12); midcom::get('auth')->request_sudo('midcom.services.at'); $entry = new midcom_services_at_entry_dba(); $entry->arguments = $args; $stat = $entry->create(); $this->assertTrue($stat); //@todo For some reason, this throws a "Critical internal error". Needs to be investigated //$this->register_object($entry); $this->assertEquals($args, $entry->arguments); $this->assertEquals(midcom_services_at_entry_dba::SCHEDULED, $entry->status); $args['arg2'] = 11; $entry->arguments = $args; $stat = $entry->update(); $this->assertTrue($stat); $this->assertEquals($args, $entry->arguments); $stat = $entry->delete(); $this->assertTrue($stat); midcom::get('auth')->drop_sudo(); }
/** * Loads all entries that need to be processed and processes them. * * @todo FIXME: refactor to use more modern MidCOM interfaces and better sanity-checking */ public function _on_execute() { $qb = midcom_services_at_entry_dba::new_query_builder(); $qb->add_constraint('start', '<=', time()); $qb->begin_group('OR'); $qb->add_constraint('host', '=', midcom_connection::get('host')); $qb->add_constraint('host', '=', 0); $qb->end_group(); $qb->add_constraint('status', '=', midcom_services_at_entry_dba::SCHEDULED); $qb->set_limit((int) $this->_config->get('limit_per_run')); midcom::get('auth')->request_sudo('midcom.services.at'); $qbret = $qb->execute(); midcom::get('auth')->drop_sudo(); if (empty($qbret)) { debug_add('Got empty resultset, exiting'); return; } foreach ($qbret as $entry) { debug_add("Processing entry #{$entry->id}\n"); //Avoid double-execute in case of long runs $entry->status = midcom_services_at_entry_dba::RUNNING; midcom::get('auth')->request_sudo('midcom.services.at'); $entry->update(); midcom::get('auth')->drop_sudo(); midcom::get('componentloader')->load($entry->component); $args = $entry->arguments; $args['midcom_services_at_entry_object'] = $entry; $interface = midcom::get('componentloader')->get_interface_class($entry->component); $method = $entry->method; if (!is_callable(array($interface, $method))) { $error = "\$interface->{$method}() is not callable"; $this->print_error($error); debug_add($error, MIDCOM_LOG_ERROR); debug_add('$interface is ' . get_class($interface)); debug_print_r('$args', $args); //PONDER: Delete instead ? (There is currently nothing we do with failed entries) $entry->status = midcom_services_at_entry_dba::FAILED; midcom::get('auth')->request_sudo('midcom.services.at'); $entry->update(); midcom::get('auth')->drop_sudo(); continue; } $mret = $interface->{$method}($args, $this); if ($mret !== true) { $error = "\$interface->{$method}(\$args, \$this) returned '{$mret}', errstr: " . midcom_connection::get_error_string(); $this->print_error($error); debug_add($error, MIDCOM_LOG_ERROR); debug_add('$interface is ' . get_class($interface)); debug_print_r('$args', $args); //PONDER: Delete instead ? (There is currently nothing we do with failed entries) $entry->status = midcom_services_at_entry_dba::FAILED; midcom::get('auth')->request_sudo('midcom.services.at'); $entry->update(); midcom::get('auth')->drop_sudo(); } else { midcom::get('auth')->request_sudo('midcom.services.at'); $entry->delete(); midcom::get('auth')->drop_sudo(); } } }
/** * Function to disable account for time period given in config * * @return boolean - indicates success */ public function disable_account() { $this->_account = midcom_core_account::get($this->_person); $timeframe_minutes = $this->_config->get('password_block_timeframe_min'); if ($timeframe_minutes == 0) { return false; } $release_time = time() + $timeframe_minutes * 60; $args = array('guid' => $this->_person->guid, 'parameter_name' => 'org_openpsa_user_blocked_account', 'password' => 'account_password'); $qb = midcom_services_at_entry_dba::new_query_builder(); $qb->add_constraint('argumentsstore', '=', serialize($args)); $qb->add_constraint('status', '=', midcom_services_at_entry_dba::SCHEDULED); $results = $qb->execute(); if (sizeof($results) > 0) { //the account is already blocked, so we just extend the block's duration $entry = $results[0]; $entry->start = $release_time; return $entry->update(); } if (!midcom_services_at_interface::register($release_time, 'org.openpsa.user', 'reopen_account', $args)) { throw new midcom_error("Failed to register interface for re_open the user account, last Midgard error was: " . midcom_connection::get_error_string()); } $this->_person->set_parameter("org_openpsa_user_blocked_account", "account_password", $this->_account->get_password()); $this->_account->set_password('', false); return $this->_account->save(); }
/** * Displays a deliverable edit view. * * Note, that the deliverable for non-index mode operation is automatically determined in the can_handle * phase. * * If create privileges apply, we relocate to the index creation deliverable * * @param mixed $handler_id The ID of the handler. * @param Array $args The argument list. * @param Array &$data The local request data. */ public function _handler_edit($handler_id, array $args, array &$data) { $this->_deliverable = new org_openpsa_sales_salesproject_deliverable_dba($args[0]); $this->_deliverable->require_do('midgard:update'); $this->_load_controller(); switch ($this->_controller->process_form()) { case 'save': $formdata = $this->_controller->datamanager->types; if (!empty($formdata['at_entry']->value)) { $entry = new midcom_services_at_entry_dba((int) $formdata['at_entry']->value); $next_cycle = 0; if (isset($formdata['next_cycle']) && !$formdata['next_cycle']->is_empty()) { $next_cycle = (int) $formdata['next_cycle']->value->format('U'); } //@todo If next_cycle is changed to be in the past, should we check if this would lead //to multiple runs immediately? i.e. if you set a monthly subscriptions next cycle to //one year in the past, this would trigger twelve consecutive runs and maybe //the user needs to be warned about that... if ($next_cycle != $entry->start) { $entry->start = $next_cycle; $entry->update(); } } $this->_master->process_notify_date($formdata, $this->_deliverable); // Reindex the deliverable //$indexer = midcom::get('indexer'); //org_openpsa_sales_viewer::index($this->_controller->datamanager, $indexer, $this->_content_topic); // *** FALL-THROUGH *** // Reindex the deliverable //$indexer = midcom::get('indexer'); //org_openpsa_sales_viewer::index($this->_controller->datamanager, $indexer, $this->_content_topic); // *** FALL-THROUGH *** case 'cancel': return new midcom_response_relocate("deliverable/{$this->_deliverable->guid}/"); } // Add toolbar items org_openpsa_helpers::dm2_savecancel($this); $this->_prepare_request_data($handler_id); $this->bind_view_to_object($this->_deliverable, $this->_request_data['controller']->datamanager->schema->name); $this->_update_breadcrumb_line($handler_id); midcom::get('head')->set_pagetitle(sprintf($this->_l10n_midcom->get('edit %s'), $this->_deliverable->title)); }
private function _create_at_entry($cycle_number, $start) { $args = array('deliverable' => $this->_deliverable->guid, 'cycle' => $cycle_number); $at_entry = new midcom_services_at_entry_dba(); $at_entry->start = $start; $at_entry->component = 'org.openpsa.sales'; $at_entry->method = 'new_subscription_cycle'; $at_entry->arguments = $args; if ($at_entry->create()) { debug_add('AT entry for cycle ' . $cycle_number . ' created'); org_openpsa_relatedto_plugin::create($at_entry, 'midcom.services.at', $this->_deliverable, 'org.openpsa.sales'); return true; } else { debug_add('AT registration failed, last midgard error was: ' . midcom_connection::get_error_string(), MIDCOM_LOG_WARN); return false; } }
private function _get_scheduled_invoices() { $invoices = array(); $at_qb = midcom_services_at_entry_dba::new_query_builder(); $at_qb->add_constraint('method', '=', 'new_subscription_cycle'); $at_qb->add_constraint('component', '=', 'org.openpsa.sales'); $at_entries = $at_qb->execute(); foreach ($at_entries as $at_entry) { try { $deliverable = org_openpsa_sales_salesproject_deliverable_dba::get_cached($at_entry->arguments['deliverable']); if ($deliverable->continuous || $deliverable->start < $this->_request_data['end'] && $deliverable->end > $this->_request_data['start']) { $invoices = array_merge($invoices, $this->_get_invoices_for_subscription($deliverable, $at_entry)); } } catch (midcom_error $e) { } } $invoices = array_filter($invoices, array($this, '_filter_by_date')); usort($invoices, array($this, '_sort_by_date')); return $invoices; }
/** * @depends testGenerate_safe_password */ public function testDisable_account() { $accounthelper = new org_openpsa_user_accounthelper(self::$_user); $account = midcom_core_account::get(self::$_user); $password = $account->get_password(); midcom::get('auth')->request_sudo('org.openpsa.user'); $this->assertTrue($accounthelper->disable_account()); $account = midcom_core_account::get(self::$_user); $this->assertEquals('', $account->get_password()); $this->assertEquals($password, self::$_user->get_parameter('org_openpsa_user_blocked_account', 'account_password')); $args = array('guid' => self::$_user->guid, 'parameter_name' => 'org_openpsa_user_blocked_account', 'password' => 'account_password'); $qb = midcom_services_at_entry_dba::new_query_builder(); $qb->add_constraint('argumentsstore', '=', serialize($args)); $result = $qb->execute(); $this->register_objects($result); $this->assertEquals(1, sizeof($result)); $account->set_password($accounthelper->generate_safe_password()); $account->save(); midcom::get('auth')->drop_sudo(); }