1: <?php
2: /**
3: * This file contains the article collection and item class.
4: *
5: * @package Core
6: * @subpackage GenericDB_Model
7: * @author Timo Hummel
8: * @copyright four for business AG <www.4fb.de>
9: * @license http://www.contenido.org/license/LIZENZ.txt
10: * @link http://www.4fb.de
11: * @link http://www.contenido.org
12: */
13:
14: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
15:
16: cInclude('includes', 'functions.str.php');
17:
18: /**
19: * Article collection
20: *
21: * @package Core
22: * @subpackage GenericDB_Model
23: */
24: class cApiArticleCollection extends ItemCollection {
25: /**
26: * Constructor to create an instance of this class.
27: *
28: * @param bool $select [optional]
29: * where clause to use for selection (see ItemCollection::select())
30: *
31: * @throws cDbException
32: * @throws cInvalidArgumentException
33: */
34: public function __construct($select = false) {
35: global $cfg;
36: parent::__construct($cfg['tab']['art'], 'idart');
37: $this->_setItemClass('cApiArticle');
38:
39: // set the join partners so that joins can be used via link() method
40: $this->_setJoinPartner('cApiClientCollection');
41:
42: if ($select !== false) {
43: $this->select($select);
44: }
45: }
46:
47: /**
48: * Creates an article item entry
49: *
50: * @param int $idclient
51: *
52: * @return cApiArticle
53: *
54: * @throws cDbException
55: * @throws cException
56: * @throws cInvalidArgumentException
57: */
58: public function create($idclient) {
59: $item = $this->createNewItem();
60:
61: $item->set('idclient', $idclient);
62: $item->store();
63:
64: return $item;
65: }
66:
67: /**
68: * Returns list of ids by given client id.
69: *
70: * @param int $idclient
71: *
72: * @return array
73: *
74: * @throws cDbException
75: */
76: public function getIdsByClientId($idclient) {
77: $sql = "SELECT idart FROM `%s` WHERE idclient=%d";
78: $this->db->query($sql, $this->table, $idclient);
79: $list = array();
80: while ($this->db->next_record()) {
81: $list[] = $this->db->f('idart');
82: }
83: return $list;
84: }
85: }
86:
87: /**
88: * Article item
89: *
90: * @package Core
91: * @subpackage GenericDB_Model
92: */
93: class cApiArticle extends Item
94: {
95: /**
96: * Constructor to create an instance of this class.
97: *
98: * @param mixed $mId [optional]
99: * Specifies the ID of item to load
100: *
101: * @throws cDbException
102: * @throws cException
103: */
104: public function __construct($mId = false) {
105: global $cfg;
106: parent::__construct($cfg['tab']['art'], 'idart');
107: $this->setFilters(array(), array());
108: if ($mId !== false) {
109: $this->loadByPrimaryKey($mId);
110: }
111: }
112:
113: /**
114: * Returns the link to the current object.
115: *
116: * @param int $changeLangId [optional]
117: * change language id for URL (optional)
118: *
119: * @return string
120: * link
121: *
122: * @throws cInvalidArgumentException
123: */
124: public function getLink($changeLangId = 0) {
125: if ($this->isLoaded() === false) {
126: return '';
127: }
128:
129: $options = array();
130: $options['idart'] = $this->get('idart');
131: $options['lang'] = ($changeLangId == 0) ? cRegistry::getLanguageId() : $changeLangId;
132: if ($changeLangId > 0) {
133: $options['changelang'] = $changeLangId;
134: }
135:
136: return cUri::getInstance()->build($options);
137: }
138:
139: /**
140: * Userdefined setter for article fields.
141: *
142: * @param string $name
143: * @param mixed $value
144: * @param bool $bSafe [optional]
145: * Flag to run defined inFilter on passed value
146: * @return bool
147: */
148: public function setField($name, $value, $bSafe = true) {
149: switch ($name) {
150: case 'idclient':
151: $value = (int) $value;
152: break;
153: }
154:
155: return parent::setField($name, $value, $bSafe);
156: }
157:
158: }
159: