diff options
author | Florian Pritz <bluewind@xinu.at> | 2012-04-09 11:26:22 +0200 |
---|---|---|
committer | Florian Pritz <bluewind@xinu.at> | 2012-04-09 20:48:16 +0200 |
commit | c0f1755842211996a7a21ffb8773b260bb2be281 (patch) | |
tree | 609107150d2ee82cdbb3ed9157ceccf0cf98f8a7 | |
parent | cb8b733a6c2333ff90d3af0fc37e8c21c73d9e7a (diff) |
Implement simple referral system
Signed-off-by: Florian Pritz <bluewind@xinu.at>
-rw-r--r-- | application/config/migration.php | 2 | ||||
-rw-r--r-- | application/controllers/user.php | 49 | ||||
-rw-r--r-- | application/migrations/003_add_referrers.php | 33 | ||||
-rw-r--r-- | application/views/user/invite.php | 10 |
4 files changed, 93 insertions, 1 deletions
diff --git a/application/config/migration.php b/application/config/migration.php index 274e792a6..3e824520a 100644 --- a/application/config/migration.php +++ b/application/config/migration.php @@ -21,7 +21,7 @@ $config['migration_enabled'] = true; | be upgraded / downgraded to. | */ -$config['migration_version'] = 2; +$config['migration_version'] = 3; /* diff --git a/application/controllers/user.php b/application/controllers/user.php index 722996d93..4fffbef9b 100644 --- a/application/controllers/user.php +++ b/application/controllers/user.php @@ -57,6 +57,55 @@ class User extends CI_Controller { $this->load->view($this->var->view_dir.'footer', $this->data); } } + + function create_invitation_key() + { + $this->muser->require_access(); + + $userid = $this->muser->get_userid(); + + // TODO: count both, invited users and key + $query = $this->db->query(" + SELECT count(*) as count + FROM invitations + WHERE user = ? + ", array($userid))->row_array(); + + if ($query["count"] + 1 > 3) { + // TODO: better message + echo "You've reached your invitation limit."; + return; + } + + $key = random_id(12, 16); + + $this->db->query(" + INSERT INTO invitations + (`key`, `user`, `date`) + VALUES (?, ?, ?) + ", array($key, $userid, time())); + + redirect("user/invite"); + } + + function invite() + { + $this->muser->require_access(); + + $userid = $this->muser->get_userid(); + + $query = $this->db->query(" + SELECT * + FROM invitations + WHERE user = ? + ", array($userid))->result_array(); + + $this->data["query"] = $query; + + $this->load->view($this->var->view_dir.'header', $this->data); + $this->load->view($this->var->view_dir.'invite', $this->data); + $this->load->view($this->var->view_dir.'footer', $this->data); + } function logout() { diff --git a/application/migrations/003_add_referrers.php b/application/migrations/003_add_referrers.php new file mode 100644 index 000000000..524e92ff0 --- /dev/null +++ b/application/migrations/003_add_referrers.php @@ -0,0 +1,33 @@ +<?php +defined('BASEPATH') OR exit('No direct script access allowed'); + +class Migration_Add_referrers extends CI_Migration { + + public function up() + { + $this->db->query(" + CREATE TABLE `invitations` ( + `user` int(8) unsigned NOT NULL, + `key` varchar(16) CHARACTER SET ascii NOT NULL, + `date` int(11) unsigned NOT NULL, + PRIMARY KEY (`key`), + KEY `user` (`user`), + KEY `date` (`date`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin + "); + $this->db->query(" + ALTER TABLE `users` + ADD `referrer` INT(8) UNSIGNED NOT NULL DEFAULT '0' + "); + } + + public function down() + { + $this->db->query(" + ALTER TABLE `users` + DROP `referrer` + "); + $this->dbforge->drop_table('invitations'); + + } +} diff --git a/application/views/user/invite.php b/application/views/user/invite.php new file mode 100644 index 000000000..f97804274 --- /dev/null +++ b/application/views/user/invite.php @@ -0,0 +1,10 @@ +<?php echo form_open('user/create_invitation_key'); ?> + <input type="submit" value="Create new key" name="process" /> +</form> + +<p>Unused invitation keys:</p> +<p> +<?php foreach($query as $key => $item): ?> + <?php echo $item["key"]; ?><br /> +<?php endforeach; ?> +</p> |