Example #1
0
 /**
  * import in database
  * 
  * @param array $records
  * @param boolean $replace existing events with same id
  * @return boolean true on success
  */
 public function import($records, $replace = 0)
 {
     $count = array('added' => 0, 'updated' => 0);
     $tables = $this->_db->getTableFields(array('#__redevent_events', '#__redevent_event_venue_xref'), false);
     $current = null;
     // current event for sessions
     foreach ($records as $r) {
         $row = Jtable::getInstance('Redevent_customfield', '');
         $row->bind($r);
         if (!$replace) {
             $row->id = null;
             $update = 0;
         } else {
             if ($row->id) {
                 $update = 1;
             }
         }
         // store !
         if (!$row->check()) {
             JError::raiseWarning(0, JText::_('COM_REDEVENT_IMPORT_ERROR') . ': ' . $row->getError());
             continue;
         }
         if (!$row->store()) {
             JError::raiseWarning(0, JText::_('COM_REDEVENT_IMPORT_ERROR') . ': ' . $row->getError());
             continue;
         }
         // add the field to the object table
         switch ($row->object_key) {
             case 'redevent.event':
                 $table = '#__redevent_events';
                 break;
             case 'redevent.xref':
                 $table = '#__redevent_event_venue_xref';
                 break;
             default:
                 JError::raiseWarning(0, 'undefined custom field object_key');
                 break;
         }
         $cols = $tables[$table];
         if (!array_key_exists('custom' . $row->id, $cols)) {
             switch ($row->type) {
                 default:
                     // for now, let's not restrict the type...
                     $columntype = 'TEXT';
             }
             $q = 'ALTER IGNORE TABLE ' . $table . ' ADD COLUMN custom' . $row->id . ' ' . $columntype;
             $this->_db->setQuery($q);
             if (!$this->_db->query()) {
                 JError::raiseWarning(0, 'failed adding custom field to table');
             }
         }
     }
     return $count;
 }
 /**
  * Method to store a row in the database from the FOFTable instance properties.
  * If a primary key value is set the row with that primary key value will be
  * updated with the instance property values.  If no primary key value is set
  * a new row will be inserted into the database with the properties from the
  * FOFTable instance.
  *
  * @param   boolean  $updateNulls  True to update fields even if they are null.
  *
  * @return  boolean  True on success.
  */
 public function store($updateNulls = false)
 {
     if (!$this->onBeforeStore($updateNulls)) {
         return false;
     }
     $k = $this->_tbl_key;
     if (!empty($this->asset_id)) {
         $currentAssetId = $this->asset_id;
     }
     if (0 == $this->{$k}) {
         $this->{$k} = null;
     }
     // The asset id field is managed privately by this class.
     if ($this->_trackAssets) {
         unset($this->asset_id);
     }
     // If a primary key exists update the object, otherwise insert it.
     if ($this->{$k}) {
         $this->_db->updateObject($this->_tbl, $this, $this->_tbl_key, $updateNulls);
     } else {
         $this->_db->insertObject($this->_tbl, $this, $this->_tbl_key);
     }
     // If the table is not set to track assets return true.
     if (!$this->_trackAssets) {
         $result = true;
         return $this->onAfterStore();
     }
     if ($this->_locked) {
         $this->_unlock();
     }
     /*
      * Asset Tracking
      */
     $parentId = $this->_getAssetParentId();
     $name = $this->_getAssetName();
     $title = $this->_getAssetTitle();
     $asset = Jtable::getInstance('Asset', 'JTable', array('dbo' => $this->getDbo()));
     $asset->loadByName($name);
     // Re-inject the asset id.
     $this->asset_id = $asset->id;
     // Check for an error.
     $error = $asset->getError();
     if ($error) {
         $this->setError($error);
         return false;
     }
     // Specify how a new or moved node asset is inserted into the tree.
     if (empty($this->asset_id) || $asset->parent_id != $parentId) {
         $asset->setLocation($parentId, 'last-child');
     }
     // Prepare the asset to be stored.
     $asset->parent_id = $parentId;
     $asset->name = $name;
     $asset->title = $title;
     if ($this->_rules instanceof JAccessRules) {
         $asset->rules = (string) $this->_rules;
     }
     if (!$asset->check() || !$asset->store($updateNulls)) {
         $this->setError($asset->getError());
         return false;
     }
     // Create an asset_id or heal one that is corrupted.
     if (empty($this->asset_id) || $currentAssetId != $this->asset_id && !empty($this->asset_id)) {
         // Update the asset_id field in this table.
         $this->asset_id = (int) $asset->id;
         $query = $this->_db->getQuery(true);
         $query->update($this->_db->qn($this->_tbl));
         $query->set('asset_id = ' . (int) $this->asset_id);
         $query->where($this->_db->qn($k) . ' = ' . (int) $this->{$k});
         $this->_db->setQuery($query);
         $this->_db->execute();
     }
     $result = true;
     $result = $this->onAfterStore();
     return $result;
 }
Example #3
0
 public function importUsers()
 {
     $mainframe = JFactory::getApplication();
     $jinput = $mainframe->input;
     $csv = $jinput->files->get('csv');
     $groups = $jinput->get('group', array(), 'array');
     // selected groups
     $events = $jinput->get('event', array(), 'array');
     // selected events
     $users = array();
     $i = 0;
     ini_set('auto_detect_line_endings', true);
     // we need to detect the new line break automatically
     $handle = fopen($csv['tmp_name'], "r");
     if ($handle) {
         while (!feof($handle)) {
             $results = fgetcsv($handle);
             //we must check if every results exists, else, return the error
             if (!$results[0] || !$results[1] || !$results[2] || count($results) > 3) {
                 //redirect and display error
                 fclose($handle);
                 $url = JRoute::_('index.php?option=com_community&view=users', false);
                 $message = JText::_('COM_COMMUNITY_USERS_CSV_FILE_ERROR');
                 $mainframe->redirect($url, $message, 'error');
             }
             $users[$i] = $results;
             $i++;
         }
     } else {
         //redirect and display error
         $url = JRoute::_('index.php?option=com_community&view=users', false);
         $message = JText::_('COM_COMMUNITY_USERS_CSV_FILE_ERROR');
         $mainframe->redirect($url, $message, 'error');
     }
     fclose($handle);
     $totalusers = count($users);
     if (!$totalusers) {
         //if it's empty
         //redirect and display error
         $url = JRoute::_('index.php?option=com_community&view=users', false);
         $message = JText::_('COM_COMMUNITY_USERS_CSV_FILE_ERROR');
         $mainframe->redirect($url, $message, 'error');
     }
     $duplicates = 0;
     $db = JFactory::getDbo();
     $groupTable = Jtable::getInstance('Groups', 'CommunityTable');
     $eventTable = Jtable::getInstance('Events', 'CommunityTable');
     //we must make sure the mail notification is set to no before proceeding
     $userPlugin = JPluginHelper::getPlugin('user', 'joomla');
     $params = new CParameter($userPlugin->params);
     $sendNotification = $params->get('mail_to_user', 1);
     if ($sendNotification) {
         //redirect and display error
         $url = JRoute::_('index.php?option=com_community&view=users', false);
         $message = JText::_('COM_COMMUNITY_EMAIL_IMPORT_USER_PLUGIN_SETTING_ENABLED_ERROR');
         $mainframe->redirect($url, $message, 'error');
     }
     //lets try to create the users
     foreach ($users as $user) {
         //check if the user already exists in the system
         $email = trim($user[2]);
         $name = trim($user[0]);
         $query = 'SELECT username FROM ' . $db->quoteName('#__users') . ' WHERE email=' . $db->quote($email) . ' OR username='******'name' => $name, 'username' => $email, "password" => $randomPassword, "password2" => $randomPassword, "email" => $email, "block" => 0, "groups" => array(2));
         $newUser = new JUser();
         $newUser->bind($data);
         if ($newUser->save()) {
             $cuser = CFactory::getUser($user->id);
             $cuser->save();
         }
         $mailq = CFactory::getModel('Mailq');
         $emailSubject = JText::sprintf('COM_COMMUNITY_EMAIL_IMPORT_USER_WELCOME_SUBJECT', JFactory::getConfig()->get('sitename'));
         $mailBody = JText::_("COM_COMMUNITY_EMAIL_IMPORT_USER_WELCOME_BODY");
         $params = new CParameter();
         $params->set('site_url', JURI::root());
         $params->set('username', $email);
         $params->set('password', $randomPassword);
         $params->set('target', $name);
         //add the user details to mail queue
         $mailq->add($email, $emailSubject, $mailBody, '', $params, 0, 'etype_users_new_invite');
         //if we have groups, we will assign this user to the group
         if (count($groups) > 0) {
             foreach ($groups as $group) {
                 $data = new stdClass();
                 $data->groupid = $group;
                 $data->memberid = $newUser->id;
                 $data->approved = 1;
                 $data->permissions = 1;
                 $groupTable->addMember($data);
                 $groupTable->addMembersCount($group);
             }
         }
         // same goes for events
         if (count($events) > 0) {
             foreach ($events as $event) {
                 $data = new stdClass();
                 $data->eventid = $event;
                 $data->memberid = $newUser->id;
                 $data->approval = 0;
                 $data->permission = 3;
                 //members
                 $data->status = 1;
                 $eventTable->addMember($data);
             }
         }
     }
     $url = JRoute::_('index.php?option=com_community&view=users', false);
     $message = JText::sprintf('COM_COMMUNITY_USERS_IMPORT_USER_SUCCESS', $totalusers - $duplicates, $duplicates);
     $mainframe->redirect($url, $message, 'message');
 }
Example #4
0
 /**
  * import in database
  * 
  * @param array $records
  * @param boolean $replace existing events with same id
  * @return boolean true on success
  */
 public function import($records, $replace = 0)
 {
     $count = array('added' => 0, 'updated' => 0);
     $current = null;
     // current event for sessions
     foreach ($records as $r) {
         $v = Jtable::getInstance('Textlibrary', 'Table');
         $v->bind($r);
         if (!$replace) {
             $v->id = null;
             $update = 0;
         } else {
             if ($v->id) {
                 $update = 1;
             }
         }
         // store !
         if (!$v->check()) {
             JError::raiseWarning(0, JText::_('COM_REDEVENT_IMPORT_ERROR') . ': ' . $v->getError());
             continue;
         }
         if (!$v->store()) {
             JError::raiseWarning(0, JText::_('COM_REDEVENT_IMPORT_ERROR') . ': ' . $v->getError());
             continue;
         }
     }
     return $count;
 }