public function post() { $input = JFactory::getApplication()->input; $user = $this->plugin->getUser(); if (!$user) { $this->plugin->setResponse($this->getErrorResponse(404, JText::_('JERROR_ALERTNOAUTHOR'))); return; } $authorised = $user->authorise('core.create', 'com_akeebasubs'); $override_users = $this->getOverrideUsers(); $is_override = in_array($user->id, array_keys($override_users)); $allowed = $authorised || $is_override; if (!$allowed) { $this->plugin->setResponse($this->getErrorResponse(404, JText::_('JERROR_ALERTNOAUTHOR'))); return; } // Check/create user $newuser['name'] = $input->get('name', '', 'STRING'); $newuser['email'] = $input->get('email', '', 'USERNAME'); $newuser['username'] = $newuser['email']; $subscriber_userid = $this->getSubscriberUserid($newuser); if (!$subscriber_userid) { $this->plugin->setResponse($this->getErrorResponse(404, 'Problem creating user. Name and email are mandatory. Make sure you use a valid email')); return; } // Create subscription $levelid = $input->get('subscription', '', 'INT'); $enabled = $input->get('enabled', 0, 'INT'); $state = $enabled ? 'C' : 'N'; // Do validations if ($is_override && !in_array($levelid, $override_users[$user->id])) { $this->plugin->setResponse($this->getErrorResponse(404, 'Invalid subscription id')); return; } if (!$this->checkLevel($levelid)) { $this->plugin->setResponse($this->getErrorResponse(404, 'Invalid subscription id')); return; } $jNow = new JDate(); $now = $jNow->toUnix(); $mNow = $jNow->toSql(); $startDate = $now; $nullDate = JFactory::getDbo()->getNullDate(); $level = FOFModel::getTmpInstance('Levels', 'AkeebasubsModel')->setId($levelid)->getItem(); if ($level->forever) { $jStartDate = new JDate(); $endDate = '2038-01-01 00:00:00'; } elseif (!is_null($level->fixed_date) && $level->fixed_date != $nullDate) { $jStartDate = new JDate(); $endDate = $level->fixed_date; } else { $jStartDate = new JDate($startDate); // Subscription duration (length) modifiers, via plugins $duration_modifier = 0; JLoader::import('joomla.plugin.helper'); JPluginHelper::importPlugin('akeebasubs'); $app = JFactory::getApplication(); $jResponse = $app->triggerEvent('onValidateSubscriptionLength', array($state)); if (is_array($jResponse) && !empty($jResponse)) { foreach ($jResponse as $pluginResponse) { if (empty($pluginResponse)) { continue; } $duration_modifier += $pluginResponse; } } // Calculate the effective duration $duration = (int) $level->duration + $duration_modifier; if ($duration <= 0) { $duration = 0; } $duration = $duration * 3600 * 24; $endDate = $startDate + $duration; } $mStartDate = $jStartDate->toSql(); $jEndDate = new JDate($endDate); $mEndDate = $jEndDate->toSql(); $note = 'Subscription created via API.' . PHP_EOL . 'API User : %1$s' . PHP_EOL . 'API Userid : %2$s' . PHP_EOL . 'Subscriber Name : %3$s' . PHP_EOL . 'Subscriber Email : %4$s' . PHP_EOL . 'Date : %5$s' . PHP_EOL . 'Level id : %6$s'; $subscription = FOFTable::getInstance('Subscription', 'AkeebasubsTable'); $subscription->user_id = (int) $subscriber_userid; $subscription->akeebasubs_level_id = $levelid; $subscription->enabled = $enabled; $subscription->state = $state; $subscription->processor = 'api'; $subscription->created_on = $mStartDate; $subscription->publish_up = $mStartDate; $subscription->publish_down = $mEndDate; $subscription->notes = sprintf($note, $user->name, $user->id, $newuser['name'], $newuser['email'], date('Y-m-d H:i:s'), $levelid); if ($subscription->store()) { // Send email //$mailer = AkeebasubsHelperEmail::getPreloadedMailer($subscription, 'plg_akeebasubs_subscriptionemails_new_active'); //$mailer->addRecipient($newuser['email']); //$send = $mailer->Send(); $this->plugin->setResponse(array('code' => 200, 'id' => $subscription->akeebasubs_subscription_id)); } else { $this->plugin->setResponse($this->getErrorResponse(404, 'There was a problem creating the subscription. Make sure you use a valid level id. Name and email are mandatory.')); } }
/** * Database iterator constructor. * * @param mixed $cursor The database cursor. * @param string $column An option column to use as the iterator key. * @param string $class The table class of the returned objects. * @param array $config Configuration parameters to push to the table class * * @throws InvalidArgumentException */ public function __construct($cursor, $column = null, $class, $config = array()) { // Figure out the type and prefix of the class by the class name $parts = FOFInflector::explode($class); if (count($parts) != 3) { throw new InvalidArgumentException('Invalid table name, expected a pattern like ComponentTableFoobar got ' . $class); } $this->_tableObject = FOFTable::getInstance($parts[2], ucfirst($parts[0]) . ucfirst($parts[1]))->getClone(); $this->cursor = $cursor; $this->class = 'stdClass'; $this->_column = $column; $this->_fetched = 0; $this->next(); }
/** * Returns a FOFDatabaseIterator based on a given relation * * @param array $relation Indexed array holding relation definition. * tableClass => name of the related table class * localKey => name of the local key * remoteKey => name of the remote key * pivotTable => name of the pivot table (optional) * theirPivotKey => name of the remote key in the pivot table (mandatory if pivotTable is set) * ourPivotKey => name of our key in the pivot table (mandatory if pivotTable is set) * * @return FOFDatabaseIterator * * @throws RuntimeException * @throws InvalidArgumentException */ protected function getIteratorFromRelation($relation) { // Sanity checks if (!isset($relation['tableClass']) || !isset($relation['remoteKey']) || !isset($relation['localKey']) || !$relation['tableClass'] || !$relation['remoteKey'] || !$relation['localKey']) { throw new InvalidArgumentException('Missing array index for the ' . __METHOD__ . ' method. Please check method signature', 500); } if (array_key_exists('pivotTable', $relation)) { if (!isset($relation['theirPivotKey']) || !isset($relation['ourPivotKey']) || !$relation['pivotTable'] || !$relation['theirPivotKey'] || !$relation['ourPivotKey']) { throw new InvalidArgumentException('Missing array index for the ' . __METHOD__ . ' method. Please check method signature', 500); } } // Get a table object from the table class name $tableClass = $relation['tableClass']; $tableClassParts = FOFInflector::explode($tableClass); if (count($tableClassParts) < 3) { throw new InvalidArgumentException('Invalid table class named. It should be something like FooTableBar'); } $table = FOFTable::getInstance($tableClassParts[2], ucfirst($tableClassParts[0]) . ucfirst($tableClassParts[1])); // Get the table name $tableName = $table->getTableName(); // Get the remote and local key names $remoteKey = $relation['remoteKey']; $localKey = $relation['localKey']; // Get the local key's value $value = $this->table->{$localKey}; // If there's no value for the primary key, let's stop here if (!$value) { throw new RuntimeException('Missing value for the primary key of the table ' . $this->table->getTableName(), 500); } // This is required to prevent one relation from killing the db cursor used in a different relation... $oldDb = $this->table->getDbo(); $oldDb->disconnect(); // YES, WE DO NEED TO DISCONNECT BEFORE WE CLONE THE DB OBJECT. ARGH! $db = clone $oldDb; // Begin the query $query = $db->getQuery(true)->select('*')->from($db->qn($tableName)); // Do we have a pivot table? $hasPivot = array_key_exists('pivotTable', $relation); // If we don't have pivot it's a straightforward query if (!$hasPivot) { $query->where($db->qn($remoteKey) . ' = ' . $db->q($value)); } else { $subQuery = $db->getQuery(true)->select($db->qn($relation['theirPivotKey']))->from($db->qn($relation['pivotTable']))->where($db->qn($relation['ourPivotKey']) . ' = ' . $db->q($value)); $query->where($db->qn($remoteKey) . ' IN (' . $subQuery . ')'); } $db->setQuery($query); $cursor = $db->execute(); $iterator = FOFDatabaseIterator::getIterator($db->name, $cursor, null, $tableClass); return $iterator; }
/** * Method to get the field options. * * @return array The field option objects. */ protected function getOptions() { $options = array(); $this->value = array(); $value_field = $this->element['value_field'] ? (string) $this->element['value_field'] : 'title'; $input = new FOFInput(); $component = ucfirst(str_replace('com_', '', $input->getString('option'))); $view = ucfirst($input->getString('view')); $relation = FOFInflector::pluralize((string) $this->element['name']); $model = FOFModel::getTmpInstance(ucfirst($relation), $component . 'Model'); $table = $model->getTable(); $key = $table->getKeyName(); $value = $table->getColumnAlias($value_field); foreach ($model->getItemList(true) as $option) { $options[] = JHtml::_('select.option', $option->{$key}, $option->{$value}); } if ($id = FOFModel::getAnInstance($view)->getId()) { $table = FOFTable::getInstance($view, $component . 'Table'); $table->load($id); $relations = $table->getRelations()->getMultiple($relation); foreach ($relations as $item) { $this->value[] = $item->getId(); } } return $options; }
/** * Returns a FOFDatabaseIterator based on a given relation * * @param array $relation Relation definition * * @return FOFDatabaseIterator * * @throws RuntimeException */ private function getIteratorFromRelation($relation) { // Get a table object from the table class name $tableClass = $relation['tableClass']; $tableClassParts = FOFInflector::explode($tableClass); $table = FOFTable::getInstance($tableClassParts[2], ucfirst($tableClassParts[0]) . ucfirst($tableClassParts[1])); // Get the table name $tableName = $table->getTableName(); // Get the remote and local key names $remoteKey = $relation['remoteKey']; $localKey = $relation['localKey']; // Get the local key's value $value = $this->table->{$localKey}; // This is required to prevent one relation from killing the db cursor used in a different relation... $oldDb = $this->table->getDbo(); $oldDb->disconnect(); // YES, WE DO NEED TO DISCONNECT BEFORE WE CLONE THE DB OBJECT. ARGH! $db = clone $oldDb; // Begin the query $query = $db->getQuery(true)->select('*')->from($db->qn($tableName)); // Do we have a pivot table? $hasPivot = array_key_exists('pivotTable', $relation); // If we don't have pivot it's a straightforward query if (!$hasPivot) { $query->where($db->qn($remoteKey) . ' = ' . $db->q($value)); } else { $subQuery = $db->getQuery(true)->select($db->qn($relation['theirPivotKey']))->from($db->qn($relation['pivotTable']))->where($db->qn($relation['ourPivotKey']) . ' = ' . $db->q($value)); $query->where($db->qn($remoteKey) . ' IN (' . $subQuery . ')'); } $db->setQuery($query); $cursor = $db->execute(); $iterator = FOFDatabaseIterator::getIterator($db->name, $cursor, null, $tableClass); return $iterator; }
/** * Database iterator constructor. * * @param mixed $cursor The database cursor. * @param string $column An option column to use as the iterator key. * @param string $class The table class of the returned objects. * @param array $config Configuration parameters to push to the table class * * @throws InvalidArgumentException */ public function __construct($cursor, $column = null, $class, $config = array()) { // Figure out the type and prefix of the class by the class name $parts = FOFInflector::explode($class); $this->_tableObject = FOFTable::getInstance($parts[2], ucfirst($parts[0]) . ucfirst($parts[1])); $this->cursor = $cursor; $this->class = 'stdClass'; $this->_column = $column; $this->_fetched = 0; $this->next(); }