1: <?php
2:
3: /**
4: * Deep
5: *
6: * @package rsanchez\Deep
7: * @author Rob Sanchez <info@robsanchez.com>
8: */
9:
10: namespace rsanchez\Deep\Model;
11:
12: use Illuminate\Database\Eloquent\Model;
13: use rsanchez\Deep\Model\Field;
14: use rsanchez\Deep\Repository\FieldRepository;
15: use rsanchez\Deep\Collection\ChannelCollection;
16: use rsanchez\Deep\Relations\HasManyFromRepository;
17:
18: /**
19: * Model for the channels table
20: */
21: class Channel extends Model
22: {
23: /**
24: * {@inheritdoc}
25: *
26: * @var string
27: */
28: protected $table = 'channels';
29:
30: /**
31: * {@inheritdoc}
32: *
33: * @var string
34: */
35: protected $primaryKey = 'channel_id';
36:
37: /**
38: * {@inheritdoc}
39: */
40: protected = array('fields');
41:
42: /**
43: * Global Field Repository
44: * @var \rsanchez\Deep\Repository\FieldRepository
45: */
46: public static $fieldRepository;
47:
48: /**
49: * Set the global FieldRepository
50: * @param \rsanchez\Deep\Repository\FieldRepository $fieldRepository
51: * @return void
52: */
53: public static function setFieldRepository(FieldRepository $fieldRepository)
54: {
55: self::$fieldRepository = $fieldRepository;
56: }
57:
58: /**
59: * {@inheritdoc}
60: *
61: * @param array $models
62: * @return \rsanchez\Deep\Collection\ChannelCollection
63: */
64: public function newCollection(array $models = array())
65: {
66: return new ChannelCollection($models);
67: }
68:
69: /**
70: * Define the Fields Eloquent relationship
71: * @return \rsanchez\Deep\Relations\HasManyFromRepository
72: */
73: public function fields()
74: {
75: return new HasManyFromRepository(
76: self::$fieldRepository->getModel()->newQuery(),
77: $this,
78: 'channel_fields.group_id',
79: 'field_group',
80: self::$fieldRepository,
81: 'getFieldsByGroup'
82: );
83: }
84:
85: /**
86: * Get channel fields of the specified type
87: * @param string $type name of a fieldtype
88: * @return \rsanchez\Deep\Collection\FieldCollection
89: */
90: public function fieldsByType($type)
91: {
92: return $this->fields->getFieldsByFieldtype($type);
93: }
94:
95: /**
96: * Get the cat_group attribute as an array
97: * @param string $data pipe-delimited list
98: * @return array of category group IDs
99: */
100: public function getCatGroupAttribute($data)
101: {
102: return $data ? explode('|', $data) : array();
103: }
104:
105: /**
106: * Return the channel_name when cast to string
107: *
108: * @var string
109: */
110: public function __toString()
111: {
112: return $this->channel_name;
113: }
114: }
115: